Actions

Difference between revisions of "Joomla CodeSniffer"

From Joomla! Documentation

(PHPStorm 3.x / Alternative method: Fix wrong wiki syntax)
m (Clean up image)
 
Line 1: Line 1:
(This page needs expanded installation instructions, especially for Windows-based systems.)
 
 
 
== A Nose For Joomla ==
 
== 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 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.
 +
 
 +
 
 +
[[File:CodeSnifferInAction.jpg|center|thumb|300px|PHP Code Sniffer in action]]
  
 
==Why?==
 
==Why?==
Line 17: Line 18:
 
==Installation==
 
==Installation==
  
First of all you have to install the [http://pear.php.net/package/PHP_CodeSniffer/ 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:
+
First of all you have to install the [http://pear.php.net/package/PHP_CodeSniffer/ 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:
  
<code>pear install PHP_CodeSniffer-1.3.2</code>
+
<code>pear install PHP_CodeSniffer</code>
 +
 
 +
To specify a specific version of the PHP CodeSniffer, type:
 +
 
 +
<code>pear install PHP_CodeSniffer-1.5.x</code> (specify a number for x, in June 2014 was 1.5.3)
 +
 
 +
'''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 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.
 
Pear will handle the whole installation by itself.
  
Then download and unzip the [https://github.com/joomla/joomla-platform/tree/master/build/phpcs Joomla! CodeSniffer] and copy the contents of it into /path/to/PHP_CodeSniffer/Standards/Joomla.
+
Then download and unzip the [https://github.com/joomla/coding-standards 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.
+
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)
 
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)
  
Line 50: Line 57:
 
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.
 
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.
+
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 ===
 
=== Netbeans ===
Line 92: Line 99:
  
 
==== PHPStorm ====
 
==== PHPStorm ====
[[Image:PhpStorm4CodeSniffer.png|left|thumb|200px|1) PHPStorm  4.x CodeSniffer]]
 
<div style="clear: both"></div>
 
 
 
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.
 
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 [http://blog.jetbrains.com/webide/2012/03/checking-your-code-with-php-code-sniffer-in-phpstorm-4-0/ phpStorm Blog])
+
See: the [http://www.jetbrains.com/phpstorm/webhelp/using-php-code-sniffer-tool.html JetBrains documentation page for setting the Code Sniffer]  and also  [http://blog.jetbrains.com/webide/2012/03/checking-your-code-with-php-code-sniffer-in-phpstorm-4-0/ 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 =====
 
===== Installing Joomla Sniff =====
  
if you are using Xampp,
+
# get the sniffers from the [https://github.com/joomla/coding-standards coding standards repo]
# unpack Joomla CodeSniffer rules under \xampp\php\PEAR\PHP\CodeSniffer\Standards\Joomla
+
# 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
 
# 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 is available in the list
+
# Joomla should be now available in the list. See following image:
 +
<gallery>
 +
File:PhpStorm4CodeSniffer.png|PHPStorm  4.x CodeSniffer
 +
File:PhpStorm6CodeSniffer.png|PHPStorm  6.x CodeSniffer
 +
File:PhpStorm8CodeSniffer.png|PHPStorm  8.x CodeSniffer
 +
</gallery>
 +
<div style="clear: both"></div>
 +
 
 +
===== 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
 +
# first get the xml scheme file from here: https://github.com/betweenbrain/phpstorm-joomla-code-style
 +
# Then save the file in phpstorm [http://www.jetbrains.com/phpstorm/webhelp/project-and-ide-settings.html#d231397e460 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 ====
 +
* 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 =====
  
===== Installing PHP PSR-1 and -2 Sniff =====
+
A CodeSniffer sniff to check against the PSR-x Coding Standard
  
A CodeSniffer sniff to check against the PSR-1 Coding Standard (thanks to https://github.com/klaussilveira/phpcs-psr1 for posting the codesniffer rules)
+
Download files at
 +
* https://github.com/squizlabs/PHP_CodeSniffer/tree/master/CodeSniffer/Standards
  
 
if you are using Xampp,  
 
if you are using Xampp,  
Line 115: Line 150:
 
# PSR is available in the list
 
# PSR is available in the list
  
==== PHPStorm 3.x / Alternative method ====
+
==== 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.</note>
+
'''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''.
  
<del>There is currently no plugin available</del> 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.
+
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 on "Settings" and search for "External tools"
Line 145: Line 180:
  
 
[[Category:Development]][[Category:Bug Squad]]
 
[[Category:Development]][[Category:Bug Squad]]
 +
[[Category:IDE (Integrated development environment)]]

Latest revision as of 05:10, 25 October 2014