Singleton code complete and methods

We are using singleton classes in our projects called by using getInstance.  IE:

class whatever {
    public static function getInstance($config = '') {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self($config);
        }

        return self::$_instance;
    }

    public function do() {
       return true;
    }
}

whatever::getInstance(array('db'=>'something'))->do();

The problem is that is appears phpStorm doesn't know how that they are a class inside the project and thus we cannot do code complete or method jumps or anything else with them.  Its sort of annoying.  Is there a way around this?  Some way to manually point them? Am I missing something?

3 comments
Comment actions Permalink

Hi Bryan,

Based on your example: Add appropriate PHPDoc comments, e.g.

class ClassWhatever
{
    
    /**
     * @param array|string $config
     * @return ClassWhatever
     */
    public static function getInstance($config = '')
    {


        if (!(self::$_instance instanceof self)) {

            self::$_instance = new self($config);
        }


        return self::$_instance;
    }


    /**
     * @return bool
     */
    public function doThis()
    {
       return true;
    }
}


ClassWhatever::getInstance(array('db'=>'something'))->doThis();

But speaking in general -- have a look at these related tickets:

0
Comment actions Permalink

Ah, my problem was I was just being generic and saying instance and not the class name.  THANK YOU!  That has been annoying me for months.

0
Comment actions Permalink

I noticed that having a @return <TYPE> breaks it if the type is not correct.
And the auto generated DocBlock ends up mixed when it should be object.

eg.
* @return mixed whatever  BROKEN

* @return whatever  WORKS
* @return object whatever  WORKS

Just thought it was worth mentioning as this can be a bit decieving as it is picky in that the TYPE either has to be correct or not present.

0

Please sign in to leave a comment.