Code sniffs for CakePHP and then some more

This started out sometime early this year as an exercise to create a set of Code Sniffs which could be applied to our CakePHP projects...

Ummm... I guess, at this point I should rewind a bit and give a couple of background definitions and some more..

Initially I had thought that I will just use one of the pre-packaged sniffs but the first difficulty that I ran into was with $anything->something - in CakePHP if the "something" is an object then it begins with an uppercase alphabet but a variable or method name begins with a lowercase alphabet, thus $anything->Something == Object. Tinkering around the code a bit made it apparent that I had to have my own set of Cake sniffs to manage this but a separate standard just for this seemed an over kill and the simplicity of code made it kind of fun to add more standards which I liked but were in different set of sniffs

So I added the disallow short open tags sniff, then the Uppercase Constants sniff and soon the list was

PHP:
  1. public function getIncludedSniffs()
  2.     {
  3.         return array(
  4.                 'Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php',
  5.         'Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php',
  6.                 'Generic/Sniffs/WhiteSpace/',
  7.         'Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php',
  8.                 'PEAR/Sniffs/Classes/ClassDeclarationSniff.php',
  9.                 'PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php',
  10.                 'PEAR/Sniffs/Files/LineEndingsSniff.php',
  11.                 'PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php',
  12.                 'PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php',
  13.                 'PEAR/Sniffs/Functions/ValidDefaultValueSniff.php',
  14.                 'PEAR/Sniffs/Commenting/',
  15.         'PEAR/Sniffs/NamingConventions/ValidClassNameSniff.php',
  16.                 'Squiz/Sniffs/Functions/GlobalFunctionSniff.php',
  17.         'Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php',
  18.         'Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php',
  19.         'Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php',
  20.         'Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php',
  21.         'Squiz/Sniffs/Commenting/VariableCommentSniff.php',
  22.         'Zend/Sniffs/Files/ClosingTagSniff.php'
  23.                );
  24.     }//end getIncludedSniffs()

The only sniff I was missing was something which counts the number of lines in a function and restricts them to 45. Why 45? because those many lines fit in one screen full of Eclipse editor in the layout we commonly use... The end result was FunctionLineCountSniff.php

And the tyrant that I am I put in a SVN pre-commit hook (also supplied in the package) into our local SVN setup - so now if any of the developer misses/truants out on any of the standards they get an angry red output in their Eclipse console, something like

svn: 'pre-commit' hook failed with error output:
FILE: trunk/app/controllers/clients_controller.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AND 0 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 71 | ERROR | Missing function doc comment
--------------------------------------------------------------------------------

There were a few cribs initially but soon everyone got used to it and we now have code which looks uniformly pretty - Code pretty? Oh! well I will leave that for some other time.... right now I am working on a version of Bake which bakes using most of these standards - will save a lot of work.

Meanwhile if anyone wants to use these code sniffs feel free to download them here


About this entry