Multiple output renderers in PHP_CodeSniffer
12 JanOne of the oldest PHP_CodeSniffer feature requests, added back in 2007, is one to allow PHP_CodeSniffer to produce multiple reports after a run. PHP_CodeSniffer provides a number of reporting options, including writing the report to a file as well as the screen, but you've only been able to select one report type to view.
I'm happy to report that this feature has finally been completed, is now available in SVN and will be available in the next release.
How does it work?
Using the new feature is pretty easy. Previously, you'd specify a report type like this:
phpcs --report=summary
This still works, but you can also specify reports like this:
phpcs --report-summary --report-source
This tells PHP_CodeSniffer to print two reports to the screen.
You can also print reports to a file:
phpcs --report-summary=/tmp/summary.txt --report-source=/tmp/source.txt
Or even mix and match:
phpcs --report-summary --report-source=/tmp/source.txt
Note that if you are printing reports to the screen, they will be printed in the order that you specify them on the command line. So if you'd like to see a full report with a summary at the bottom, you would use the command:
phpcs --report-full --report-summary
Why is this useful?
It's all about saving time. If you need to view more than one report, you're now saving yourself time with this feature.
If you are checking a lot of files, PHP_CodeSniffer might take a while to run and might produce quite a long error report. Sometimes you just want a summary of the files that have errors so you can assign files to different developers. Sometimes you want the complete list of errors to act as a todo list. Often, you want both. So now, you can print both the full and summary reports in one run rather than having to process your files twice.
Build systems also benefit from this feature. A build system will often capture the output produced by PHP_CodeSniffer and display it somewhere in the interface, but it will also source the full error list from a file, often in an XML or CheckStyle format. Previously, you'd have the same XML-based report displayed in the output as you did in the file, but now you can have your build system show a summary of the errors from the output and still print the complete list to a file for post-processing.
How do I get it?
If you can't wait for the next release, you can play around with this new feature by installing PHP_CodeSniffer from the SVN source. Note that this will replace your existing PHP_CodeSniffer install:
pear uninstall PHP_CodeSniffer
svn co http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk PHP_CodeSniffer
cd PHP_CodeSniffer
pear install -f package.xml