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

  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 Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

12 Responses to Code sniffs for CakePHP and then some more

  1. Daniel Hofstetter July 4, 2008 at 9:03 pm #

    It would be cool if the cake team would also use those code sniffs ;-)

  2. Tarique Sani July 5, 2008 at 9:11 am #

    I will be happy enough if bake output was phpDoc commented and bake allowed multiple rules of validations but I guess incorporating patches into bake is a low priority right now…

  3. Tim Koschuetzki July 9, 2008 at 3:21 am #

    We are considering to implement this for the Cake Testsuite maybe. :)

  4. Tarique Sani July 9, 2008 at 10:02 am #

    @Tim – that indeed would be a welcome addition!

  5. nate July 11, 2008 at 11:59 pm #

    Hey, couple things:

    (1) I started on a set of sniffs a few weeks ago but never finished, so I might just steal yours. ;-)

    (2) There was a ticket on adding docblock-generation to Bake, but someone must have shot it down, because I don’t see it in there. Anyway, if you have a patch against the current codebase, attach it to a ticket and assign it to me, I’ll get it in there.

    Validation though… not so much. :-P I’d rather keep Bake as light and focused as possible, and that’s something that’s simple enough to do without stepping through command prompts.

  6. Tarique Sani July 12, 2008 at 9:50 am #

    Hi Nate, Life would be easier for me if Cake itself had a set of code sniffs – so I should say thanks :) Also I will be happy to make any modifications to the sniffs if needed.

    I had seen the last docblock patch and had not liked it too much – will definitely post a ticket once I have something ready.

  7. Amit Badkas July 12, 2008 at 10:21 am #

    @nate – The ticket in which patch included to bake with phpDoc comments is #4276

  8. nate July 16, 2008 at 6:17 pm #

    Hey Tarique, I’ll let you know about the patches as soon as I can. I’d like to do another release in the next week or two, so if you can work up a patch for Bake by then, that’d be great. If not, no worries. Thanks.

  9. Rexibit Web Services July 22, 2008 at 7:06 pm #

    Interesting. I am going to have to look into Cake. It sounds fun.

Trackbacks/Pingbacks

  1. PHP CodeSniffer for CakePHP built over sanisoft/cakephp_codesniffs « Flashes of Insight - August 8, 2010

    [...] for CakePHP is available from the PEAR site.  As we googled it around for a while, we landed on sanisoft’s CakePHP CodeSniffer which helped us to start up with [...]

  2. Yet another version of PHP CodeSniffer for CakePHP « Flashes of Insight - August 9, 2010

    [...] a version PHP_CodeSniffer for CakePHP on my github by adding some more sniffs to the existing sanisoft’s CakePHP CodeSniffer based on the CakePHP standards.  This upgraded version basically focuses on the introducing the [...]

  3. CakePHP Code Sniff Updates at SANIsoft - September 12, 2011

    [...] Code Sniff UpdatesBy Rita Chouhan on September 12, 2011 in CakePHP, Open Source, PHP TweetIn an earlier post, Dr.Sani discussed about CodeSniffer and sniffs for CakePHP Standard. In that we saw how to define [...]

Leave a Reply