Difference between revisions of "Joomla CodeSniffer"
From Joomla! Documentation
(Windows instructions seem pretty good!) |
Javiparati (talk | contribs) (→PHPStorm 3.x / Alternative method: this is really old. We are not in version 3 but 6 of PHPStorm so I'm removing this) |
||
Line 144: | Line 144: | ||
# Go to Settings (CTRL-ALT-S) and search for "PHP Code Sniffer Validation", click the button Validate to force reload of rules from disk | # Go to Settings (CTRL-ALT-S) and search for "PHP Code Sniffer Validation", click the button Validate to force reload of rules from disk | ||
# PSR is available in the list | # PSR is available in the list | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== References === | === References === |
Revision as of 09:58, 15 August 2013
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 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.4.x
(specify a number for x, versions 1.4.0 through 1.4.5 are available)
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 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)
Use
pear config-get php_dir
to find out where the PEAR directory is on your system, then add "/PHP/PHP_CodeSniffer/Standards" to it.
Usage[edit]
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[edit]
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 PHP Storm that integrates the CodeSniffer into your favorite IDE, so any coding standard violations are shown like "normal" errors.
Netbeans[edit]
For netbeans you have to install the sniffer plugin first. You can download the plugin here.
- Start your netbeans IDE
- Open Tools => Plugins => Downloaded and press Add Plugin
- Choose the loaded nbm file and confirm the installation
- Now there is a new tab in Tools => Options => PHP called "PHPCodeSniffer"
- 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)
- As "standard" type "Joomla" to use the Joomla! standard
- now you can click test Settings to check the settings and finish the installation by pressing Ok
- Open he task window (Window => Tasks) to sniff your code.
- 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)
Eclipse[edit]
Installation is a breeze and follows the usual pattern:
- Help => Install new Software...
- Work with: Fill in one of the update site URLs found here: http://www.phpsrc.org/eclipse/pti/
- Select the desired tools
- Restart Eclipse.
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)
- Window => Preferences
- PHP Tools => PHP CodeSniffer
Happy sniffing
PHPStorm[edit]
PHPStorm[edit]
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 http://www.jetbrains.com/phpstorm/webhelp/using-php-code-sniffer-tool.html#d59626e1126 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 http://github.com/joomla/coding-standards.git Joomla
Installing Joomla Sniff[edit]
- get the sniffers from the coding standards repo
- unpack Joomla CodeSniffer rules under:
- If you are using XAMPP \xampp\php\PEAR\PHP\CodeSniffer\Standards\Joomla
- If not maybe you should check here: /usr/share/php/PHP/CodeSniffer/Standards
- Go to Settings (CTRL-ALT-S) and search for "PHP Code Sniffer Validation", click the button Validate to force reload of rules from disk
- Joomla should be now available in the list. See following image:
Installing Joomla Code style[edit]
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
- first get the xml scheme file from here: https://github.com/betweenbrain/phpstorm-joomla-code-style
- Then save the file in phpstorm home directory: <phpstorm home directory>/config/codestyles/Joomla.xml
- 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[edit]
- https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
- https://github.com/pmjones/fig-standards/blob/psr-1-style-guide/proposed/PSR-1-basic.md
- https://github.com/pmjones/fig-standards/blob/psr-1-style-guide/proposed/PSR-2-advanced.md
Installing PHP PSR-0, PSR-1 and PSR-2 Sniff[edit]
A CodeSniffer sniff to check against the PSR-x Coding Standard
Download files at
if you are using Xampp,
- unpack PSR-1 CodeSniffer rules under \xampp\php\PEAR\PHP\CodeSniffer\Standards\PSR (SPR or any directory will work)
- Go to Settings (CTRL-ALT-S) and search for "PHP Code Sniffer Validation", click the button Validate to force reload of rules from disk
- PSR is available in the list
References[edit]
- http://www.phpsrc.org/ PTI - PHP tools integration for Eclipse
- http://sourceforge.net/projects/phpmdnb/ Netbeans plugin
- http://hakre.wordpress.com/2010/03/06/php-code-sniffer-eclipse-and-wordpress/ - Excellent article. Just change "Wordpress" for "Joomla!" ;)