How to enable inherited static method suggest/autocomplete?

Hello!

I have 2 classes:

abstract class SingletonAbstract
{

    private static $_instances = array ();

    public static function getInstance ()
    {
        $class = get_called_class ();

        if (!isset (self::$_instances[$class])) {
            self::$_instances[$class] = new $class ();
        }

        return self::$_instances[$class];
    }

}


and


class Fetcher_Admin
    extends SingletonAbstract
{
}


I want PhpStorm to show suggest/autocomplete on Fetcher_Admin methods, when i type something like this:

 Fetcher_Admin::getInstance ()->


I tryed a lot of phpdoc keywords - @static, @method, but always fail.

Does anybody knows a solution?

4 comments

If you can afford to make an assigment to a variable -- then this can be easily done. See example below:

/** @var Fetcher_Admin $fetcher */
$fetcher = Fetcher_Admin::getInstance();
$fetcher->

(Obviously you need to add some "real" methods in order to see it working)

Another way is to redefine getInstance() method in each class and provide correct retrun type (something like this):

class Fetcher_Admin extends SingletonAbstract
{
    /**
     * @static
     * @return Fetcher_Admin
     */
    public static function getInstance ()
    {
        return parent::getInstance();
    }
}

and then you can use the code in "normal" way:

Fetcher_Admin::getInstance()->
0

Thank you!

But first way requires some additional code every Fetcher_Admin usage, and second - add one more unnesessary "proxy" call. This is not a great problem, but... May be there is more beautifyl way?

0

AFAIK no other way at the moment. Most of the people (based on comments to other forum posts/tickets) are happily (arguable point) going using the first route.

The problem with this concept that implemented in your code (the way how getInstance() is implemented) is that IDE cannot reliably evaluate the value of $class variable during design/coding time (at least ATM) -- that can only be done reliably during execution by PHP interpreter itself.

But you better wait for an answer from one of the developers -- they may know better. In mean time you can watch/vote/comment this ticket: http://youtrack.jetbrains.net/issue/WI-3330?projectKey=WI or search for other similar/related tickets.

0

Well, your answer is quite complete and we have nothing to add. Several other improvements to type inference system will improve the situation.

0

Please sign in to leave a comment.