Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm: annotation for inherited method return type?

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.

like image 648
Richard Turner Avatar asked Feb 14 '23 04:02

Richard Turner


1 Answers

Using the standard @method annotation for the class works:

 /**
  * @method FeatureContext getMainContext()
  */
 class SubContext extends BehatContext
 {
     public function foo()
     {
          $this->getMainContext()->bar();
     }
 }
like image 147
Richard Turner Avatar answered Mar 05 '23 15:03

Richard Turner