CodeSniffer is a PEAR package that checks PHP code for violations of coding standards. According to the developers:
It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.
While it won’t make you bug-free, it is another tool to help you produce better quality code, and is very useful when working with a team of developers. CodeSniffer thoughtfully comes with several standards to verify code against the PEAR, Zend, and Squiz coding standards. What follows is a CodeSniffer standard that adheres to the WordPress Coding Standard. Hurray for coding violations!
The WordPress Coding Standard will check for:
- Brace usage
- Spaces around logic statements, assignments, functions, and classes
- No CamelCase function names
- Deprecated WordPress functions usage, with suggested alternatives
- Use of posix regular expressions
- Filenames with an underscore
Installation
Details about how to install CodeSniffer can be found at the CodeSniffer site. The WordPress Coding Standard can be installed by following these steps:
- Download PHP_CodeSniffer_Standards_WordPress-0.0.1.tgz
- Install using
pear install PHP_CodeSniffer_Standards_WordPress-0.0.1.tgz
Alternatively if you do not have access to the pear command you can install it manually:
- Download PHP_CodeSniffer_Standards_WordPress-0.0.1.tgz
- Unpack
- Copy
PHP_CodeSniffer_Standards_WordPress-0.0.1/WordPressinto[YOUR_PEAR_DIRECTORY]/PHP/CodeSniffer/Standards
Usage
Full details of using CodeSniffer can be found at the CodeSniffer site. To use the WordPress Coding Standard you can specify it as an argument:
phpcs --standard=WordPress your-file.php
Alternatively you can set the WordPress Coding standard to be your default:
phpcs --config-set default_standard WordPress
Caveats
Please note that the WordPress standard is not foolproof and it can be very (very) pedantic. It is currently in a very early state and is being released in the hope that others may contribute.
Thanks to urbangiraffe
