WordPress CodeSniffer Standard
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:
pear install http://urbangiraffe.com/resources/
download/plugin/PHP_CodeSniffer_Standards_WordPress-0.0.1.tgz
NOTE: The above line has been split to fit the page - you should enter it as one line.
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.







6 Comments
Apr 24, 2009 9:11 am |
Any version for Windows?
Apr 24, 2009 2:45 pm |
It should work for any platform that supports PEAR, which includes Windows - the code is all in PHP.
May 7, 2009 4:24 pm |
Nice and congrats. I wish more of the frameworks (or major apps) would do this. It'd be a nice way to keep their community moving in roughly the same direction.
Of course, I'm biased as one of my colleagues developed a CodeSniffer spec for the Kohana framework - http://blueparabola.com/blog/coding-standard-analysis-using-phpcodesniffer
May 8, 2009 6:42 pm |
why download the package and then install using PEAR?
Here's the oneliner:
$sudo pear install http://urbangiraffe.com/resources/download/plugin/PHP_CodeSniffer_Standards_WordPress-0.0.1.tgz
May 9, 2009 10:23 am |
Very true. Instructions updated
Jun 12, 2010 11:40 am |
On linux be sure the directory is not called WordPress but Wordpress (the lowercase `p`).
Pings & Trackbacks
ma.tt, scott.jarkoff.com, devezine.com,
Leave a Reply