10

I have installed the Symfony2 plugin for Phpstorm but I can't get the IDE to see these existing services or other injected objects. Can these be fixed somehow, so the warnings go away?

enter image description here

androidu
  • 4,678
  • 6
  • 36
  • 51
  • In order to have a full code completion for annotations of Symfony framework and others, you should install first the Symfony Plugin then PHP annotations, this article shows what you should install in PHPStorm to code faster Symfony Applications http://refreshmymind.com/symfony-plugins-phpstorm/ – Charaf JRA Jan 07 '16 at 10:59

6 Answers6

26

I had a similar problem and would recommend double checking the following...

  1. As @Marcel suggested, check your Symfony2 plugin is turned on...

    1.1. Settings > Symfony2 plugin

    1.2. Check the paths to the these directories are correct, for some reason I had two symfony projects within a project so the default paths were not accurate

Symfony2 plugin

  1. You should double check that the plugin's reference to your container XML is accurate, by default it will be "app\cache\dev\appDevDebugProjectContainer.xml" as you can see mine had to be updated and you can see the path is now "EXISTS", it said "ERROR" before

    2.1. Settings > Symfony2 plugin > Container

    2.2. enter image description here

  2. Then, as per @Igor's suggestion clear your cache

Carlton
  • 5,533
  • 4
  • 54
  • 73
3

Finally, thanks to the @googol help, I've figured out a way to specify services definitions files in custom paths with a YML format:

You only have to create or modify the .idea/symfony2.xml file including the following tag: xml <option name="containerFiles"> <list> <container_file path="custom/path/to/your/services_definition.yml" /> </list> </option>

You'll have to restart the PhpStorm and that's it! :)

Here you have my complete symfony2.xml file as an example:

xml <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="Symfony2PluginSettings"> <option name="pluginEnabled" value="true" /> <option name="twigAnnotateTemplate" value="false" /> <option name="twigAnnotateAsset" value="false" /> <option name="twigAnnotateAssetTags" value="false" /> <option name="twigAnnotateRoute" value="false" /> <option name="twigAnnotateTranslation" value="false" /> <option name="phpAnnotateTemplate" value="false" /> <option name="phpAnnotateService" value="false" /> <option name="phpAnnotateRoute" value="false" /> <option name="phpAnnotateTemplateAnnotation" value="false" /> <option name="phpAnnotateTranslation" value="false" /> <option name="phpHighlightServices" value="true" /> <option name="codeFoldingTwigRoute" value="false" /> <option name="codeFoldingTwigTemplate" value="false" /> <option name="codeFoldingTwigConstant" value="false" /> <option name="containerFiles"> <list> <container_file path="custom/path/to/your/services_definition.yml" /> </list> </option> </component> </project>

And here you have a feature request in order to include the possibility to add this custom path service definition through the plugin UI settings instead of having to edit the config file manually: https://github.com/Haehnchen/idea-php-symfony2-plugin/issues/573

JavierCane
  • 2,324
  • 2
  • 22
  • 19
1

Clear the cache for development environment. It should fix it if service really exists. Navigate to your project dir in terminal and type:

app/console ca:c

Igor Pantović
  • 9,107
  • 2
  • 30
  • 43
0

Check to make sure your plugin is at the latest version. I know past versions of the plugin did not resolve the services automatically.

Otherwise, you can add the following to tell the IDE what variable type $exportHelper is:

<?php

/* @var $exportHelper \Acme\Bundle\ExportBundle\Service\ExportService */
$exportHelper = $this->get('Acme.csv.service');

This will then give you auto-completions when typing something like "$exportHelper->".

jayem
  • 229
  • 2
  • 11
  • I have the latest version for the symfony plugin and also I use the annotations. – androidu Dec 14 '13 at 11:52
  • Can you verify that the namespace/class path is correct by CTRL clicking on the class name to jump to that class. Do other services work automatically? – jayem Dec 16 '13 at 18:30
  • I can ctrl+click only if I type hint using annotations. The warning I get is just because of phpstorm's syntax highlighter. – androidu Dec 17 '13 at 08:47
0

If the service is defined in XML and not in YAML then the auto-completion works out-of-the-box without the need to use the @var annotation.

You can try this sample definition that will provide you with autocomplete when you want to use one of the defined services.

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">


    <services>
        <service id="activityService" class="ActivityManager">
            <argument type="service" id="activityWriter"/>
        </service>
        <service id="activityWriter" class="DirectStatsActivityWriter">
        </service>
    </services>
</container>

)

0

So it seems that I didn't enable my Symfony2 plugin and that was the reason nothing Symfony related worked. I assumed it was enabled by default after installation. Oh well

androidu
  • 4,678
  • 6
  • 36
  • 51