1

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?

Brian Kendig
  • 2,452
  • 2
  • 26
  • 36
  • Whoever downvoted me, please at least let me know what's wrong with the question? – Brian Kendig Mar 03 '14 at 19:20
  • I see other posts saying that Xdebug *can* be used for this, but I don't know how to make Xdebug start collecting code coverage data (http://xdebug.org/docs/code_coverage says this is controlled by debug.coverage_enable, which already defaults to on), or after it's collected some data, how to generate the nice HTML reports from it. – Brian Kendig Mar 03 '14 at 20:51
  • PHPUnit is generating those reports today, similar to Sonar and other tools. I believe XDebug simply gives you the coverage stats, not the reports. You would have to read through the XDebug manuals for this. – Steven Scott Mar 04 '14 at 23:16

1 Answers1

2

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.

Brian Kendig
  • 2,452
  • 2
  • 26
  • 36
  • I would like to do the same: Record all function calls / all code execution of our live application in a month and create a report that contains how often the function was called. It would be great if all http request could be recorded to replay the usage. Did you find a solution? – surfmuggle Jul 13 '19 at 05:48