PhpStorm autocomplete/suggestions bug?

Let's say we have 2 php classes "test1" and "test2":
class test1 code:

class test1
{
    public function methodOne()
    {
    }

    public function methodTwo()
    {
    }
}

class test2 code:

class test2
{
    private $testVar;

    public function createInstance()
    {
        $this->testVar = new test1();

        $this->testVar-> // gives methodOne() and methodTwo() as suggestions, good! :)
        $this->testVar->methodOne(); // ok, no problem here.
    }

    public function executeMethod()
    {
        $this->testVar-> // no methodOne() and methodTwo() as suggestions?? ?:|
        $this->testVar->methodOne(); // PhpStorm says "Method 'methodOne' not found in class".
    }
}


Description:
As you can see, behind the "$this->testVar->" in the "executeMethod()", PhpStorm gives no autocomplete or any kind of method suggestions. This really surprised me because when I open the same code within NetBeans or Aptana Studio they gave (as expected) the methodOne() and methodTwo() as suggestions.

My Question:
Is this a bug in PhpStorm? I am doing something wrong? Maybe some PhpStorm IDE setting will fix this?

Additional info:
- it has been tested in PhpStorm 6.0 and EAP version 7.0 build 131.235 with their default "out-of-the-box" settings under Windows 7 and 8 (both 64-bits)
- i tried the option "Invalidate Caches" without succes

Please ask me when something is not clear and/or if you need additional information about this case ;)

2 comments

Hi there,

Is this a bug in PhpStorm?

No.

I am doing something wrong?

Yes and No at the same time.

Maybe some PhpStorm IDE setting will fix this?

Setting -- no. Just a bit of standard PHPDoc "magic".

Adding PHPDoc for $testVar field declaration resolves the issue:

    /** @var test1 */
    private $testVar;

  • It works in createInstance() method with no additional help because IDE sees how $this->testVar is instantiated ( = new test1(); ) in the same block (method body).
  • In executeMethod() such direct info is absent (who knows, maybe you are assigning completely different type to it in some another method). This is where PHPDoc comes in, as you specifically tell IDE what type this variable/field is.


P.S.
You should consider using PHPDoc for documenting your code (in case you are not using it already, of course) -- helps a lot (not only right now for IDE .. but for other peoplewho will use your code in the future  (including yourself, in a year or so when you may forget how this code works exactly)). You can Ctrl+Click on any known to IDE out-of-the-box class/function and it will take you to corresponding stub file -- you will see that PHPDoc is used heavily for documentation purposes (or check big modern open-source porojects like Symfony/Yii/Doctrine etc).

0

Thanks for your answer! Your advice and mentioning of PHPDoc makes sense to me. From now on I have an extra reason to use PHPDoc in my PHP code :p

0

Please sign in to leave a comment.