More QA with CruiseControl and PHP_CodeSniffer
I am using CruiseControl for some time now, to run all Unit Tests for a project continuous after each commit. Since Sebastian Bergmann started the development of PHPUnit 3.2 he added support for software metrics and PMD to PHPUnit, that give you further informations about the quality of your source code. These two features are really well integrated into CruiseControl, the only thing you need to do is to comment out the following lines in the main.jsp file.
<%--
<cruisecontrol:tab name="pmd" label="PMD">
<%@ include file="pmd.jsp" %>
</cruisecontrol:tab>
--%>
The next thing that is important for each project is the coding standard of a project, a company or at least a single person. Java has a nice tool called Checkstyle that checks all your code against a defined set of rules. I knew this tool really well from my diploma thesis, because I used Checkstyle to check the complete code against the Sun coding standards. Checkstyle is also really good integrated into CruiseControl, so that it checks on each build that your code meets the coding standards.
So what can a PHP developer do, to achieve the same support? This question is answered fast! There is a nice PEAR package that does nearly the same job as Checkstyle. The name of this package is PHP_CodeSniffer. This PEAR package provides a variety of pre defined coding standards like PEAR, ZEND etc., a small cli script phpcs to run PHP_CodeSniffer against your code and as best it provides an XML output generator. This output is not compatible with checkstyle but it is simple to transform into the Checkstyle format that is supported by CruiseControl. You can use the following stylesheet to transform the PHP_CodeSniffer output for your CruiseControl installation:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" />
<xsl:template match="/">
<checkstyle>
<xsl:apply-templates select="phpcs/*" />
</checkstyle>
</xsl:template>
<xsl:template match="file">
<file>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:for-each select="child::*">
<xsl:call-template name="entry" />
</xsl:for-each>
</file>
</xsl:template>
<xsl:template name="entry">
<error>
<xsl:attribute name="line">
<xsl:value-of select="@line" />
</xsl:attribute>
<xsl:attribute name="severity">
<xsl:value-of select="local-name()" />
</xsl:attribute>
<xsl:attribute name="message">
<xsl:value-of select="text()" />
</xsl:attribute>
</error>
</xsl:template>
</xsl:stylesheet>
You have to add the following lines to your ant build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="mapi-xplib-de" default="build" basedir=".">
...
<target name="checkstyle">
<exec executable="phpcs"
output="${basedir}/build/logs/codesniffer.pcs"
dir="${basedir}/source">
<arg line="--report=xml
--standard=MapiSource
--ignore=src/autoload src/" />
</exec>
<exec executable="xsltproc"
output="${basedir}/build/logs/checkstyle.xml"
dir="${basedir}/build/logs">
<arg line="${checkstyle.xslt} ${basedir}/build/logs/codesniffer.pcs" />
</exec>
</target>
<target name="build" depends="checkout,checkstyle,test" />
...
</project>
PLEASE NOTE I know that ant supports xsl transformation without external exec commands but it doesn't work with my current gcj installation. :-)
Tags:
- checkstyle
- continuous integration
- cruisecontrol
- phpundercontrol
- php
- php_codesniffer
- quality assurance
Categories:
Published: October 27, 2007