Quality Assurance Tools for PHP

Map of PHP QA Tools

The map above contains the following tools that are useful for quality assurance in PHP projects:

  • PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
  • phpmd scans PHP source code and looks for potential problems such as possible bugs, suboptimal code or overcomplicated expressions.
  • phpcpd is a Copy/Paste Detector (CPD) for PHP code.
  • PHP_Depend is a PHP software metrics tool.
  • PHPUnit is the de-facto standard unit test framework for PHP.

Build Automation is the act of scripting or automating a wide variety of tasks that a software developer will do in their day-to-day activities, including tasks that involve the tools listed above.

Apache Ant is a Java-based build tool that is similar to make, but without make's wrinkles. Its build files are XML-based, calling out a target tree where various tasks get executed.

The listing below shows an Apache Ant build file for a project named Money.

<project name="Money" default="build">
 <target name="clean">
  <delete dir="${basedir}/build"/>

 <target name="prepare">
  <mkdir dir="${basedir}/build/logs"/>

 <target name="phpcs">
  <exec dir="${basedir}"
   <arg line="--report=checkstyle ."/>

 <target name="phpmd">
  <exec dir="${basedir}"
   <arg line=". xml codesize
              --reportfile ${basedir}/build/logs/pmd.xml"/>

 <target name="phpcpd">
  <exec dir="${basedir}"
   <arg line="--log-pmd=${basedir}/build/logs/pmd-cpd.xml ."/>

 <target name="pdepend">
  <exec dir="${basedir}"
   <arg line="--jdepend-xml=${basedir}/build/logs/jdepend.xml ."/>

 <target name="phpunit">
  <exec dir="${basedir}"
   <arg line="--log-xml         ${basedir}/build/logs/junit.xml
              --coverage-clover ${basedir}/build/logs/clover.xml

 <target name="build"

The main target, build, depends on the targets that

  1. Delete the build directory, if it exists.
  2. Prepare the build directory, if it does not exist.
  3. Run PHP_CodeSniffer on the project's sourcecode and write a logfile in Checkstyle XML format.
  4. Run phpmd on the project's sourcecode and write a logfile in PMD XML format.
  5. Run phpcpd on the project's sourcecode and write a logfile in PMD-CPD XML format.
  6. Run PHP_Depend on the project's sourcecode and write a logfile in JDepend XML format.
  7. Run the project's tests using PHPUnit and write logfiles in JUnit XML and Clover XML format.

Below is the output for invoking Apache Ant in the project directory:

sb@ubuntu Money % ant
Buildfile: build.xml

   [delete] Deleting directory /home/sb/Money/build

    [mkdir] Created dir: /home/sb/Money/build/logs



     [exec] phpcpd 1.1.0 by Sebastian Bergmann.
     [exec] 0.00% duplicated lines out of 722 total lines of code.

     [exec] PHP_Depend 0.9.4 by Manuel Pichler
     [exec] Executing Dependency-Analyzer:
     [exec]                                                                 16
     [exec] Generating pdepend log files, this may take a moment.

     [exec] PHPUnit 3.4.0 by Sebastian Bergmann.
     [exec] ......................
     [exec] Time: 0 seconds
     [exec] OK (22 tests, 34 assertions)
     [exec] Writing code coverage data to XML file, this may take a moment.


Total time: 4 seconds

The generated logfiles can be processed by CruiseControl because it already knows the XML formats used. phpUnderControl is a customization of CruiseControl that caters to the needs of PHP projects and makes a lot of things easier.

Sonar enables to collect, analyze and report metrics on source code. It not only offers consolidated reporting on and across projects throughout time, but it becomes the central place to manage code quality. The developers of Sonar are working on out-of-the-box support for PHP projects.