Quality Assurance Tools for PHP
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> <target name="prepare"> <mkdir dir="${basedir}/build/logs"/> </target> <target name="phpcs"> <exec dir="${basedir}" executable="phpcs" output="${basedir}/build/logs/checkstyle.xml" failonerror="false"> <arg line="--report=checkstyle ."/> </exec> </target> <target name="phpmd"> <exec dir="${basedir}" executable="phpmd" failonerror="false"> <arg line=". xml codesize --reportfile ${basedir}/build/logs/pmd.xml"/> </exec> </target> <target name="phpcpd"> <exec dir="${basedir}" executable="phpcpd" failonerror="false"> <arg line="--log-pmd=${basedir}/build/logs/pmd-cpd.xml ."/> </exec> </target> <target name="pdepend"> <exec dir="${basedir}" executable="pdepend" failonerror="false"> <arg line="--jdepend-xml=${basedir}/build/logs/jdepend.xml ."/> </exec> </target> <target name="phpunit"> <exec dir="${basedir}" executable="phpunit" failonerror="true"> <arg line="--log-xml ${basedir}/build/logs/junit.xml --coverage-clover ${basedir}/build/logs/clover.xml MoneyTest"/> </exec> </target> <target name="build" depends="clean,prepare,phpcs,phpmd,phpcpd,pdepend,phpunit"/> </project>
The main target, build, depends on the targets that
- Delete the
builddirectory, if it exists. - Prepare the
builddirectory, if it does not exist. - Run PHP_CodeSniffer on the project's sourcecode and write a logfile in Checkstyle XML format.
- Run phpmd on the project's sourcecode and write a logfile in PMD XML format.
- Run phpcpd on the project's sourcecode and write a logfile in PMD-CPD XML format.
- Run PHP_Depend on the project's sourcecode and write a logfile in JDepend XML format.
- 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 clean: [delete] Deleting directory /home/sb/Money/build prepare: [mkdir] Created dir: /home/sb/Money/build/logs phpcs: phpmd: phpcpd: [exec] phpcpd 1.1.0 by Sebastian Bergmann. [exec] [exec] 0.00% duplicated lines out of 722 total lines of code. pdepend: [exec] PHP_Depend 0.9.4 by Manuel Pichler [exec] [exec] Executing Dependency-Analyzer: [exec] 16 [exec] [exec] Generating pdepend log files, this may take a moment. phpunit: [exec] PHPUnit 3.4.0 by Sebastian Bergmann. [exec] [exec] ...................... [exec] [exec] Time: 0 seconds [exec] [exec] OK (22 tests, 34 assertions) [exec] [exec] Writing code coverage data to XML file, this may take a moment. build: BUILD SUCCESSFUL 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.
15/03/2009 at 09:59 Permalink
Can you talk a bit more about the differences between sonar and phpuc?
best regards, stefan
Reply
15/03/2009 at 10:39 Permalink
Reply
15/03/2009 at 10:18 Permalink
Reply
15/03/2009 at 10:40 Permalink
Reply
15/03/2009 at 11:50 Permalink
Reply
15/03/2009 at 14:16 Permalink
Reply
15/03/2009 at 18:41 Permalink
Reply
15/03/2009 at 20:04 Permalink
When you talk about QA tools in PHP projects, then I see Selenium as one. Since you not included it in your list of tools, I would like to know why?
Reply
15/03/2009 at 22:33 Permalink
Reply
15/03/2009 at 21:44 Permalink
Reply
15/03/2009 at 22:32 Permalink
Reply
16/03/2009 at 23:50 Permalink
Reply
18/03/2009 at 07:34 Permalink
Reply
17/03/2009 at 07:39 Permalink
Reply
17/03/2009 at 10:17 Permalink
Reply
17/03/2009 at 10:20 Permalink
Reply
18/03/2009 at 13:12 Permalink
Reply
17/03/2009 at 13:28 Permalink
- a custom version of PHP that is statically typed, performs various other checks, and picks up on possible XSS vulnerabilities
- a custom lint-like tool ('Code quality checker').
to enforce our coding standards.
We don't develop these tools for other people really (they're tailored to our own needs), but anybody interested can see our approach and get the tools/code from http://ocportal.com/docs/codebook.pdf
We're pretty o.c. about things, and frankly some of our coding standards would drive other coders insane - but I find our approach works very well and let's us catch quite a few classes of bugs early on.
Reply
04/11/2009 at 19:33 Permalink
I´m using pdepend, but i don´t know what is the meaning of jdepend graph, i´ve try to know in the users@pdepend.org list whitout any result. Any idea?
Thanks a lot.
Best regards.
Reply
05/11/2009 at 10:47 Permalink
I´ve found the requested information in Manuel Pichler´s blog, but i can´t write the links in the comment.
Best regards.
Reply
12/05/2010 at 07:09 Permalink
Does anybody had problems with phpcpd? I got all above tools working great together with PHPUndercontrol, the only problem is phpcpd. Task is executed during build process, I got logs/pmd-cpd.xml report, but it is not shown by Cruisecontrol/PHPUndercontrol :(
Can you give me hint on what can be wrong ?
P.S. I'm using all latest version from PEAR, for example phpundercontrol is 0.5.1
Reply