Quality Assurance on PHP projects - PHP_CodeSniffer
Note: This article was originally published at Planet PHP
on 17 July 2011.
PHP_CodeSnifferA is 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 withA PHP_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 withA PHP_CodeSnifferA have 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.
InstallationInstallingA PHP_CodeSnifferA is easy when using theA PEARA framework. Make sure you have installed and upgraded the pear libraries that come with your OS. After that all you need to do asA rootA orA AdministratorA is the following.
user@server: $ pear install PHP_CodeSniffer
Or you can go to theA download pageA ofA PHP_CodeSnifferA and download the source package yourself and install it the way you want it. In most cases, theA PEARA installation 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 byA PHP_CodeSnifferA are the following:
- Zend
- PEAR
- PHPCS
- Squiz
- MySource
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 ofA PHP_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)


