Difference between revisions of "Joomla CodeSniffer"

From Joomla! Documentation

(Removed outdated netbeans plugin and link to a better version)
Line 107: Line 107:
<pre>-np --standard=/home/elkuku/libs/joomla/build/phpcs/Joomla --report=emacs $FilePath$</pre>
<pre>-np --standard=/home/elkuku/libs/joomla/build/phpcs/Joomla --report=emacs $FilePath$</pre>
You may now right click any file or folder and choose the sniffer from the context menu.
You may now right click any file or folder and choose the sniffer from the context menu or add a new toolbar button with a nice Joomla! logo [[File:icon-16-joomla.png]].
=== References ===
=== References ===

Revision as of 11:06, 8 February 2012

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

A Nose For Joomla

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


- 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.


First of all you have to install the PHP CodeSniffer. This set of files is intended to work with phpcs version 1.3, so behavior with any other version is undefined. One way to do this is to start the console (cmd.exe in windows environments) and type:

pear install PHP_CodeSniffer-1.3.2

Pear will handle the whole installation by itself.

Then download and unzip the Joomla! CodeSniffer and copy the contents of it into /path/to/PHP_CodeSniffer/Standards/Joomla.

In Unix systems this path is usualy /usr/lib/php/PHP/PHP_CodeSniffer/Standards/Joomla but this varies from system to system. Windows based system with XAMPP have the following path: PATH_TO_XAMMP\php\PEAR\PHP\CodeSniffer\Standards (for example: C:\xampp\php\PEAR\PHP\CodeSniffer\Standards)


pear config-get php_dir

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


You invoke the custom standard by

phpcs --standard=Joomla file/to/sniff

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

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 and netbeans that integrates the CodeSniffer in our favorite IDE, so any coding standard violations are shown like "normal" errors.


For netbeans you have to install the sniffer plugin first. You can download the plugin here.

  1. Start your netbeans IDE
  2. Open Tools => Plugins => Downloaded and press Add Plugin
  3. Choose the loaded nbm file and confirm the installation
  4. Now there is a new tab in Tools => Options => PHP called "PHPCodeSniffer"
  5. You have to set the path to the phpcs.bat from the installed PHP_CodeSniffer PEAR package
    • for Unix based systems the path is something like /usr/bin/phpcs
    • in XAMPP (windows) you can find the file in the php root folder (e.g. C:\xampp\php\phpcs.bat)
  6. As "standard" type "Joomla" to use the Joomla! standard
  7. now you can click test Settings to check the settings and finish the installation by pressing Ok
  8. Open he task window (Window => Tasks) to sniff your code.
  9. Most time it is a good idea to filter the task (show only in edited file or create an own filter which displays only sniff errors)


1) Eclipse PTI

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:
  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.

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


There is currently no plugin available, but the CodeSniffer can be integrated easily as an external tool. PHPStorm will display the output in the console, including clickable links containing line and column numbers to the files that contain errors.

  • Click on "Settings" and search for "External tools"
  • Click "Add..."
  • Choose a "name", "Group" and "Description".
  • Click "Output Filters"
    • Click "Add...", Choose a name and enter under "Regular expression to match output" the value: $FILE_PATH$:$LINE$:$COLUMN$
  • "Program": Search for the phpcs executable on your system.
  • "Paramaters":
    • --standard=<path/to/joomla-platform>/build/phpcs/Joomla The path to the Joomla! coding standards.
    • --report=emacs The will generate a simple list containing links to the error files
    • Optional you may want to specify -p for "progress" or -n for "errors only".
    • The last parameter has to be $FilePath$ specifying the file or folder you want to sniff.

A typical "Parameters" line on a Linux system might look like this:

-np --standard=/home/elkuku/libs/joomla/build/phpcs/Joomla --report=emacs $FilePath$

You may now right click any file or folder and choose the sniffer from the context menu or add a new toolbar button with a nice Joomla! logo Icon-16-joomla.png.