How to make PHPStorm/IntelliJ Idea guess the return type of a method/function according to a passed parameter value

Basically what I've seen in many frameworkds is having a Registry/DIC/Service registry to which the developer makes calls to retrieve a class/object.

It is very widespread and more than 50% of my code and guess lots of other people is related to making calls like this:

$mailer = $services->get('mail');

After I do this all the intelligence of autosuggestions, parameter suggestion.... all the sweetness is gone.

Is there a way to describe the return type with annotations? Something like:

@return $service_name 'mail' \Swift\Mailer | 'response' \Response

A guide how to extend the annotation functionallity for certain classes into a plugin will be useful too. Like a framework-based plugin?

1 comment
Comment actions Permalink

Hi Anton,

The best way (in my opinion) of doing such thing right now (current PhpStorm version -- v3.0) is to provide PHPDoc comment for such variable (this guaranteed to work -- I'm having plenty of code like that, just not using namespaces), e.g.:

/** @var $mailer \Swift\Mailer */
$mailer = $services->get('mail');

or (the same, just variable name and variable type are swapped)

/** @var \Swift\Mailer $mailer */
$mailer = $services->get('mail');

There are some tickets already that aiming at implementing such functionality in general. Have a look at them (and vote/comment if required):


Please sign in to leave a comment.