PhpStorm: annotation for inherited method return t

2019-06-25 02:59发布

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.

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

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.

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.

1条回答
对你真心纯属浪费
2楼-- · 2019-06-25 03:50

Using the standard @method annotation for the class works:

 /**
  * @method FeatureContext getMainContext()
  */
 class SubContext extends BehatContext
 {
     public function foo()
     {
          $this->getMainContext()->bar();
     }
 }
查看更多
登录 后发表回答