Posibility to give phpstorm hint about return type (dynamicly from input string), то get correct autocomplete for return object

Good time of day.

I have a lot, a lot of factory methods in my code like:

/**
* @return libDbConnection
*/

function dbConnection(){
  return $this->getSingleton('libDbConnection');
}

/**
* @return libRequest
*/

function request(){
  return $this->getSingleton('libRequest');
}

The only meaning of all this methods is to give phpstorm a hint about return type то get correct autocomplete for return object.

Is there any way to give phpstorm a hint something like:

/**
* @specialHint Hey, phpstorm take first string argument of this method and take it as return type object 
*/

function getSingleton($libName){
  //if $libName == 'libDbConnection' - give autocompletion for libRequest if $libName == 'libRequest' - give autocompletion for libRequest
  return $this->_loadLogic($libName);
}

Thanks.

1 comment
Comment actions Permalink

Hi there,

PhpStom can do that for static methods (e.g. SomeClass::getSingleton('MyClass'); ). Check this link for details on how to provide such info: https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata

Unfortunately you case (ordinary methods like $this->getSingleton(); ) is not yet supported. Checkout DynamicReturnTypePlugin -- it should do what you want.

0

Please sign in to leave a comment.