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.