1

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?

2
  • 1
    Accordingly to your screenshot (#1) your cAlert() method returns nothing (void) -- that's why you see such "Cannot find declaration" message. Your particular code example can be solved by adding PHPDoc comment for cAlert() method with @return MyClass in it. Commented Nov 19, 2015 at 16:26
  • It was an error with my screenshot (updated). The cAlert() method return a instance of Alert through return ClassCall::getActive($className);. But many thanks because /** @return Alert */ solve the problem! Of course, that was it, how could I not have thought of that before! Commented Nov 19, 2015 at 16:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Same response that LazyOne. It's worked out well! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.