Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comment class for code completion in phpstorm

Suppose I have a class

class Class1
{
    public function method1(){
        return "hello world";
    }

}

and I have another class that uses this in a class

class Class2
{
     /** @var $firstClass Class1 */
     private $firstClass;

     public function __construct($firstClass)
     {
            $this->firstClass = $firstClass;

     }

     public function method2()
     {
            return $this->firstClass-> "I want code completion to work here"
     }
 }

How do I get the code completion to work using PHPDoc. I've searched phpdoc for scope, declaration, all kinds of stuff. I can't figure out the terms or placement of the comment line for phpdoc to pick this up.

I'm using phpstorm. Help!

like image 419
polyhedron Avatar asked Sep 25 '13 01:09

polyhedron


1 Answers

Check your class namespaces.

If class have namespace be sure you properly define them in comments or by alias.
Comments example:

...
    /** @var \vendor\namespace\Class1 */
    private $firstClass;
...

Alias example:

use \vendor\namespace\Class1;
...
    /** @var Class1 */
    private $firstClass;
...


Check project directories definition.

If your class directories not defined in project settings, PHPStorm can't index those classes. In this case code completion will not work. How to set up project directories in PHPStorm.


Clear PHPStorm cache.

Sometimes PHPStorm cache became invalid. Because of this, codecompletion may not work properly. Official how to cache cleanup.

like image 81
Alexander Yancharuk Avatar answered Nov 09 '22 09:11

Alexander Yancharuk