Search code examples
phpstormyaf

PhpStorm does not support yaf's init method


In other class, PhpStorm can identify __construct() function, but in yaf controller it can not identify the initialization method init(), resulting in the init() can not trace initialization operation.

class TestController extends Yaf_Controller_Abstract{
    private $model;
    public function init() {
        $this->model = new TestModel();
    }

    public function test(){
        $this->model->testDeclaration();
    }
}

class TestModel{
    public function testDeclaration(){
    }
}

In this example, I want use 'go to declaration' from test function $this->model->testDeclaration(); to testDeclaration() function in TestModel class. But it does not work.

PhpStorm tells me:

Cannot find declaration to go to

How can I use 'go to declaration' correctly here?


Solution

  • In other class, PhpStorm can identify __construct() function, but in yaf controller it can not identify the initialization method init(), resulting in the init() can not trace initialization operation.

    PhpStorm has special treatment for __constructor() -- it tracks what type class variable/property will get if it will have any assignment operations within the method body.

    For example, in this code it knows that $this->model will be instance of TestModel class -- IDE keeps this info even outside of the __construct() method body.

    For other methods, like init() in your case, such info gets discarded outside (so it's local to the method body only).


    You can easily resolve this by using simple PHPDoc comment with @var tag where you will provide type hint for model property:

    /** @var \TestModel Optional description here */
    private $model;
    

    Make a habit of providing type hint for all properties/class variables, even if IDE autodetects its' type -- it helps IDE in long run.

    https://phpdoc.org/docs/latest/references/phpdoc/tags/var.html