如何为IDE编写魔术(_call和_callStatic)方法 [英] How to document magic (_call and _callStatic) methods for IDEs

查看:567
本文介绍了如何为IDE编写魔术(_call和_callStatic)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过许多快乐的岁月编码,在记事本++和崇高的时候,我被告知要给一个PHP IDE一个去。我正在尝试phpStorm,它似乎很好。代码完成和文档是一个很好的功能,但是当使用魔法方法时,并不适合我。



我们的情况是这样的:

 抽象类a {
public static function __callStatic($ method,$ args)
{
if( strpos($ method,get_by_)=== 0)
{
// do stuff
} elseif(strpos($ method,get_first_by_)=== 0){
// do stuff
} elseif($ method ==get_all){
// do stuff
}
}
}

class b扩展一个{
//一些更多的东西
}

b :: get_by_user_id(27);
b :: get_first_by_id(156);
b :: get_all();

魔术callStatic方法允许我们通过一个或多个参数来获取对象的集合,函数调用。



我发现有一个@method语句用于这些情况,但phpStorm只是拾取了第一个语句。此外,我只能将返回类型设置为混合,因为我喜欢将其设置为所调用的类(在我的示例中为b)。



解决方案

使用类级别的PHPDoc注释 - 特别是 @method 标签 - 在PhpStorm中工作正常:

  / ** 
* @method static someClass get_by_user_id(int $ id)Bla-bla
* @method static someClass get_first_by_id(int $ id)
* /
抽象类a {
...

在上面:




  • @method - PHPDoc标签

  • static - 告诉这是静态方法

  • someClass $ this - 返回类型

  • get_by_user_id - 方法名称

  • (int $ id) code> - 方法签名:([[type] [parameter]< ...>] )

  • Bla-bla - 一些可选描述



    • 更多关于 @method





      PS
      虽然 @method static 在PhpStorm中可以正常工作(告诉IDE该方法是静态的)它可能不是(但是)由实际的phpDocumentor工具支持(对不起,一段时间没有使用)。






      或者:(在PhpStorm中,当然)设置|检查| PHP |未定义|未定义的方法 - >如果__magic方法存在于类中,则降级严重性 - 它不会以任何方式帮助这些方法的代码完成,但不会将这些魔术方法标记为未定义的方法错误。






      phpDocumentor的票关于使用RegEx /部分名称 @property / @method 标签(如何对文档有用,在处理代码完成时可能对实际的IDE有多少帮助):




      After many happy years coding in notepad++ and sublime, I've been advised to give a PHP IDE a go. I'm trying out phpStorm and it seems nice. The code completion and documentation is a great feature but isn't working out for me when magic methods are used. Is there a work around to get phpStorm to understand what's going on in magic methods?

      Our situation is something like this:

      abstract class a {
          public static function __callStatic($method,$args)
          {
              if(strpos($method,"get_by_") === 0)
              {
                  //do stuff
              } elseif(strpos($method,"get_first_by_") === 0) {
                  //do stuff
              } elseif($method == "get_all") {
                  //do stuff
              }
          }
      }
      
      class b extends a {
          // some more stuff
      }
      
      b::get_by_user_id(27);
      b::get_first_by_id(156);
      b::get_all();
      

      The magic callStatic method allows us to get a collection of objects via 1 or more arguments that make up the function call.

      I see that there is an @method statement for use in these cases but phpStorm is only picking up the first of these statements. Furthermore I can only set the return type to mixed where as I'd prefer to be able to set it as whatever class this was called on (b in my example).

      Any ideas or suggestions would be very gratefully received, thanks.

      解决方案

      Use class-level PHPDoc comment -- specifically @method tag -- works fine in PhpStorm:

      /**
       * @method static someClass get_by_user_id(int $id) Bla-bla
       * @method static someClass get_first_by_id(int $id) 
       */
      abstract class a {
      ...
      

      In the above:

      • @method -- PHPDoc tag
      • static -- tells that this is static method
      • someClass or $this -- return type
      • get_by_user_id -- method name
      • (int $id) -- method signature: ([[type] [parameter]<, ...>])
      • Bla-bla -- some optional description

      More about @method:

      P.S. While @method static works fine in PhpStorm (tells IDE that method is static) it may not be (yet?) supported by actual phpDocumentor tool (sorry, have not used it for a while).


      Alternatively: (in PhpStorm, of course) Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class -- it will not help with code completion for such methods in any way, but will not mark those magic methods as "undefined method" errors.


      phpDocumentor's ticket regarding using RegEx/partial names for @property/@method tags (how it can be useful for documentation and how little help it may bring to the actual IDE when dealing with code completion):

      这篇关于如何为IDE编写魔术(_call和_callStatic)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆