Dead code detection in PHP

14,729

Solution 1

xdebug's code coverage tools allow you to test which lines of code are actually being executed, without needing to put trace statements in all of the functions/methods.

Example:

<?php
    xdebug_start_code_coverage();

    function a($a) {
        echo $a * 2.5;
    }

    function b($count) {
        for ($i = 0; $i < $count; $i++) {
            a($i + 0.17);
        }
    }

    b(6);
    b(10);

    var_dump(xdebug_get_code_coverage());  // array '/path/file.php' => array line_number => int 1 or 0.
?>  

Solution 2

It's a little late now, but PHPDCD claims to do this statically, which should give you a much more informative result than having to profile actual code execution with xprof / xdebug.

Solution 3

I don't know of a way to detect code that is completely unused, that may be beyond the abilities of all the tools out there. But in terms of the tools out there, hit https://phpqa.io/ for a good rundown of them.

  • So far, one of my favorites in phploc which tears apart your code from an Object Oriented perspective and gives you details on how many classes vs how many functions vs how many tests vs average loc per function vs Cyclomatic Complexity.

  • My next favorite is phpcpd which is the "PHP Copy-Paste Detector". It tokenizes your entire codebase, looks for common signatures, and gives you a list of files with line numbers.

  • There are lots of other tools on that page, choose the ones that are useful to you.

We're actively using these tools in web2project and in the two years since we forked from dotProject, we've dropped about 35% of the codebase from refactoring, eliminating duplication (originally 12%, now about 2.5%), and generally structuring things better. And that's counting our 15k+ lines of Unit Tests. :)

Solution 4

I would recommend running through the system with xdebug profiler (http://xdebug.org/docs/profiler).

Run through the system the view the logs with http://code.google.com/p/webgrind/ and physically see what's being called.

Solution 5

Regarding profiling tools, if you decide to go that way you may take a look at xhprof http://developers.facebook.com/xhprof/
It has smaller size of the output files and web interface that you could embed into your app for continuous tracking. It is able to generate visual representation of call tree. I recommend it over xdebug for that purpose.

Share:
14,729
Nikita Fedyashev
Author by

Nikita Fedyashev

Updated on June 03, 2022

Comments

  • Nikita Fedyashev
    Nikita Fedyashev about 2 years

    I have a project with very messy code - lots of duplication and dead code here and there.

    Some time ago there was zero code coverage by unit-tests but now we're trying to write all new code in T.D.D. manner and lowering technical debt by covering "old" code by unit-tests as well(test-last technique).

    Business logic's complexity is quite high and sometimes no one can answer whether some methods are used or not.

    How this dead code methods can be found? Extensive logging? Higher test coverage?(It is not very easy because customers want new features to come out)

  • Nikita Fedyashev
    Nikita Fedyashev over 14 years
    Ben, can I use it for code which is not covered by unit-tests?
  • Ben James
    Ben James over 14 years
    The 'code coverage' in the xdebug sense does not mean test coverage. The two are not related, so you can use this to see which lines are executed, whether or not they are tested.
  • Ether
    Ether over 14 years
    You can get data on code coverage during tests simply by enabling the code coverage data collection right before you run all your tests, and turn it off right after.
  • Edson Medina
    Edson Medina almost 11 years
    It works statically, so it doesn't work for anonymous stuff like variable methods ($object->$method) or call_user_func(). It helps but it's far from perfect.
  • El Yobo
    El Yobo almost 11 years
    Yeah, no static tool will ever be able to handle those reliably unfortunately, as the actual behaviour is only determined at runtime.
  • Cees Timmerman
    Cees Timmerman almost 7 years
    phpdcd.phar --exclude vendor --recursive . works for me.
  • Andy Preston
    Andy Preston about 3 years
    This project is no longer maintained and its repository is only kept for archival purposes.