在使用 notepad++ 和 sublime 编码多年之后,有人建议我尝试一下 PHP IDE。我正在尝试 phpStorm,它看起来不错。代码完成和文档是一个很棒的功能,但在使用魔术方法时对我来说不起作用。有没有办法让 phpStorm 了解魔术方法中发生的情况?
我们的情况是这样的:
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();
神奇的 callStatic 方法允许我们通过构成函数调用的 1 个或多个参数来获取对象集合。
我看到在这些情况下有一个 @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)
*/
abstract class a {
...
上文中:
-
@method
-- PHPDoc 标签
-
static
-- 告诉这是静态方法
-
someClass
or $this
-- 返回类型
-
get_by_user_id
-- 方法名称
-
(int $id)
-- 方法签名:([[type] [parameter]<, ...>])
-
Bla-bla
-- 一些可选的描述
更多关于@method
:
- https://docs.phpdoc.org/latest/references/phpdoc/tags/method.html https://docs.phpdoc.org/latest/references/phpdoc/tags/method.html
- https://github.com/phpDocumentor/phpDocumentor2/blob/develop/docs/PSR.md#711-method https://github.com/phpDocumentor/phpDocumentor2/blob/develop/docs/PSR.md#711-method
P.S.
While @method static
在 PhpStorm 中工作正常(告诉 IDE 该方法是静态的),但实际的 phpDocumentor 工具可能(还?)不支持它(抱歉,有一段时间没有使用它了)。
或者:(当然是在 PhpStorm 中)Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class
-- 它不会以任何方式帮助完成此类方法的代码,但不会将这些魔术方法标记为“未定义方法”错误。
phpDocumentor 的门票关于使用正则表达式/部分名称@property
/@method
标签(它对文档有何帮助,以及在处理代码补全时它对实际 IDE 的帮助有多大):
- https://github.com/phpDocumentor/phpDocumentor2/issues/689 https://github.com/phpDocumentor/phpDocumentor2/issues/689
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)