PHP_CodeSniffer 2.2.0 released22 Jan
I've just released version 2.2.0 of PHP_CodeSniffer. This release brings the usual bug fixes, but also quite a lot of sniff changes (the PHPCS and Squiz standards have changed as a result) and some important new features as well. Thanks to all the developers who directly contributed code to this release.
PHPCS Config File
I find myself regularly running PHP_CodeSniffer over a code base using the same command line arguments each time. I could put most of these arguments into the project's ruleset.xml file, but I would still need to specify what files and directories need to be checked, and what coding standard to use.
From version 2.2.0, if PHP_CodeSniffer is not given a list of files to check and is not given a standard to use, it will look for a phpcs.xml file in the current directory to figure out what it should do. The phpcs.xml file has the exact same format as a normal ruleset.xml file, so it can also be used as your primary standard. But it should also specify some files and/or directories to process using the new ruleset <file> tags. Take a look at PHP_CodeSniffer's own phpcs.xml file for an example.
Having one of these phpcs.xml files allows you to run the phpcs or phpcbf commands without specifying any command line options and still have them check or fix an entire codebase.
If you are using the phpcs.xml file as your primary standard, the files and directories you specify in there will be ignored if any files or directories are specified on the command line. So you can run phpcs like this:
Or you can use the phpcs.xml as a normal standard:
$ phpcs --standard=phpcs.xml /path/to/file
Coloured Report Output
After years of prompting, I've finally added colours to the report and progress output that PHP_CodeSniffer produces.
If you want to see colours in the output, use the --colors command line argument. Use the command phpcs --config-set colors true to turn colours on by default, or configure it in your ruleset. If you have done this, use the --no-colors command line argument to turn colours off for a single run.
Hopefully the colours are tastefully used and don't feel like using colour just for the sake of it. If you disagree, please let me know.
PHP_CodeSniffer has always allowed you to set a report width so that you can see more output in the screen-based reports. From version 2.2.0, you can tell PHP_CodeSniffer to use as much space as it can by adding --report-width=auto to the command.
When using auto width, PHP_CodeSniffer will use the entire width of your terminal window to produce report output. If the report doesn't need all that width, PHP_CodeSniffer will only use as much as it needs to print the report without wrapping. Use the command phpcs --config-set report_width auto to use auto-sizing by default, or configure it in your ruleset.
PSR2 Tab Width
The PSR2 standard now includes a tab-width setting of 4 in the ruleset.xml file. PSR2 requires 4 spaces for indenting, so running that standard over tab indented code will now detect, and optionally fix, those tab indents correctly without you needing to specify the --tab-width=4 command line argument.