Tuesday, 28 October 2008

PHP_CodeSniffer == CSS_CodeSniffer?

Earlier this year, I added support for tokenizing JavaScript files to PHP_CodeSniffer so developers could ensure their JS files also conform to their coding standards. With new products like the MySource Mini using such rich JS interfaces, PHP developers are having to move away from traditional server-side coding improve their client-side coding skills. Having JS support in PHP_CodeSniffer was a move to recognise this and we are now using it very successfully within Squiz to check for formatting and performance errors.

The next natural step was to add support for checking CSS files. These same PHP developers are having to write large amounts of CSS these days and, in my experience, CSS files tend to be fairly messy when a large number of developers are working on them. To counter this, I've added a basic CSS tokenizer to PHP_CodeSniffer and the Squiz coding standard now enforces formatting standards such as indentation and spacing around colons.

Sniffs can be written to improve the performance of both PHP and JS code and the same is true for CSS files, although it works a little differently. One of the sniffs in the Squiz standard ensures that shorthand notation is used to define CSS colours where possible (e.g., #F0F instead of #FF00FF). This helps to ensure developers focus on keeping the size of CSS files down, even though we will certainly minimise the size before deployment. I think PHP_CodeSniffer helps to remind developers of these performance issues each time they run the checker, which can only be a good thing.

I've committed the CSS tokenizing code to CVS, so you can grab it directly from there or wait until the 1.2.0a1 release in the next few weeks.

4 comments:

christer said...

This is brilliant! :)

I sent you a mail about a tokenizer for Smarty templates some time ago... I would be happy to try and implement that for the codesniffer package.

Greg Sherwood said...

Thanks christer. Yes, I remember your email. It would be great to have a Smarty tokenizer in PHP_CodeSniffer, but I don't use Smarty so I don't know what sniffs would be worthwhile for developers who use it.

If you decide to give it a go, I'd be happy to help where I can. Just shoot me an email.

Anonymous said...

I discovered PHP_CodeSniffer about 6 months ago and have been very impressed. I use it in precommit hooks for our cvs repository to make sure all checked in files adhere to our coding standards. Are there any places on the web to get additional sniffs other than what's included in the release?

Greg Sherwood said...

I commit back all the sniffs contributed to me as they are usually generic sniffs that other developers may want to use. I know of a few other developers and companies writing their own sniffs, but they are doing so internally and I'm not sure if they intend to release them for public use.

So for now, the included sniffs are pretty much all there is for general use.