Features Releases

Locating Dead Code

When facing legacy code, probably the first thing you want to do is clean it up. PhpStorm 2019.1 can help you with this, particularly by finding and removing dead code with the new Unused Declaration inspection. It will carefully analyze your code and data flow, and highlight the entities that are never used.

To get started, make sure that the inspection is enabled under Settings/Preferences | Editor | Inspections | PHP | Unused. Now, when you work in the editor, PhpStorm will only highlight unused classes, class members, and functions:


To locate and view all unreachable declarations (that are present in your code but are never executed at runtime), you need to run the inspection in batch mode. This will build the reachability graph in order to find unused code “clusters” throughout the selected scope. To do this, select either Code | Inspect Code… or Code | Run Inspection by Name… | Unused Declaration.


As soon as the inspection runs, all of the found issues will be listed in the Inspection results:


From here, you can start eliminating dead code by safely removing the affected elements, or commenting them out. If you hit a false positive or otherwise want to keep the element as it is, you’ll have two possible options:

  • Suppress the inspection, which will add the @noinspection tag for the given statement or the entire file.
  • Add the element as an entry point. By default, all code in the global scope as well as tests is treated as reachable. If you know that a method or function is executed, you may add it as an entry point. The code inside the entry point is now executed and reachable, as well.
    When you add an entry point, your source code files stay unaffected, and the element’s record is stored with the project under .idea\misc.xml.

We hope that with this new inspection, you’ll find that modernizing apps becomes a lot easier and less painful! Let us know how it goes and what works or doesn’t work for you!

Your JetBrains PhpStorm Team
The Drive to Develop

image description