Search code examples
intellij-ideaphpstorm

PhpStorm "Cannot find declaration to go" with custom class instance


PhpStorm is my favorite IDE, I use it on a daily basis. Currently I work on a PHP project that use a custom PHP framework. This project and framework do not follow best PHP practices and do not focus on writing clean, readable code.

In the source code we call functions to get a new instance of a class. For example:

cAlert()->addError('...')

The function cAlert is declared on a PHP file included by the framework:

function cAlert() {
    return c('Alert');
}

function c($className) {
    return ClassCall::getActive($className);
}

ClassCall create a new instance or get an active instance of the class. Finally the Alert class is used:

class Alert {
    public function addError($error) {
        ...
    }
}

Problem

While editing the source code, I would like to navigate to the location of methods. This PhpStorm/IntelliJ feature doesn't seem to undestrand this specific architecture.

PhpStorm cannot find declaration to go to for the addError method of the class Alert (but it can find of course the cAlert method).

enter image description here

Question

I understand that PhpStorm cannot automatically resolve this method declaration.

Is it possible to configure the IDE in order to manually link, in my example, cAlert() with the Alert class?


Solution

  • Add a PHPDoc right above the cAlert() function declaring which class is returned:

    /**
     * @return My\Application\Class
     */
    function cAlert() {
        return c('Alert');
    }
    

    Then PHPStorm knows which class to look up.