Continuous integration/PHP CodeSniffer

Draft to implement MediaWiki coding conventions using the PHP_CodeSniffer tool.

PHP_CodeSniffer is a style check tool for PHP. It is highly modulars, each rule is an independent class, to write a standard you would create a ruleset.xml including the class you want to check your code against. Each modules can thus reuse other module rules and then implements its own rules.

installing
First you want to install PHP_CodeSniffer using pear, the tool will be then made available as phpcs.

$ pear install PHP_CodeSniffer $ phpcs --version PHP_CodeSniffer version 1.4.3 (stable) by Squiz Pty Ltd. (http://www.squiz.com.au) $

You will want to grab the MediaWiki standard for PHP_CodeSniffer, it is available in the WMF git repository mediawiki/tools/codesniffer.git/:


 * Gitweb: https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/tools/codesniffer.git
 * Clone anon:  https://gerrit.wikimedia.org/r/p/mediawiki/tools/codesniffer.git 
 * Clone auth:  ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer.git 

To load the MediaWiki standard, you will have to pass the full path to the phpcs command:

$ phpcs --standard=/path/to/mediawiki/tools/codesniffer

You will want to make an alias for it:

$ alias phpcsmw='phpcs --standard=/path/to/mediawiki/tools/codesniffer/MediaWiki'

run
cd /path/to/mediawiki phpcsmw -s -v.


 * Useful options:
 * -v verbose mode (show the files being parsed)
 * -p gives you some progress (output E/. for each file kind of like PHPUnit)
 * -s shows the sniff name in the report, really useful

To get a nice report for a directory:

phpcsmw -v -s includes/api --report-summary

Where:
 * -v show the progress
 * -s report the snif name
 * --report-summary build a report per file and, since -s was given, per sniff.

getting details
One can list the available coding standards using phpcs -i. As of version 1.4.3, that does not show a standard loaded using --standard like our phpcsmw alias. Anyway, a list of standards would be:

$ phpcsmw -i The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend $

To list the rule for a standard, pass the '-e' option:

$ phpcsmw --standard=PSR1 -e

The PSR1 standard contains 7 sniffs

Generic (4 sniffs) -- Generic.Files.ByteOrderMark Generic.NamingConventions.CamelCapsFunctionName Generic.NamingConventions.UpperCaseConstantName Generic.PHP.DisallowShortOpenTag

PSR1 (2 sniffs) --- PSR1.Classes.ClassDeclaration PSR1.Files.SideEffects

Squiz (1 sniffs)

Squiz.Classes.ValidClassName

Below each section is a list of sniffs which are really a PHP class which can then be reused by a module. That is exactly what we are doing for our MediaWiki module, instead of reinventing the wheel, we include sniffs from other modules.