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
@noinspectiontag 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
Subscribe to Blog updates
Thanks, we've got you!
Qodana Clone Finder: Early Access Program
In December 2020, we announced the EAP for Qodana, which is rapidly evolving into a comprehensive platform that allows companies to perform multi-level evaluations of the quality of code they own, contract, or purchase. Qodana helps you detect bugs without relying on an IDE, either on a local machi…
PhpStorm 2020.2 EAP #3
The Early Access Program for PhpStorm 2020.2 is in full swing and today we’ve got the third build of the 2020.2 EAP for you. In this blog post, you can read about improvements for Git installed in WSL2 and Search Everywhere. Download PhpStorm 2020.2 EAP (more…)…
PhpStorm 2019.3.3 Preview
We’ve just rolled out a preview for the third minor update for PhpStorm 2019.3. Please give the PhpStorm 2019.3.3 Preview build 193.6494.5 a try and share your feedback with us. (more…)…
How to Upgrade to PHP 7.4 with PhpStorm
Check out our fresh video with a few nice tips and tricks on how to update your codebase to PHP 7.4. It combines materials from the PhpStorm 2019.2 and PhpStorm 2019.3 releases, with a few cool bonuses to boot. Nikita drew our attention to the fact that setters are slower than assigning prop…