如何处理“在课堂上找不到的方法"?警告魔术实施的方法? [英] How to deal with "method not found in class" warning for magically implemented methods?

查看:81
本文介绍了如何处理“在课堂上找不到的方法"?警告魔术实施的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坐在一个大型代码库上,其中包含几个类,这些类通过魔术实现的方法(使用__call__callStatic)公开功能.例如:

I am sitting on a large codebase that contains several classes that expose functionality through magically implemented methods (using __call and __callStatic). For example:

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

问题在于PhpStorm认为test()方法不存在,因此它在呼叫站点发出警告.这有点烦人,因为代码当然会运行并按预期运行.

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.

我已经通过检查如果类中存在__magic方法,则降低严重性"选项来降低严重性,但是我更愿意采用以下一种方法:

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:

  1. 完全禁用特定类的此功能,仅
  2. 与IDE一起工作而不是反对它-向其提供我已经拥有的信息,以便我们达成共识

以上是否可能?如果是这样,怎么办?

其他奖励问题:考虑链接方法调用的情况.

Additional bonus question: consider the case where method calls are being chained.

$f = new Foo;
$f->test()->chain()->moreChain();   // potentially runs fine

假设对$f->test()的魔术调用返回了适当的内容,则后续的调用(可能但不一定是魔术)也可以正常工作.但是,由于我无法告诉IDE test()返回的内容,因此它会将其余整个调用链也标记为缺少方法.更糟糕的是,降级严重性"设置不适用于这些警告,因为IDE不知道这些中间对象应该属于什么类.

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.

有没有可以解决此问题的解决方案?

尽管用@method注释记录魔术方法似乎可行,但我必须假定这种方法目前存在一些问题,因为花了一些工夫才解决了这些相关的错误:

Even though documenting the magic methods with @method annotations 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:

  • 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.

推荐答案

好,您可以转到首选项菜单,在检查下,转到未定义->未定义方法,然后检查是否存在__magic方法降级的严重程度.

Well, you can go to the preference menu, under Inspections, go to Undefined -> Undefined Method and check Downgrade severity if __magic methods are present.

这将使标志的严重性降低(而不是警告,而不是信息),这仍将使您的文档检查处于绿色状态.

That would make the flag less severe, (instead of Warning, as Info), which would still give you a green light on your document check.

除了可能会使用的每种方法的目标类上具有@property@method PHPDoc表示法之外,我什么都没有知道.

There's nothing else I'm aware of aside from having @property or @method PHPDoc notations on the target class for every method that's likely to be used.

这篇关于如何处理“在课堂上找不到的方法"?警告魔术实施的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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