Checking coding standards with PHP_Codesniffer


Posted in: tools | Save to del.icio.us | Twit This! 30 Mar 2009

One of the important factors in developing a successful software product is the application of coding standards. And by successful I mean a product that is easy to develop, modify and maintain. Every developer likes to code in a particular style: in the way he declares variables or function blocks, the number of indentation spaces, commenting style etc. Giving every developer in a team complete freedom to use whatever style he likes will very likely result in a code that is hard to understand, modify and maintain. Even though every coding standard has its good points and bad; the important thing is to try to adhere to a particular one.

A coding standard basically tells developers in what style they must write their code. If programmers work in a team than a coding standard ensures that each will be able to read the others code without any effort. New programmers drafted in the team will be quick to pick the coding style and continue the work rather than waste time deciphering each persons individual style.

But writing a coding standard document and making your team or yourself (if you are a individual programmer) stick to it is a completely different matter. Most programmers usually slip into their own style of coding after some time. A tool which scans your source code and checks it against a standard would be quite a help. PHP_CodeSniffer is one such tool.

PHP_CodeSniffer is a PHP5 script that “sniffs” PHP, JavaScript and CSS files to detect violations of a defined coding standard.

1. Installing PHP_Codesniffer
The easy way to install is with the PEAR installer.

c:\>pear install PHP_Codesniffer

By default a number of coding standards are installed against which you can check your code. You can find the installed standards by:

c:\>phpcs -i
The installed coding standards are MySource, PEAR, PHPCS, Squiz and Zend

2. Checking source file or directory
You can check a source file or directory against a particular standard. By default PHP_Codesniffer will use the PEAR coding standard. We will run the script on the following code using the default PEAR standard.

<?php
/* Hello.php */
function hello() {
    return "Hello World!";
}
 
echo hello();
 
?>
c:\localhost\test>phpcs hello.php

Which will return the following report.

FILE: c:\localhost\test\hello.php
--------------------------------------------------------------
FOUND 3 ERROR(S) AND 0 WARNING(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------
 2 | ERROR | Missing file doc comment
 3 | ERROR | Missing function doc comment
 3 | ERROR | Opening brace should be on a new line
--------------------------------------------------------------

Using the Zend standard on the same code will return the following.

c:\localhost\test>phpcs --standard=zend hello.php
FILE: c:\localhost\test\hello.php
-------------------------------------------------------------------
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 2 LINE(S)
-------------------------------------------------------------------
 3 | WARNING | Consider putting global function "hello" in a static class
 9 | ERROR   | A closing tag is not permitted at the end of a PHP file
--------------------------------------------------------------------

You can run the script on a directory as below with a summary report option.

c:\localhost\test>phpcs --report=summary ./ds
 
PHP CODE SNIFFER REPORT SUMMARY
----------------------------------------------------------------
FILE                                        ERRORS  WARNINGS
----------------------------------------------------------------
c:\localhost\test\ds\link_list.php             8       4
c:\localhost\test\ds\linklist.class.php        64      30
c:\localhost\test\ds\LinkListTest.php          7       0
c:\localhost\test\ds\hello.php                 3       0
----------------------------------------------------------------
A TOTAL OF 82 ERROR(S) AND 34 WARNING(S) WERE FOUND IN 4 FILE(S)
----------------------------------------------------------------

3. Your own coding standards
You can even create your own custom coding standards and add it to PHP_Codesniffer. Coding standard files in PHP_CodeSniffer are a collection of sniff files. Each sniff file checks one particular part of a coding standard only. The Coding Standard Tutorial explains it in quite a detail.

4. Subversion integration
PHP_Codesniffer also integrates with Subversion, which ensures that developers do not commit code that violates the coding standard. Instead developers are presented with the list of errors they need to correct before committing to a repository.




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



2 Responses

1

Peter

March 30th, 2009 at 7:13 am

This looks pretty cool - It strikes me that sometimes web apps will have specific coding standards for plugins built for their systems (I know wordpress does specifically, because thats what I generally use). I may see if I can set this up with a custom standard to match the wordpress coding standards to keep myself honest.

2

Sameer Borate’s Blog: Checking coding standards with PHP_Codesniffer | Development Blog With Code Updates : Developercast.com

March 30th, 2009 at 12:37 pm

[...] his code-diesel blog Sameer has posted a tutorial that introduces you to the PHP_CodeSniffer library and some simple examples of it in action. A [...]

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 14 Users Online
    • 12 Guests, 2 Bots