Tombstone

  1. Tombstone is the dead code detection library for PHP
  2. In our real life most of the time we use only 20 percentage of any application frequently
  3. So using Tombstone library (GitHub link: scheb/tombstone) we will check how to remove zero times or rarely called functions
  4. As a result our codebase will get slimmer and healthier by removing unwanted functions
  5. Leaving dead code in project makes it much harder to find the bugs
  6. Using IDE’s find usage we can easily identify whether the function is called or not
  7. 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
  8. So for these kind of issue, dead code deduction library will be used as handy tool
  9. Tombstoning is a basic step by placing a marker code inside our code block where you think the code is dead
  10. Once you placed the marker code wait for a while and allow users to use your application
  11. After that you will get a log of all called functions so these called functions are still in use
  12. Hope you understand the theory part of how tombstoning process works
  13. Lets we see it in practical way by using the library scheb/tombstone
  14. First import vendor package scheb/tombstone in your codebase using composer
  15. 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
  16. 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
  17. We created helper and now we are going to use tombstone method in our code.
  18. Please refer the given below gist
  19. Here in class DeadCode we have two method called() and notCalled() with tombstone marker
  20. 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
  21. 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)
  22. 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
  23. 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
  24. So the method DeadCode::notCalled() is the safe candidate to remove from our code because that method is called nowhere
  25. Please find the simple sample for dead code detection (GitHub link: deadcode detection)