Joomla CodeSniffer

From Joomla! Documentation

A Nose For Joomla

This is a custom coding standard for the PHP CodeSniffer that attempts to codify and enforce the Joomla coding standards. This article covers how to set the automatic code style checker. It consist's of three parts:

  1. Install PHP CodeSniffer (phpcs)
  2. Clone Joomla Code Style (for use with for phpcs)
  3. Configure your IDE to work with PHP CodeSniffer + Joomla Code Style
PHP Code Sniffer in action


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

1. Installation of PHP Code Sniffer (phpcs)


First of all you have to install the PHP CodeSniffer. This set of files is intended to work with phpcs version 1.4, 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

To specify a specific version of the PHP CodeSniffer, type:

pear install PHP_CodeSniffer-1.5.x (specify a number for x, in August 2015 was 1.5.6)

Important note: currently the latest PHPcs is the 2.x series. But they are under development so Joomla Sniffers are not yet compatible with this 2.x version. PEAR gives you the option to install it by default but since Joomla sniffers will not work remind to always install PHPCS 1.5. version not 2.0.

Pear will handle the whole installation by itself.

A note for windows users: If you are using XAMPP, while installing the PHPCS make sure in cmd.exe the path is set to <path-to-xampp>xampp/php. After that use codeː pear install PHP_CodeSniffer-1.5.x to install PHPCS. Sometimes while running the command pear install PHP_CodeSniffer-1.5.x in cmd.exe user may get these 2 error like:
1. C:\xampp\php>pear install PHP_CodeSniffer-1.5.6 No releases available for package ""install failed
Just clear the cache using the command: pear clear-cache
After that you are good to go.
2. ERROR: failed to mkdir C:\php\pear\tests\PHP_CodeSniffer\CodeSniffer\Core\File
This error arises because config values set for different pear related directories are wrong.
Use the following command one by oneː
1. pear config-set doc_dir <PATH-TO-XAMPP>\xampp\php\pear(e.g.pear config-set doc_dir C:\xampp\php\pear)
2. pear config-set cfg_dir <PATH-TO-XAMPP>\xampp\php\pear
3. pear config-set data_dir <PATH-TO-XAMPP>\xampp\php\pear
4. pear config-set test_dir <PATH-TO-XAMPP>\xampp\php\pear
5. pear config-set www_dir <PATH-TO-XAMPP>\xampp\php\pear
End of Note

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 usually /usr/lib/php/PHP/PHP_CodeSniffer/Standards/Joomla but this varies from system to system. In Xubuntu 12.04, the path is /usr/share/php/PHP/CodeSniffer/Standards/Joomla. 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.


PHP Code Sniffer is available via Composer. It can be installed system-wide with the following command:

composer global require 'squizlabs/php_codesniffer=1.*'

On Linux PHP Code Sniffer (phpcs) will be installed under your user folder:

  • Symbolic Link: ~/.composer/vendor/bin/phpcs
  • The file: ~/.composer/vendor/squizlabs/php_codesniffer/scripts/phpcs

2. Clone Joomla Codestyle

In order to have PHP Code Sniffer (phpcs) sniffing your Joomla projects for the right code style, you've to clone the Joomla Codestyle into the /Standards/ folder of Code Sniffer. That folder already has other folders for styles like PSR1, PSR2, Squiz & Zend.

On Linux, goto the Standards folder of CodeSniffer & git clone from :

$ cd  ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/ 
$ git clone Joomla

You'll end up with a folder like ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Joomla/


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]

3. 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, Netbeans and PHPStorm that integrates the CodeSniffer into your favourite IDE, so any coding standard violations are shown like "normal" errors.


Netbeans has the sniffer functionality integrated into the core system

  1. Start your netbeans IDE
  2. Open Tools => Options => PHP => Code Analysis => Code Sniffer
  3. 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)
  4. As "Default Standard" choose "Joomla" to use the Joomla! standard
  5. now you can click Ok to start sniffing
  6. Open from the top menu Source => Inspect...
  7. Enjoy


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

PHP Tools compatibility issue

PHP Tools does not directly support PHP_CodeSniffer-1.5.x. It reports this error:

PHP Fatal error: Uncaught exception 'PHP_CodeSniffer_Exception' with message 'Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found' in /usr/share/php5/PEAR/PHP/CodeSniffer/Standards/Joomla/Sniffs/Commenting/FileCommentSniff.php:18

It comes with a pre-packaged CodeSniffer version located in eclipse/plugins/org.phpsrc.eclipse.pti.library.pear_.../php/library/PEAR/PHP. To fix the issue, you can just remove the pre-packaged CodeSniffer version and link the local version. For example, /usr/share/php/PHP.

ln -s /usr/share/php/PHP /home/.../eclipse/plugins/org.phpsrc.eclipse.pti.library.pear_.../php/library/PEAR/PHP

In any case, you can also debug a PHP Tools issue, enabling the debug mode to show the internal commands in Eclipse console:

  1. Window => Preferences
  2. PHP Tools => PHP CodeSniffer
  3. Debug: print PHP output to console

Command example:

/usr/bin/php -c /tmp/zend_debug/session8314597725413224814.tmp -d asp_tags=off -d short_open_tag=on /home/.../eclipse/plugins/ --report=xml --standard=/home/.../Joomla /home/..../myphpfile.php



CodeSniffer is supported out of the box in PHPStorm, just go to settings, and under "Inspections" you will see the list of Sniff you have installed.

See: the JetBrains documentation page for setting the Code Sniffer and also phpStorm Blog)

For users on ubuntu using phpStorm. Don't listen instructions at because there is no working bat (windows file) on linux system. To validate your CodeSniffer choose /usr/bin/phpcs launcher.

Under Settings->Inspections when you choose joomla profile, in the tree below, search for PHP and open it and than make sure that PHP Code Sniffer validation checkbox is checked. Otherwise PHP Code Sniffer won't sniff your code.

On ubuntu you install code standards with: cd /usr/share/php/PHP/CodeSniffer/Standards sudo git clone Joomla

Installing Joomla Sniff
  1. get the sniffers from the coding standards repo
  2. unpack Joomla CodeSniffer rules under:
    1. If you are using XAMPP \xampp\php\PEAR\PHP\CodeSniffer\Standards\Joomla
    2. If not maybe you should check here: /usr/share/php/PHP/CodeSniffer/Standards
  3. Go to Settings (CTRL-ALT-S) and search for "PHP Code Sniffer Validation" under inspection option, click the button Validate to force reload of rules from disk
  4. Make sure to go through this path Settings->Languages & Framework->PHP->Code Sniffer->Select 3 dot option in right pane and make the PHP Code Sniffer(PHPCS) Path to the phpcs.bat (e.g C:\xampp\php\phpcs.bat)
  5. Joomla should be now available in the list. See following image:
Installing Joomla Code style

It's nice to be able to check that the standards are respected, but it's even nicer if phpstorm helps you formatting properly too as you are coding. To add joomla code style to phpstorm

  1. first get the xml scheme file from here:
  2. Then save the file in phpstorm home directory: <phpstorm home directory>/config/codestyles/Joomla.xml
  3. restart phpstorm, and you should be able to select Joomla from the Code Style > Scheme select box

PHP PSR-0, PSR-1 and PSR-2

Installing PHP PSR-0, PSR-1 and PSR-2 Sniff

A CodeSniffer sniff to check against the PSR-x Coding Standard

Download files at

if you are using Xampp,

  1. unpack PSR-1 CodeSniffer rules under \xampp\php\PEAR\PHP\CodeSniffer\Standards\PSR (SPR or any directory will work)
  2. Go to Settings (CTRL-ALT-S) and search for "PHP Code Sniffer Validation", click the button Validate to force reload of rules from disk
  3. PSR is available in the list

PHPStorm / Alternative method

NOTE: This method is outdated, but it might be useful if you are looking for a different integration. It also demonstrates the use of external tools in PHPStorm - so it shouldn't be deleted.

The CodeSniffer can also 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. 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)
  • "Parameters":
    • --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.