PHPStan and static analysis help us with detect unused private methods. I also made a package to handle unused public methods.
But we can do better. When we remove dead parts of our code, we sometime leak classes that are never used. But we still have to maintain them, upgrade them and test them.
Do you want to avoid spending time and money on something you don't use?
Almost 4 years ago we had legacy project that required PHP and Symfony upgrade. But instead of jumping right into an upgrade, we made experiment with dead-code detection.
How much was it? We had a talk in Czech on PHP meetup in Prague, where we shared more details. It's often more than people guess they have in a codebase they maintain for years.
Not to stretch your language skill, in the end, we've removed around ~20 % of the codebase. That's 20 % of code we we'd have to test, fix in static analysis, upgrade to newer Symfony or PHP and so on.
A leaking class requires our attention but we don't really need it:
Command
classes injected to console Application
There is also one special case, that is missed by both PHPStan and PhpStorm. Sometimes we test the class, so it looks used. Then we have to test it, because it is used - a class that is used only in tests can be also removed.
We've been using this principal on projects past 4 years and it's very simple in it's nature:
/app
or /src
directorySimple!
composer require tomasvotruba/class-leak --dev
The package is available on PHP 7.2+, as downgraded.
vendor/bin/class-leak check bin src config
Also remove tests and fake usages if needed, e.g. we've had some classes registered only in config/services.php
in Symfony projects, that were never used in the project source code itself.
In case you want to skip some classes, you can use --skip-type
option:
vendor/bin/class-leak check bin src packages --skip-type="Symplify\\EasyCI\\Twig\\Contract\\TwigTemplateAnalyzerInterface"
That's it. Add the job to your CI and let it spot the leaking classes for you. You'll never have to maintain leaking classes again!
Happy coding!
Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors.
I'd really appreciate it!