Felice Ostuni Felice Ostuni - 3 months ago 74
PHP Question

PHPStorm autocomplete twig vars

I'm using PHPStorm 8.1 as IDE in a php project (laravel 4.1 project with twig on view side)

Will be great to have an autocomplete hint on vars on "twig side" like you can see here on this phpstorm plugin for symfony2:

http://symfony2-plugin.espend.de/languages/twig/index.html#phptypes

Basically in my twig view I declare the var "type" using a comment, then I would see hints on my model (farmaco):

{# farmaco \Farmaco #}
{% farmaco. %} <-- I would see hints -->


Note: I'm not using Symfony, however I've the symfony plugin installed but the autocomplete doesn't work for me on twig files for my laravel project.

Answer

You seem to be missing the @var annotation. The correct form would be:

{# @var foo \FooService #}
{{ foo. }} <!-- press CTRL+SPACE for typehints -->

I got this working for a non-symfony project without issues. Be sure to check that the symfony plugin is actually enabled for the project (it isn't by default) and restart the IDE after you enable it.

Also make sure that the type-hint point to the fully qualified class name.


As a sidenote, I have not (yet) gotten this to work for properties, i.e. foo.bar.* when the property is accessed through magic (using __get). For those instances the property needs to be put into a variable of its own:

{# @var foo \FooService #}
{{ foo.bar. }} <!-- no typehints for bar :-( -->
{% set bar = foo.bar %}
{# @var bar \BarService #}
{{ bar.  }} <!-- press CTRL+SPACE for typehints :-) -->