如何为IDE编写魔术(_call和_callStatic)方法 [英] How to document magic (_call and _callStatic) methods for IDEs
问题描述
我们的情况是这样的:
抽象类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
- 一些可选描述 - http://www.phpdoc.org/docs/latest/references/phpdoc /tags/method.html
- https://github.com/phpDocumentor/phpDocumentor2/blob/develop/docs/PSR.md#711-method
更多关于 @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 tagstatic
-- tells that this is static methodsomeClass
or$this
-- return typeget_by_user_id
-- method name(int $id)
-- method signature:([[type] [parameter]<, ...>])
Bla-bla
-- some optional description
More about @method
:
- http://www.phpdoc.org/docs/latest/references/phpdoc/tags/method.html
- https://github.com/phpDocumentor/phpDocumentor2/blob/develop/docs/PSR.md#711-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屋!