PhpStorm 2019.2 unused element in Symfony controller
You can add your controllers and their actions as entry points. This way, those classes and methods will not be reported as dead code.
If your controllers are in src/Controller
and your action methods have the -Action
suffix, you can add these two rules as entry points in Settings| Editor | Inspections
:
Class Member/Controller/*Controller *Action/Controller/*Controller
The unused code inspection will not recognize this code being called since PhpStorm can not properly inspect how controllers are called in Symfony's HttpKernel::handleRaw()
. Potentially this can happen with other classes, e.g. when the Service Container did not pick up the class it might not be recognized as used, yet. In Symfony projects building the cache, e.g. via bin/console cache:clear
(which clears the old cache and warms up the new one) should help most of them, but definitely not always and not with controllers.
You can also run the inspection once on the whole project and then mark your controllers as entry points, which will automatically mark those classes as (potentially) used code. When you do this PhpStorm will add an entry to the file .idea/misc.xml
in your project to make sure it's ignored every time.
If the high rate of false positives bothers you, you can deactivate the feature or dial it down, so it will only look at protected/private methods for instance. You can find the option under: Settings/Preferences | Editor | Inspections | PHP | Unused
See also: https://blog.jetbrains.com/phpstorm/2019/04/locating-dead-code/
PHPStorm 2020
Use Suppressed Annotations on @Route
The quick way
- Hover the grayed function, or click on it and press
Alt + Enter
- Click on Suppress if annotated by '@Route'
Manually
- Go to
Settings... > Inspections > PHP / Unused / Unused declarations > Entry points >Suppressed Annotations
- Add
@Route
Note
Both methods does exactly the same and you only have to it once.