Method flagged as not-found...

I am getting warnings in my source code when I access public methods defined within singleton objects.  (This is the only time I get warnings...)

In my root script, for example, I have:

global $somevar;
...
$somevar = someobject::singleton();
...
$somevar->dostuffs(params);  // warning generated here:  (Method dostuffs not found in class)

where dostuffs is declared in:

class someobject
{
   ...
   private function __construct() { ... }
   ...
   public static function singleton()
   {
      ...
      $c = __CLASS__;
      self::$instance = new $c();
      ...
      return(self::$instance);
   }
   ...
   public function dostuffs($params) { ... }
   ...
}

I can suppress the warning in the inspection settings, but that globally disables functionality I rely on...
I don't want to suppress the warning in-line because of the inserted comment:

/** @noinspection PhpUndefinedMethodInspection */

... added to source will confuse those not using the IDE or the future code-bunnys that inherit this drek...

How can fix the warning display in the IDE?

thanks!

--mike

0
4 comments

Hi Michael,

Based on the code example you provided adding correct PHPDoc (@return) for singleton() method should do the job:

/**
* @return someobject
*/

public static function singleton()
{
      ...
      $c = __CLASS__;
      self::$instance = new $c();
      ...
      return(self::$instance);
}


There are few pending tickets on Issue Tracker about support of @return static for such methods (so that final/real class will be automatically detected -- will not work in 100% cases, but should work ok in most of them, at least most obvious), but it is not yet implemented properly -- planned for v5

/**
* @return static
*/

public static function singleton()
{
      ...
      $c = __CLASS__;
      self::$instance = new $c();
      ...
      return(self::$instance);
}



In case singleton() method is in parent class only (but you creating child classes, where such method is not overridden) then more general approach with PHPDoc comment with type hint (/** @var MyActualClassName $somevar*/) is required. The following should of the job in other cases (works fine everywhere in my code). For example:

/** @var someobject $somevar */

$somevar = someobject::singleton();

0

Thanks, Andriy!

0

I have updated my answer *a bit* -- check if it will be more useful.

0

Like you pointed out, using @return static is not 100% -- I see where it's working for one of my classes, but not for another.

Regardless, the explicit PHPDoc declaration takes care of the issue and makes things green so I have a happy!

Thanks again, Andriy!

--mike

0

Please sign in to leave a comment.