2

Is there any type casting for PHPDocumentor in PHPStorm? My PHPStorm IntelliSense is stubborn and i need to teach him how to behave.

This is my case:

I have a manager that instantiates classes that all extend same class. Usually i would cast my class after it was returned but PHP doesn't know how to do that. IntelliSense and PHPDocumentor support would be enough for me but i don't know how to type cast.

This is the code:

class Plugin 
{

}

class HelloPlugin extends Plugin
{
    public function hello()
    {

    }
}

class PluginManager 
{
    /** @var HelloPlugin */
    public $helloPlugin;

    function __construct()
    {
        $this->helloPlugin = $this->getPlugin('HelloPlugin');

        // Here my PHPStorm IntelliSense doesn't provide 'hello()' function for 'helloPlugin' because return type overwrote original type
        // I get error: method 'hello' not found in class
        $this->helloPlugin->hello();
    }

    /**
     * @param string $pluginClass
     * @return Plugin
     */
    function getPlugin($pluginClass)
    {
        return new $pluginClass;
    }

}
3
  • i don't think its a duplicate because this is not case with static functions, and that answer only works for static methods. Commented Nov 19, 2013 at 15:35
  • @tlenss Not really -- PHPDoc is already there (with correct type hint), but IDE overwrites it. Commented Nov 19, 2013 at 15:35
  • @LazyOne You are right.. somehow i thought he was calling a static factory. Commented Nov 19, 2013 at 15:39

1 Answer 1

2

I do not think it's actually possible :( , unless you rewrite your code somehow (just for PhpStorm ... no way).

Your code and PHPDoc is fine -- it's an IDE issue -- it temporarily (within that method only) overwrites manually provided type hint with the one it detects (it will work fine in other methods). Please vote: http://youtrack.jetbrains.com/issue/WI-17047

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

5 Comments

hi thanks for answering, it was a typo i fixed it. but error is still here. i will vote on your link. hope we get it soon. i will search more then i will mark your answer as the right one if i don't find any solutions.
@ÁlvaroG.Vicario its not weird. You have put type Plugin|HelloPlugin , but this needs to be dynamic. I guess this is just an issue with PHPStorm.
@Alvaro Nothing weird -- check value of @return tag for getPlugin types in your code and original/mine :)
Yeah, sorry, I hadn't noticed the typo and I had been playing with the code.
I'd agree that it is the return tag of getPlugin() that is driving the behavior you're seeing. I'd further argue that a method's return type should take precedence over a property's var type, as shown in the example. At runtime, it certainly would take precedence :-)

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.