Xdebug, Phing and SpikePHP Coverage Tools

Stella Power

on

April 30, 2009

Xdebug, Phing and SpikePHP Coverage Tools

Recently I've been looking into various open source PHP development tools, focusing mainly on Xdebug, Phing and SpikePHP Coverage Tools. Xdebug is an open source package of debugging tools for PHP, and is often called the "swiss army knife tool'' for PHP developers. It provides a number of useful features such as debugging, tracing, profiling and code coverage. It works well on a variety of platforms and there's also third party extensions for it for Eclipse and Firefox. Figure 1. Xdebug - var_dump() One of the simple improvements that Xdebug makes to your PHP debugging experience is making the output of var_dump() easier to read. See the above figure for an example. Not only that, but it allows you to control the output via some configuration settings. For example, you can control the maximum depth to which arrays should be printed, the number of array elements or object properties to display and the displaying of long strings. Figure 2. Xdebug - stack trace A more significant improvement for me was the handling of error messages. I encountered an error message during the running of some of my unit tests, and Xdebug was able to provide a stack trace, listing the history of the PHP function calls that led to the error. My test code was simple enough that the problem could be easily found without using Xdebug, but for large, complicated and modular PHP applications, I imagine that this tool will be indispensable in determining where exactly the function is being called from. See the above figure for an example of the stack trace produced. Xdebug provides a profiling feature which can be enabled in the php.ini configuration file. It produces logs that are compatible with the cache time profiler, Cachegrind, which is part of the Valgrind Tool Suite. Cachegrind, combined with Callgrind (another Valgrind tool), can be used to produce reports analysing the data and display it in an easy to read graph. This information can then be used to determine which parts of the code need to be optimized in order to improve its performance. Xdebug also provides a statement code coverage statistics feature. This means that if there is a method with 100 lines of code, and only 75 of these lines are actually executed when tests are being run, then the method is considered to have a code coverage of 75 percent. Unfortunately, Xdebug does not provide any graphical output of the coverage statistics collected and there is no command line tool to analyse the data, which leads me to investigate the development tools, Phing and SpikePHP Coverage. Figure 3. Phing - coverage chart Phing is based on the Java build tool, Apache Ant and was created to avoid the need for a Java Runtime Environment for the automation of build processes on PHP development servers. The main advantage of phing over Ant is that you can extend phing by writing your own PHP classes. Phing allows us to define a fileset of all tests, run SimpleTests on each of these files, and gather and aggregate the collected coverage statistics. It also converts the coverage statistics to a nicely formatted HTML report using XSL, as shown in above. Figure 4. SpikePHP - summary report As an alternative to using Phing, I also briefly investigated the SpikePHP Coverage tool. Like Phing, it uses Xdebug to collect code coverage statistics and presents it in a HTML format to the user. Unfortunately it doesn't appear to provide any graphical information, but does provide line by line code coverage reports along with some summary statistics.

Share it!

PHPUnit produces good test coverage reports.
Folks may want to check out the post catch wrote about Profiling Drupal. It includes details about setting up Xdebug.