#native_company# #native_desc#
#native_cta#

Enforcing Coding Standards with PHP_CodeSniffer Page 2

By W. Jason Gilmore
on October 21, 2010

Validating a PHP Script

Although you can define your own coding standards and use them in conjunction with PHP_CodeSniffer, chances are one of the standards bundled with the package will be useful for you. These include MySource, PEAR (the default), PHPCS, Squiz and Zend. Therefore, to determine how well a particular PHP script conforms to the PEAR Coding Standard, all you need to do is pass the name (and potentially the path) of the script to phpcs:

%>phpcs AboutController.php

Depending on how well your script conforms, you may see any number of returned errors. As an example, I’ve purposefully added several mistakes to this AboutController.php script, producing the output presented below:

--------------------------------------------------------------------------------
FOUND 13 ERROR(S) AND 2 WARNING(S) AFFECTING 11 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR   | Missing file doc comment
  3 | ERROR   | Extra newline found before class comment short description
  4 | ERROR   | There must be exactly one blank line before the tags in class
    |         | comments
  4 | ERROR   | Content of the @author tag must be in the form "Display Name
    |         | <[email protected]>"
  6 | ERROR   | Missing @category tag in class comment
  6 | ERROR   | Missing @package tag in class comment
  6 | ERROR   | Missing @license tag in class comment
  6 | ERROR   | Missing @link tag in class comment
 10 | ERROR   | Missing function doc comment
 15 | ERROR   | Function doc comment is empty
 25 | ERROR   | Function doc comment is empty
 30 | ERROR   | Public method name "AboutController::ContactAction" is not in
    |         | camel caps format
 41 | WARNING | Line exceeds 85 characters; contains 97 characters
 47 | WARNING | Line exceeds 85 characters; contains 133 characters
 59 | ERROR   | Function doc comment is empty
--------------------------------------------------------------------------------

Obviously, using this output as a guide, you can iteratively fix the errors and rerun the phpcs command until the script is acceptable.
To validate an entire directory of scripts, you can pass phpcs the directory path:

%>phpcs /var/www/dev.wjgilmore.com/application/controllers

Testing JavaScript and CSS Files

You can also use PHP_CodeSniffer to examine your JavaScript and CSS files. In particular, the Squiz coding standard (bundled with PHP_CodeSniffer) can be incredibly useful for examining these files, as it uses JSLint for examining JavaScript files and performs a number of useful CSS validations, such as determining whether a particular style is defined more than once.
For instance, to use Squiz when validating a file, pass along the --standard option:

%>phpcs --standard=Squiz styles.css

Conclusion

Over the years I’ve introduced a number of tools which can make your life as a developer much easier, among them Git, PHPDocumentor and PHPUnit. With PHP_CodeSniffer added to your workbench, you’ll have another powerful tool capable of ensuring your code meets team guidelines, which will in turn improve the productivity of all involved.

About the Author

Jason Gilmore is the founder of the publishing and consulting firm WJGilmore.com. He also is the author of several popular books, including “Easy PHP Websites with the Zend Framework”, “Easy PayPal with PHP”, and “Beginning PHP and MySQL, Fourth Edition”. Follow him on Twitter at @wjgilmore.