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:

  1. 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:

  1. Download PHP_CodeSniffer_Standards_WordPress-0.0.1.tgz
  2. Unpack
  3. Copy PHP_CodeSniffer_Standards_WordPress-0.0.1/WordPress into [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.

5 Comments

  1. Showfom :

    Apr 24, 2009 9:11 am | Reply

    Any version for Windows?

    • John( author ) :

      Apr 24, 2009 2:45 pm | Reply

      It should work for any platform that supports PEAR, which includes Windows - the code is all in PHP.

  2. Keith Casey :

    May 7, 2009 4:24 pm | Reply

    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 ;)

  3. Ken Guest :

    May 8, 2009 6:42 pm | Reply

    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

    • John( author ) :

      May 9, 2009 10:23 am | Reply

      Very true. Instructions updated

Pings & Trackbacks

ma.tt, scott.jarkoff.com, devezine.com,

Leave a Reply


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Home | Main content | Software | Terms & Conditions | Sitemap | John Godley © 2010