Quality Assurance on PHP projects - PHP_CodeSniffer
Note: This article was originally published at Planet PHP on 18 July 2011.PHP_CodeSnifferAis probably the most convenient tool out there to analyze your source code and to verify it complies to company policies. Although it's debatable why source code should follow strict guidelines, it's only a matter of time before you discover yourself that it pays off to have a code base that appears to be written by one developer.
The first question you have to ask is what standard are you going to implement. There are several standards already packaged withAPHP_CodeSniffer, but are they useful within your company? Maybe you want to extend or override some standards with your own implementation. Do remember, the standards supplied withAPHP_CodeSnifferAhave been negotiated over and over by the developers for ages. So if you want to define your own standards, be warned that it can be a long and tedious track before you can agree on a specific standard.
InstallationInstallingAPHP_CodeSnifferAis easy when using theAPEARAframework. Make sure you have installed and upgraded the pear libraries that come with your OS. After that all you need to do asArootAorAAdministratorAis the following.
user@server: $ pear install PHP_CodeSniffer
Or you can go to theAdownload pageAofAPHP_CodeSnifferAand download the source package yourself and install it the way you want it. In most cases, theAPEARAinstallation is a more elegant, easy way to install the tool.
Configuration & ExecutionPHP_CodeSnifferdoesn't require much configuration, but you have to decide on which coding standard you want to check the code base.
Standards provided byAPHP_CodeSnifferAare the following:
But as stated, you can also define your own standard and provide the base path of the repository on command line
user@server: phpcs --standard=/path/to/my/standards /path/to/php/sources
There are a lot of extra options provided with this tool, but let me focus here on the more important ones you might find useful in your day-to-day usage ofAPHP_CodeSniffer.
Ignoring files and pathsIf you have a couple of external libraries or test scripts in your PHP projects, you might want to exclude them because they're not really part of your concerns. Wouldn't it be easy to just exclude them from the analysis? The following command will exclude paths you have no interest in.
user@server: phpcs --standard=PEAR --ignore=*/tests/*,*/library/Zend/*"/
Truncated by Planet PHP, read more at the original (another 3884 bytes)