php 如何记录 IDE 的魔法(_call 和 _callStatic)方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15634021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to document magic (_call and _callStatic) methods for IDEs
提问by Rob Forrest
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?
在记事本++和崇高编码多年之后,我被建议尝试使用PHP IDE。我正在试用 phpStorm,它看起来不错。代码完成和文档是一个很棒的功能,但在使用魔术方法时对我不起作用。有没有办法让 phpStorm 了解魔术方法中发生的事情?
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.
神奇的 callStatic 方法允许我们通过组成函数调用的 1 个或多个参数来获取对象的集合。
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).
我看到在这些情况下有一个 @method 语句,但 phpStorm 只选择这些语句中的第一个。此外,我只能将返回类型设置为混合类型,因为我希望能够将其设置为调用的任何类(在我的示例中为 b)。
Any ideas or suggestions would be very gratefully received, thanks.
任何想法或建议将不胜感激,谢谢。
回答by LazyOne
Use class-level PHPDoc comment -- specifically @methodtag -- works fine in PhpStorm:
使用类级 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 {
...
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
@method
-- PHPDoc 标签static
-- 说明这是静态方法someClass
或$this
-- 返回类型get_by_user_id
-- 方法名称(int $id)
-- 方法签名:([[type] [parameter]<, ...>])
Bla-bla
-- 一些可选的描述
More about @method
:
更多关于@method
:
- https://docs.phpdoc.org/latest/references/phpdoc/tags/method.html
- https://github.com/phpDocumentor/phpDocumentor2/blob/develop/docs/PSR.md#711-method
- https://docs.phpdoc.org/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).
PS虽然@method static
在 PhpStorm 中工作正常(告诉 IDE 该方法是静态的),但实际的 phpDocumentor 工具可能(还没有?)支持它(抱歉,有一段时间没有使用它)。
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.
或者:(当然在 PhpStorm 中)Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class
——它不会以任何方式帮助完成此类方法的代码,但不会将这些魔术方法标记为“未定义方法”错误。
phpDocumentor's ticketregarding 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):
phpDocumentor关于为@property
/@method
标签使用 RegEx/部分名称的票证(它如何对文档有用以及在处理代码完成时它对实际 IDE 带来的帮助有多大):
回答by Yauheni Prakopchyk
Somewhat related to original question:
与原始问题有些相关:
You can also define this in phpstorm meta file. Here's an example for factory method (v2016.3):
您也可以在 phpstorm 元文件中定义它。这是工厂方法的示例(v2016.3):
// Define in .phpstorm.meta.php
namespace PHPSTORM_META {
$STATIC_METHOD_TYPES = [
\Factory::create('') => [],
];
}
// Then use in code
$factory = new \Factory();
$user = $factory->create(\User::class);
// Here you get autocomplete.
$user->subscribe();
This way you don't have to docblock every possibility when magic happens.
这样你就不必在魔法发生时阻止所有的可能性。
Have some docsfor details.
有一些文档了解详细信息。