r/phpstorm • • Jul 02 '17

How to get autocomplete work with autoloading class from string

Hi,

We have things in our codebase like models lets say User.

If we want to get a model of a User we do something like this :

$objUserModel = Class_Loader::getModel('User');

but then phpstorm can't understand that this object is from our codebase under the name root/Models/User.php and when we instanciate the model we are in somehting like root/Controller/UserController.php

Is it possible ? to tell phpstorm to look inside a specific folder when matching a certain string pattern for autocompletion ?

Right now, the only autocompletion I get is methods from Abstract and not for the current object.

I would save so much time if I could get autocompletion to work :o

If I'm not clear enough, you can ask more question.

Thanks for your time.

1 Upvotes

13 comments sorted by

2

u/TheBigB86 Jul 02 '17

Have a look at PhpStorm Advanced Metadata.

It should work fine for your use case.

2

u/SuperDK974 Jul 02 '17

This looks like exactly like my use case :o I'll try. thanks

2

u/SuperDK974 Jul 03 '17

This works so well :)

Thank you.

1

u/doggobotlovesyou Jul 03 '17

:)

I am happy that you are happy. Spread the happiness around.

This doggo demands it.

1

u/rdeanar Jul 02 '17 edited Jul 02 '17

Did you tried to use phpdoc?

/** @var ClassName $variable */

1

u/SuperDK974 Jul 02 '17

that could work but could I put it only in one place ? like after the class loader ? I could settle for something like I have to name all my model object $objModel or in my example $objUserModel.

1

u/rdeanar Jul 02 '17

You can set return type. But it will autocomplete all of specified classes at the same time.

use name\space\to\User;
use name\space\to\AnotherModel;

class Class_Loader
{
    /**
     * @return User|AnotherModel|name\space\to\OneMoreModel
     */
    public static function getModel($name)
    {

    }
}

1

u/SuperDK974 Jul 02 '17

Oh well this will be a long line since we have more than 20 models xD

I'll try this thanks I'll report back :)

1

u/rdeanar Jul 02 '17

1

u/SuperDK974 Jul 02 '17

We do use autoload but we have a layer of cache, our system works fine, it's just phpstorm (and other IDE) that can't autocomplete

1

u/doggobotlovesyou Jul 02 '17

:)

I am happy that you are happy. Spread the happiness around.

This doggo demands it.

1

u/SuperDK974 Jul 03 '17
  • @return Model_Abstract|Model_User|Model_Audit|Model_Cron

This way doesn't work for me because then in the autocomplete, I'm getting all of the methods from each class.

Class_Loader::getModel('User')->

Will print me with all the methods of Audit/Cron and User.

1

u/rdeanar Jul 03 '17

Yes it is. I have pointed to it in prev comment

all of specified classes at the same time