Wednesday 21 March 2007

Easier custom standards with PHP_CodeSniffer

I committed a fairly large change to PHP_CodeSniffer today that allows you to create custom coding standards without having to write your own sniffs.

The MySource 4.0 team needed a new code sniff to check that we were including files correctly, but the sniff was very specific to the product, so I could not include it in the Squiz coding standard. I needed the team to use a new MySource4 coding standard to check their code, but I wanted it to be the Squiz coding standard with a few more sniffs.

The only way to do this previously was to create empty sniffs files for every Squiz sniff that I wanted to include. This meant a lot of directories and a lot of maintenance as I would have to update the MySource4 standard every time I added or removed sniffs from the Squiz standard.

So I decided to give coding standards their own management class and allow them to override a method in there to tell PHP_CodeSniffer which sniffs, directories of sniffs, or whole coding standards they would like to include in addition to their own. This not only allowed me to create the MySource4 standard easily, but also allowed me to remove some empty sniff files from both the Squiz and PEAR coding standards.

I haven't update the end user docs for PHP_CodeSniffer yet, but I will get around to it before the next release. All existing coding standards will need this management class, so you'll need to be aware of this if you do have your own custom standard.