PHP CodeSniffer and common errors

by rp

sniffer11aI am currently writing a wrapper for PHPUnit for my employer which makes writing Selenium test cases for developers far easier. It would abstract them from the details of knowing server, urls, browser versions, reporting etc. All they now need to do is just write a basic test case for the functionality they added in the sprint and then potentially this can be added into hudson for continuous integration. This was interesting and requires a blog post to document the classes I extended. However this post is more about the next steps. This also would be a great help for the testing team to amass regression suite written by the developer itself, which if done properly would be of the highest quality.

So the next step was to make a pear package out of it so that other projects could extend out of it and also to free the developer of the include path hell that php offers and as part of the exercise, I ran my code past php code sniffer. However good the tool is, its lacking on part of documentation (or atleast i couldn’t find much) and its obscure error messages only meant that I would have to dig into the code to find out what exactly it was looking for. So here are some of them of the obscure ones:

--------------------------------------------------------------------------------
FOUND 2 ERROR(S) AND 0 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 2 | ERROR | Missing file doc comment
 2 | ERROR | Missing class doc comment
--------------------------------------------------------------------------------

There isn’t any documentation that points out what it actually means with file document and class document. It turns out that it needs documentation about the class as well as the class, usually the same if there is only one level. Here is what I put in my class file to satisfy that.

/**
* IPC_Test_Config
*
*
* @category Testing
* @package  IPC.Test
* @author   Rajat Pandit <rajat_pandit@lalaland.com>
* @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link     http://ipcmedia.com
*/
/**
* IPC_Test_Config
*
* @category Testing
* @package  IPC.Test
* @author   Rajat Pandit <rajat_pandit@lalaland.com>
* @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link     http://ipcmedia.com
*/

Here’s the next one:

--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 10 | WARNING | PHP version not specified
--------------------------------------------------------------------------------

Now typically you would expect another @tag but its just * PHP version 5 that you need to add. This is what the final version looks like:

/**
* IPC_Test_Config
*
* PHP version 5
*
* @category Testing
* @package  IPC.Test
* @author   Rajat Pandit <rajat_pandit@ipcmedia.com>
* @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link     http://ipcmedia.com
*/
/**
* IPC_Test_Config
*
* @category Testing
* @package  IPC.Test
* @author   Rajat Pandit <rajat_pandit@ipcmedia.com>
* @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link     http://ipcmedia.com
*/
class IPC_Test_Config

I hope this post if useful for anyone who wants to use a code sniffer to ensure that the quality of code is kept to its highest.