Search code examples
phpinheritancephpdoc

PHPdoc field inheritance : Field 'xxx' not found in 'xxx'


I am trying to document my code and to remove my IDE warnings on my code. I'm working with IntelliJ IDEA.

I have two classes, the first is an abstract Controller like this :

abstract class Controller {
   /** @var Service $service */
   public static $service;
   ...
}

The second one is a UserController, which extends the first :

/** @property UserService $service */
abstract class UserController extends Controller {
   public static function testService() {
      static::$service->testMethod();
   }
   ...
}

My issues :

  • If I declare a new function on the UserService without the @property tag, my IDE is telling me that Method 'testMethod' not found in Service
  • If I use the @property, the IDE is telling me that Field '$service' not found in UserController

How can I fix this ?


Solution

  • I would solve the need via a property override... this shouldn't hinder the code logic itself:

    abstract class Controller {
       /** @var Service */
       public static $service;
       ...
    }
    
    abstract class UserController extends Controller {
       /** @var UserService */
       public static $service;
    
       public static function testService() {
          static::$service->testMethod();
       }
       ...
    }
    

    Side note: the @var syntax for class properties doesn't need the variable name repeated... that's only needed when @var is used to denote type on a local variable. In the class property case, including the property name is actually just using the property name as the first word of the "description" portion.