Tombstone is the dead code detection library for PHP
In our real life most of the time we use only 20 percentage of any application frequently
So using Tombstone library (GitHub link: scheb/tombstone) we will check how to remove zero times or rarely called functions
As a result our codebase will get slimmer and healthier by removing unwanted functions
Leaving dead code in project makes it much harder to find the bugs
Using IDE’s find usage we can easily identify whether the function is called or not
But if function a() called inside function b() which is called inside function c() and function c() is nowhere used it’s difficult to identify these kind of issues
So for these kind of issue, dead code deduction library will be used as handy tool
Tombstoning is a basic step by placing a marker code inside our code block where you think the code is dead
Once you placed the marker code wait for a while and allow users to use your application
After that you will get a log of all called functions so these called functions are still in use
Hope you understand the theory part of how tombstoning process works
Lets we see it in practical way by using the library scheb/tombstone
First import vendor package scheb/tombstone in your codebase using composer
Create the tombstone helper as like the given below gist if you have doubt please refer the sample github toy project linked at the last
The class Tombstone helper has three main components public property $logPath to assign the log path, constructor which is used to add the stream handling for our log file and finally we created a public method tombstone() to do the tombstoning process
We created helper and now we are going to use tombstone method in our code.
Please refer the given below gist
Here in class DeadCode we have two method called() and notCalled() with tombstone marker
As you noted in our tombstone marker method we passed two arguments first one is marker created date and second one is who created the marker
We call our DeadCode::called() method but not the DeadCode::notCalled() (don’t panic we didn’t print anything so the index.php page is remain blank)
So after few times of execution of index page and when we see our log we found that DeadCode class method notCalled() wasn’t logged
The log is appended in tombstones.log file you can easily parse what each line of log says: when the log was logged with tombstone date and who called in which file at which line number in which function and in which function it was invoked
So the method DeadCode::notCalled() is the safe candidate to remove from our code because that method is called nowhere
Please find the simple sample for dead code detection (GitHub link: deadcode detection)