When a project gets bigger, specially when using a framework, it’s mandatory to have some processes that help you in removing code that is not used anymore such as models, classes, template files, etc.
That is not an easy task, however the benefits or removing non used coded are tons (code coverage, development speed, bugs, etc.). But what’s the best approach to detect that a template file is not used anymore in your PHP project?
We discuss about that at Emagister with Eber Herrera and we get to two different approaches that might help you.
Code analysis approach
For each type of object you want to remove, you can create a command to do it. However, the main algorithm is:
- Get a list of items to check (list of models, view helpers, etc.): Symfony Finder Component can help you
- Iterate on your code and grep it or parse it: PHP Parser can do the job
- Report the object that is candidate to be removed
- Manual review, remove and feel like a star!
It’s possible that every type of object needs a different strategy. However, this process can be applied in most cases. For example, ViewHelpers in Zend Framework that are not called, TPLs that are not included, etc.
What’s the problem? Some objects usage depends on an application request so you should check your access logs. But there’s another approach.
Opcode cached files approach
And there’s the tricky one! If you have played a bit with opcache (if not you should because it’s faster than APC), you will see that is easy to get a list of all the files cache by the opcache.
At Emagister, we have developed a command, that connects to our opcache, gets the cached files, stores that info in a redis and the computes the difference with the files in our project. We run this process frequently.
Benefits are that it does not affect the performance, it’s base on real usage of your site (so let the command work and check from time to time the differences) and it affects any object because it checks the file usage, not the objects contained in it, so no parsing needed.
And remember children! Keep your clean code. You will be a happier developer and you’ll make others happier.