Autocompletion on variables

I'm having troubles forcing the type of a variable to a specific class in order to be able to use autocompletion.
In other IDEs (NetBeans and Eclipse) I could use the pseudo-phpDoc @var as so:

/* @var $variable Class */

(notice the single * and the inversion of type and name).
I've also tried to use a more standard @var, this way:

/** @var Class $variable */

In this case the autocompletion for variable name (while typing "$variable") worked, with even the indication of the right type "Class" on the right of the popup, but then typing -> or even Ctrl+Space endend in a "No suggestions" message.
I need this kind of trick in order to use autocompletion with foreach variables, don't know if its the right way to achieve it, and sorry if this question was answered before, I've done a quick search in the forum but found nothing.
Thanks a lot,

  Gabriele Genta (aka demerzel3)

Comment actions Permalink

Basically the
/** @var Class $variable */
should work in most contexts.

In case of foreach you should annotate ITERATED COLLECTION like this:
/** @var ItemClass[] $list */

Comment actions Permalink

I found why @var didn't work in the beginning: I was using the same variable in other foreach loops before the annotated one, with different implicit types.
Renaming those variables removed the problem, and this syntax now works just fine:
/** @var Class $variable */

Anyway, the annotation you proposed on the collection is much cleaner and works perfectly as well,
thanks a lot :).
(loving PhpStorm, by the way)

   Gabriele Genta

Comment actions Permalink

Will this work in Javascript? Would be very help to limit autocomplete based using a known type rather than depending on IDE to infer it.


Comment actions Permalink

A sort of. For JavaScript you need to use JSDoc.
E.g. for local variables it will look like
/** @type window */
var x;

Comment actions Permalink

is there a way to activate the type-hinting like in Netbeans / Zend-Studio?

/* @var $tpl Template */

instead of

/** @var Template $tpl */

I need this for compatibility of code between different IDEs.

thanx for your help.


Please sign in to leave a comment.