php 如何处理神奇实现的方法的“类中未找到方法”警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12582202/
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 deal with "method not found in class" warning for magically implemented methods?
提问by Jon
I am sitting on a large codebase that contains several classes that expose functionality through magically implemented methods (using __calland __callStatic). For example:
我坐在一个包含几个类的大型代码库上,这些类通过神奇实现的方法(使用__call和__callStatic)公开功能。例如:
class Foo {
public function __call($name, $parameters) {
echo "You called $name().\n";
}
}
$f = new Foo;
$f->test(); // runs fine, but PhpStorm flags as a warning
The problem is that PhpStorm thinks that the test()method does not exist, so it gives a warning at the call site. This is a little annoying, as of course the code will run and behave as expected.
问题是PhpStorm认为该test()方法不存在,所以在调用现场给出了警告。这有点烦人,因为代码当然会按预期运行和表现。
I have already tuned down the severity by checking the "downgrade severity if __magic methods are present in class" option, but I would prefer to either:
我已经通过检查“如果类中存在 __magic 方法时降级严重性”选项来降低严重性,但我更喜欢:
- completely disable this functionality for specific classes only, or
- work with the IDE rather than against it -- provide it with the information I already have so our views agree
- 仅针对特定类完全禁用此功能,或
- 与 IDE 合作而不是反对它——向它提供我已经拥有的信息,以便我们的观点达成一致
Is any of the above possible? If so, how?
以上任何一种都可能吗?如果是这样,如何?
Additional bonus question: consider the case where method calls are being chained.
额外的奖励问题:考虑方法调用被链接的情况。
$f = new Foo;
$f->test()->chain()->moreChain(); // potentially runs fine
Assuming that the magic call to $f->test()returns something appropriate the subsequent (possibly, but not necessarily, also magic) calls will work fine. However, since there is no way that I know of to tell the IDE what test()returns it flags the rest of the call chain as full of missing methods too. And to make matters worse, the "downgrade severity" setting does not apply to these warnings since the IDE does not know what class these intermediate objects are supposed to be.
假设魔术调用$f->test()返回适当的内容,后续(可能,但不一定,也是魔术)调用将正常工作。但是,由于我不知道有什么方法可以告诉 IDEtest()返回什么,它也将调用链的其余部分标记为缺少方法。更糟糕的是,“降级严重性”设置不适用于这些警告,因为 IDE 不知道这些中间对象应该是什么类。
Is there a solution that can also cover this case?
是否有解决方案也可以涵盖这种情况?
Update
更新
Even though documenting the magic methods with @methodannotations seems to work, I have to assume that there are currently several problems with this approach because it only took me a little work to come upon these related bugs:
尽管用@method注释记录魔术方法似乎有效,但我必须假设这种方法目前存在几个问题,因为我只花了一点点工作就发现了这些相关的错误:
- Type hinting for the method arguments does not work correctly with primitives
- Annotations work for one call, but not for chained calls
I do hope they fix them in a reasonable time frame.
我确实希望他们在合理的时间范围内修复它们。
采纳答案by Madara's Ghost
Well, you can go to the preference menu, under Inspections, go to Undefined -> Undefined Methodand check Downgrade severity if __magic methods are present.
好吧,您可以转到首选项菜单,在检查下,转到未定义 -> 未定义方法并检查降级严重性是否存在 __magic 方法。
That would make the flag less severe, (instead of Warning, as Info), which would still give you a green light on your document check.
这将使标志不那么严重(而不是警告,作为信息),这仍然会给您的文档检查亮绿灯。
There's nothing else I'm aware of aside from having @propertyor @methodPHPDoc notations on the target class for every method that's likely to be used.
除了可能会使用的每个方法的目标类上都有@property或@methodPHPDoc 符号外,我不知道其他任何东西。


回答by Richard A Quadling
Rather than globally turning off inspections by downgrading the severity of the inspection, you can add a comment to the single line to ignore just that particular inspection.
您可以向单行添加注释以忽略该特定检查,而不是通过降低检查的严重性来全局关闭检查。
/** @noinspection PhpUndefinedMethodInspection */
Assertion::NullOrAssertionDoesNotExist();
回答by daronjay
Building on what Madara said, I found it wouldn't downgrade down far enough for my liking no matter what severity I set it to, so I made a new Severity for undefined method that has no attributes whatsoever, and turned off the downgrade checkbox (see image below)
基于 Madara 所说的,我发现无论我将其设置为什么严重性,它都不会降级到我喜欢的程度,因此我为没有任何属性的未定义方法创建了一个新的严重性,并关闭了降级复选框(见下图)
If I hover over my magic methods now, it still brings up the popup message, but otherwise it doesn't distract me. One step better than just turning off inspection, because at least this way an actual undefined method can still be detected by hovering over the name
如果我现在将鼠标悬停在我的魔法方法上,它仍然会显示弹出消息,但不会分散我的注意力。比仅仅关闭检查好一步,因为至少通过这种方式,仍然可以通过将鼠标悬停在名称上来检测实际未定义的方法
回答by Qassoul
You can use a dynamic variable :
您可以使用动态变量:
$test = 'test';
$chaine = $f->$test(); // no phpStorm flag, & the code works

