Is there a way to make PhpStorm's autocomplete "go deeper"?
Asked Answered
G

2

7

In PhpStorm, if I create an object, then I have all auto complete on that object working fine:

$object = new MyClass();
$object->getNa...

Will auto complete to

$object->getName();

So far so good, but if I get returned an object through the first method, then the auto complete will not work on that.

$car->getDriver()->getNam...

Will show an empty list. The getDriver method has its PHPDoc @return tag set to 'Driver' though and in some other IDEs, this therefore works to get the correct auto complete.

Wondering if there's a setting that I missed somewhere or if PhpStorm doesn't offer this kind of advanced auto complete yet?

Guv answered 1/10, 2011 at 10:44 Comment(5)
mmh, my PHPStorm does go deeper, indeed. Are you sure the 'deeper' Classes are known to the IDE?Thunderclap
Not a direct answer, but a PHP IDE should certainly do this. I use NetBeans, and for well-structured projects (without any syntax errors in classes that would upset the class scanner) it will do nested auto completion fine.Quarters
have you set your "@return type" correctly for the getDriver function?Corey
Yeah, the @return type hints are all there. It's a symfony project, and it's usually not working on the symfony classes. I'm a bit puzzled by this that all of you seem to have it work fine. This is the only thing I miss to completely switch to PhpStorm...Guv
You switched contexts in your example. At first, you are working with $object, which is explicitly assigned an instance of MyClass, but in your second example, you are working with $car, which we do not see the initialization for. Perhaps the problem here is not that PhpStorm doesn't know the return value of getDriver(), but that it doesn't know the type of $car.Mustard
S
8

The function getDriver() needs appropriate type-hints for the return value (function's docblock):

  * @return classOrInterfaceName

This is normally enough to have a IDE "go deeper". I'm pretty sure Phpstorm supports that, but I'm not a Phpstorm user.

Take care the file with the interface/class is within the project or referenced to it.

As a work around you can assign the return value to a variable and type-hint that variable. Might not be that comfortable but can help.

Stormie answered 1/10, 2011 at 11:8 Comment(4)
Yikes, this is weird! It's a symfony project, so most type hints are there, and I always put mine in the classes I write. On Netbeans, the autocomplete does work all the way on the same project...Guv
Well, if projects have dependency injection containers (or hide concrete implementation by factory), you can not always have type hints as types are only known at runtime. You probably run into this, as symfony2 is that flexible with it's nice DI container. You might want to go with manually hinting your variables, see How do I make my PHP IDE understand Dependency Injection Containers?.Stormie
Yep, knew about the issue on the DI since you can't know what ->get('service') will return as it depends on the service you're trying to get. But it happens to me on classes that are not hidden through DI right now. In fact using Netbeans, I get the right autocomplete showing up in cases where PhpStorm doesn't show anything. Could this be a limit of the demo version I'm still using right now?Guv
I must admit, I'm not a PHPStorm user. I would say that PHPStorm should be able to do the same like Netbeans or Eclipse, maybe there are some settings you need to set or methods you need to call before this feature is enabled in that level of depth.Stormie
H
3

Please ensure that only one definition of class Driver exists across all your project files. This is crucial for current versions of PhpStorm

see http://youtrack.jetbrains.net/issue/WI-2202 and http://youtrack.jetbrains.net/issue/WI-2760

Horrorstruck answered 5/10, 2011 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.