In a software development project it is important that the team adheres to a single coding standard. A common coding standard lets you focus on solving problems that matter instead of wasting time understanding code that was written using a different style.
PHP_CodeSniffer is the tool of choice to detect violations of code formatting standards. It ships with hundreds of sniffs that each check for one particular code property. You can define your own rule set by picking and choosing the sniffs you need or use one of the built-in rule sets such as PEAR or Zend.
Example 4-5 shows an excerpt of the build/phpcs.xml configuration file for PHP_CodeSniffer that is used in the example project. In this excerpt, we define a coding standard that demands the opening curly brace of a class, function, or method to be on the next line, that disallows the usage of tabs to indent scopes, and that ensures proper indenting of scopes.
Example 4-5. build/phpcs.xml configuration file for PHP_CodeSniffer
<?xml version="1.0"?> <ruleset name="MyRuleset"> <description>My rule set for PHP_CodeSniffer</description> <rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/> <rule ref="Generic.WhiteSpace.DisallowTabIndent"/> <rule ref="Generic.WhiteSpace.ScopeIndent"/> </ruleset>
Example 4-6 shows how to invoke PHP_CodeSniffer from our Apache Ant build script.
Example 4-6. The phpcs build target for PHP_CodeSniffer
<target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer"> <exec executable="phpcs" output="/dev/null"> <arg value="--report=checkstyle" /> <arg value="--report-file=${basedir}/build/logs/checkstyle.xml" /> <arg value="--standard=${basedir}/build/phpcs.xml" /> <arg path="${basedir}/src" /> </exec> </target>
Figure 4-6 shows how to configure a post-build action in our Jenkins job to publish the results of the analysis performed by PHP_CodeSniffer.
The PHP Mess Detector allows the definition of rules that operate on the raw data collected by PHP_Depend. Its focus is therefore not on the detection of code formatting violations but rather on issues such as possible bugs, hard-to-maintain code, unused parameters and variables as well as unused methods. It ships with about 30 rules that each check for one particular code property. You can define your own rule set by picking and choosing the rules you need or select one or more of the built-in rule sets.
Example 4-7 shows an
excerpt of the build/phpmd.xml
configuration file for PHPMD that is used in the example project. In this
excerpt, we define a coding standard that enforces thresholds for the
Cyclomatic Complexity and NPath Complexity software metrics, prohibits the
usage of the eval
, exit
, and goto
constructs of the PHP programming language,
and that reports unused arguments and variables as well as unused private
attributes and private methods.
Example 4-7. build/phpmd.xml configuration file for PHPMD
<?xml version="1.0"?> <ruleset name="MyRuleset" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> <description>My rule set for PHPMD</description> <rule ref="rulesets/codesize.xml/CyclomaticComplexity" /> <rule ref="rulesets/codesize.xml/NPathComplexity" /> <rule ref="rulesets/design.xml/EvalExpression" /> <rule ref="rulesets/design.xml/ExitExpression" /> <rule ref="rulesets/design.xml/GotoStatement" /> <rule ref="rulesets/unusedcode.xml/UnusedFormalParameter" /> <rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" /> <rule ref="rulesets/unusedcode.xml/UnusedPrivateField" /> <rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod" /> </ruleset>
Example 4-8 shows how to invoke PHPMD from our Apache Ant build script.
Example 4-8. The phpmd build target for PHPMD
<target name="phpmd" description="Perform project mess detection using PHPMD"> <exec executable="phpmd"> <arg path="${basedir}/src" /> <arg value="xml" /> <arg value="${basedir}/build/phpmd.xml" /> <arg value="--reportfile" /> <arg value="${basedir}/build/logs/pmd.xml" /> </exec> </target>
Figure 4-7 shows how to configure a post-build action in our Jenkins job to publish the results of the analysis performed by PHPMD. Figure 4-8 shows an example of what this report looks like.
Figure 4-9 shows how to configure a post-build action in our Jenkins job to publish a combined report of the violations found by PHP_CodeSniffer, PHP Copy/Paste Detector, and PHPMD.