Setting up Custom Coding Standards with PHP CodeSniffer

PHP CodeSniffer provides you with some useful coding standards to get you going.

This is nice, but how do you customize what's provided or set up your own standards?

There is a tutorial on how to set up your own standards, but that was too much work for me at the moment.

However, that is NOT the way to set up your own standards, in my opinion.

Instead, read this post on how to extend a standard which is similar to your standard, and then include or exclude sniffs as needed :

http://www.kingkludge.net/2009/02/codesniffer-part-3-writing-an-example-...

Here's the short way to do this :

On my Ubuntu system, go to where the standards are located in /usr/share/php/PHP/CodeSniffer/Standards. If you want a sneak peek at what we're doing, look at the Standards/Zend directory, which extend on the PEAR standards.

In CodeSniffer/Standards, make a new directory MyCompany. In the MyStd directory, do a 'cp ../Zend/ZendCodingStandard.php MyCompanyCodingStandard.php'.

Now edit MyCompanyCodingStandard.php and replace 'Zend' with 'MyCompany' and tidy up the comments. Also include the sniffs you want, and exclude those you don't.

This is what the meat of MyCompanyCodingStandards.php looked like after my edits :



Now many of those sniffs may be perfectly OK, but on the code base I was working with they produced too much noise, so I just excluded them for now. As the code base improves, I hope to add some of them back in. There also may be a mutually exclusive conflict with the K&R function brace and mixing PEAR and Squiz, but hey, this is a work in progress!

Categories: