Code complete not working on class functions.

Hello,

I've just downloaded the demo of PhpStorm afteran incredibly frustrating couple of hours with phpDesigner. I've code some very basic code, and what is concerning is that PhpStorm does not appear to be performing any code complete on it - is this a limitation of a demo?

File 1:

<?php
    class TestClass{
        public function GetNumber(){
            return 55;
        }
        public static function GetTestClasses(){
            return array(new TestClass(), new TestClass(), new TestClass());
        }
    }
?>


File 2:

<?php
    $TestClasses=TestClass::GetTestClasses();
    for($Index=0;$Index<count($TestClasses);$Index++){
        $Current=$TestClasses[$Index];
        echo($Current->GetNumber());

        echo('<br>');
    }
?>


The code executes just fine, however in File 2, after typing $Current-> I could expect GetNumber to appear, yet it doesn't. As I say I'm only running the demo and I'm unsure if this is a limitation, however I would fully expect PhpStorm to be able to evaluate this. The two files are in different directories, although PhpStorm can find the TestClass from File 2 as I can hold CTRL and click 'TestClass' and it will jump right to it.

Thanks.

2 comments

Hi there,

The "demo" version has no limitations -- it's fully functional version that has 30-days trial license. That's it.

The "problem" is in your code. To be specifically -- complete lack of documentation (type hints specifically). Adding 3 lines (or single line in compact form) of PHPDoc solved the problem for your example:

class TestClass
{
    /**
     * @return TestClass[]
     */
    public static function GetTestClasses()
    {
        return array (new TestClass(), new TestClass(), new TestClass());
    }

    public function GetNumber()
    {
        return 55;
    }
}


screen01.png

Thing is: PhpStorm is not soooo advanced right now and does not see the difference between return array() and return array(someClass, someClass). -- the return type in both of such cases is just an array.

What I did is told IDE that return type is array of TestClass.

Alternative solution (when class cannot be altered, e.g. 3rd party library/framework) is to add similar kind of comment in place of usage:

$TestClasses = TestClass::GetTestClasses();
for ($Index = 0; $Index < count($TestClasses); $Index++)
{
    /** @var TestClass $Current */
    $Current = $TestClasses[$Index];
    echo($Current->...

0

Thank you very much for taking the time to reply. Fortunately, all code being written is my own so I am able to make liberal use of PHPDoc, nevertheless I appreciate the alternative.

0

Please sign in to leave a comment.