Difference between revisions of "Joomla CodeSniffer"

From Joomla! Documentation

(→‎Installation: update platform coding standards usage)
Line 24: Line 24:
  
 
to find out where the PEAR directory is on your system, then add "/PHP/PHP_CodeSniffer" to it.)
 
to find out where the PEAR directory is on your system, then add "/PHP/PHP_CodeSniffer" to it.)
 +
 +
== Usage ==
  
 
You invoke the custom standard by
 
You invoke the custom standard by
Line 29: Line 31:
 
<code>phpcs --standard=Joomla --tab-width=4 file/to/sniff</code>
 
<code>phpcs --standard=Joomla --tab-width=4 file/to/sniff</code>
  
ATTENTION: When you are developing for the Joomla platform project, the updated coding style guidelines can be found in the build folder of the github project [https://github.com/joomla/joomla-platform/tree/master/build/phpcs here]. The standard from Joomlacode is not up-to-date. In that case, you also have to ommit the --tab-width=4 parameter.
+
'''NOTE''': When you are developing for the Joomla platform project, the updated coding style guidelines can be found in the build folder of the github project [https://github.com/joomla/joomla-platform/tree/master/build/phpcs here]. The standard from Joomlacode is not up-to-date. In that case, you also have to ommit the --tab-width=4 parameter.
 +
 
 +
To test a platform file using the provided platform coding standards use
 +
 
 +
<code>phpcs --standard=build/phpcs/Joomla path/to/file/or/folder</code>
  
 
Further documentation on the use of phpcs can be found at: [http://pear.php.net/package/PHP_CodeSniffer/docs]
 
Further documentation on the use of phpcs can be found at: [http://pear.php.net/package/PHP_CodeSniffer/docs]

Revision as of 03:51, 24 November 2011

(This page needs expanded installation instructions, especially for Windows-based systems.)

A Nose For Joomla[edit]

This is a custom coding standard for the PHP CodeSniffer that attempts to codify and enforce the Joomla coding standards.

Why?[edit]

- Coherent and consisting coding practice makes the files look more professional. Conflicting styles in the same project (or worse, the same file) not only look sloppy, they encourage further sloppiness.

- When all code complies with the same standard, bad code is easier for everyone to spot.

- It makes it easier for someone new to a particular file in the project to find and fix errors, or extend functionality.

- If there is no consistent standard maintained, the sometimes developers will reformat the code to suit themselves. This causes a wide range of changes in the code repository, and if there is a later problem, a significant change could be lost in the chaff produced by a diff.

Installation[edit]

First you'll need to install phpcs (http://pear.php.net/package/PHP_CodeSniffer/download/). This set of files is intended to work with phpcs version 1.3, so behavior with any other version is undefined.

Then download and unzip the Joomla CodeSniffer, and copy the contents of it into /path/to/PHP_CodeSniffer/Standards/Joomla. (The path on some systems is /usr/lib/php/PHP/PHP_CodeSniffer but this varies from system to system. Use

pear config-get php_dir

to find out where the PEAR directory is on your system, then add "/PHP/PHP_CodeSniffer" to it.)

Usage[edit]

You invoke the custom standard by

phpcs --standard=Joomla --tab-width=4 file/to/sniff

NOTE: When you are developing for the Joomla platform project, the updated coding style guidelines can be found in the build folder of the github project here. The standard from Joomlacode is not up-to-date. In that case, you also have to ommit the --tab-width=4 parameter.

To test a platform file using the provided platform coding standards use

phpcs --standard=build/phpcs/Joomla path/to/file/or/folder

Further documentation on the use of phpcs can be found at: [1]

IDE integration[edit]

1) Eclipse PTI

While everybody loves the console as it is with no doubt the most effective way to do whatever you need to do.. sometimes even linux gurus need a little bit of comfort.

Fortunately there is a plugin available for eclipse (tested) and netbeans (not tested) that integrates the CodeSniffer in our favorite IDE, so any coding standard violations are shown like "normal" errors (see screen shot 1).

Installation is a breeze and follows the usual pattern:

  1. Help => Install new Software...
  2. Work with: Fill in one of the update site URLs found here: http://www.phpsrc.org/
  3. Select the desired tools
  4. Restart Eclipse.
2) Eclipse PTI settings

You are now able to sniff for code violations against common standards like PEAR or Zend etc.

Notice that the PTI project currently is not working right on the Eclipse Indigo releases. If you want to use the Eclipse integration for the PHP_CodeSniffer, use an Eclipse Helios release and install it in there.

To sniff against your own standards, all you have to do is specify their location and activate them (see screen shot 2)

  1. Window => Preferences
  2. PHP Tools => PHP CodeSniffer

Happy sniffing