Question

When I run my PHPUnit tests, Xdebug generates a nice code coverage report which shows me exactly how many times each line of code was executed in each of my PHP files.

I want to get the same report for my web site under, say, a week of normal use, so that I can find lines of PHP on my site that might not be used any more. Rather than unit tests running my PHP code, it would be the web server, but I should be able to get the same report, right?

How do I set up Xdebug to collect data and generate a code coverage report on a live site?

Was it helpful?

Solution

I'll answer my own question. Xdebug provides a xdebug_start_code_coverage() call to start collecting code coverage information, and a xdebug_get_code_coverage() call to retrieve information about what code was covered. This is easily applicable to unit testing, because you're generally only concerned with what code was covered by the run of a sequence of tests in a controlled situation. (Turn on coverage, run the tests, then you've got the coverage results.) But for arbitrary hits on a web server application, you'd probably need to start code coverage in the preDispatch and then have the postDispatch write the stats to a database (or store them in some other way) so that later you could collate the results into a report. That's not handled by Xdebug.

Xdebug can collect profiling information in cachegrind format, so I'll see if I can use that to help find what code hasn't been called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top