Tuesday 30 January 2007

Long error messages now wrap in PHP_CodeSniffer

I added a new feature to PHP_Codesniffer yesterday that wraps error and warning messages to 80 characters. I wanted to blog about this feature because a patch was actually submitted to me by Endre Czirbesz, a user of PHP_CodeSniffer. I made some minor modifications to the patch to bring it up-to-date with the latest CVS code, but it's safe to say that this feature would not exist without Endre's help.

Here is an example of some error messages without wrapping (you'll have to forgive this Blogger template for cutting the content short):

FILE: FunctionClosingBraceSpaceUnitTest.inc
--------------------------------------------------------------------------------
FOUND 14 ERROR(S) AND 0 WARNING(S) AFFECTING 6 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
3 | ERROR | Missing function doc comment
3 | ERROR | Opening function brace should be on a new line.
3 | ERROR | Function name "MyFunction1" is invalid; consider "myFunction1" instead
8 | ERROR | You must use "/**" style comments for a function comment
8 | ERROR | Opening function brace should be on a new line.
8 | ERROR | Function name "MyFunction2" is invalid; consider "myFunction2" instead
--------------------------------------------------------------------------------
And here are the same errors with wrapping:
FILE: FunctionClosingBraceSpaceUnitTest.inc
--------------------------------------------------------------------------------
FOUND 14 ERROR(S) AND 0 WARNING(S) AFFECTING 6 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
3 | ERROR | Missing function doc comment
3 | ERROR | Opening function brace should be on a new line
3 | ERROR | Function name "MyFunction1" is invalid; consider "myFunction1"
| | instead
8 | ERROR | You must use "/**" style comments for a function comment
8 | ERROR | Opening function brace should be on a new line
8 | ERROR | Function name "MyFunction2" is invalid; consider "myFunction2"
| | instead
--------------------------------------------------------------------------------
Even without truly excessive error messages, the new wrapped format is much easier to read or send in an email notification. It also allows for more descriptive error messages in the future, if required.

You can get the latest PHP_CodeSniffer with this feature in it from CVS right now, or wait until the next official release, which is probably still a few weeks away.