user936965
user936965

Reputation:

PhpStorm docs hint specified outcome of method

Is it possible for PhpStorm to understand the outcome of a method with specified parameters or when called from a specific class to know the outcome if it's defined in the PHPDoc?

For example:

class demo {
    public static function getInstance($className)
    {
        return $className::Instance();
    }
}
class someClass {
    public function Instance() {
        return new someClass();
    }
}

class otherClass {
    public function Instance() {
        return new otherClass();
    }
}

demo::getInstance('someClass'); // PHPstorm should understand this would return someClass
demo::getInstance('otherClass'); // PHPstorm should understand this would return otherClass

At the moment my PhpStorm says

Method getInstance not found in string.

For code hinting I'd like PhpStorm to understand what sort of class is returned based on the parameter value. It would be fine to put that data in the PHPDoc or anything like that, just not additional methods please.

Upvotes: 0

Views: 105

Answers (1)

maximkou
maximkou

Reputation: 5332

First, you call non-static methods statically.

Second, you can note return type, like this:

/**
 * @return someClass|otherClass
 */
public static function getInstance($className)
{
    return $className::Instance();
}

Upvotes: 2

Related Questions