本文介绍了PhpStorm:继承方法返回类型的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Behat 子上下文类,我需要从主上下文调用一个方法,例如$this->getMainContext()->fooBar().PhpStorm 非常合理地警告我 fooBar() 不存在,因为它期望 getMainContext() 返回一个 ExtendedContextInterface,而不是我的具体 FeatureContext.

Using a Behat sub-context class I need to call a method from the main context, e.g. $this->getMainContext()->fooBar(). PhpStorm quite reasonably warns me that fooBar() doesn't exist, because it expects getMainContext() to return an ExtendedContextInterface, not my concrete FeatureContext.

有没有办法注释我的子类来告诉 PhpStorm getMainContext() 实际上返回我的具体类?

Is there a way to annotate my sub-class to tell PhpStorm that getMainContext() actually returns my concrete class?

一个解决方案是覆盖 getMainContext() 只是为了有一个方法来添加我自己的 PHPDoc,从而指定不同的返回类型,但添加一个方法只是为了在一个IDE 很糟糕.

One solution is to override getMainContext() just to have a method on which to add my own PHPDoc, thus specifying a different return type, but adding a method just to get nicer code sense in an IDE is horrid.

顺便说一句,我知道这有点老套,理论上我的子上下文不应该依赖于我的主要上下文具有特定的具体实现;在现实中,虽然 Behat 并没有做到这一点.

BTW, I know this is all a bit hacky and that theoretically my sub-context shouldn't depend upon my main context having a particular concrete implementation; in reality though Behat doesn't make that practical.

推荐答案

使用标准的@method注解类的作品:

Using the standard @method annotation for the class works:

 /**
  * @method FeatureContext getMainContext()
  */
 class SubContext extends BehatContext
 {
     public function foo()
     {
          $this->getMainContext()->bar();
     }
 }

这篇关于PhpStorm:继承方法返回类型的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:11