16

Is there a setting in the PHP_CodeSniffer to show the sniff that failed? I am comparing the output to our coding standards, and using one by one is tough to decipher which test is failing, to see which we may want to ignore.

If there was a simple method to show the failure sniff, then I could complete the configuration a lot easier and quicker.

Steven Scott
  • 10,234
  • 9
  • 69
  • 117

1 Answers1

25

You can use the -s command line argument to show the source for an error message.

$ phpcs temp.php -s       

FILE: /Users/gsherwood/Sites/Projects/PHP_CodeSniffer/temp.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------------------------
 2 | ERROR | Missing file doc comment (PEAR.Commenting.FileComment.Missing)
 2 | ERROR | Missing class doc comment (PEAR.Commenting.ClassComment.Missing)
 2 | ERROR | Opening brace of a class must be on the line after the definition
   |       | (PEAR.Classes.ClassDeclaration.OpenBraceNewLine)
 3 | ERROR | Missing function doc comment
   |       | (PEAR.Commenting.FunctionComment.Missing)
--------------------------------------------------------------------------------

Time: 0 seconds, Memory: 4.50Mb

You can also use the source report to show a list of all failed sniffs.

$ phpcs temp.php --report=source

PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
--------------------------------------------------------------------------------
STANDARD  CATEGORY            SNIFF                                        COUNT
--------------------------------------------------------------------------------
PEAR      Commenting          File comment missing                         1
PEAR      Commenting          Class comment missing                        1
PEAR      Classes             Class declaration open brace new line        1
PEAR      Commenting          Function comment missing                     1
--------------------------------------------------------------------------------
A TOTAL OF 4 SNIFF VIOLATION(S) WERE FOUND IN 4 SOURCE(S)
--------------------------------------------------------------------------------

Time: 0 seconds, Memory: 4.75Mb

$ phpcs temp.php --report=source -s

PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
--------------------------------------------------------------------------------
SOURCE                                                                     COUNT
--------------------------------------------------------------------------------
PEAR.Commenting.FileComment.Missing                                        1
PEAR.Commenting.ClassComment.Missing                                       1
PEAR.Classes.ClassDeclaration.OpenBraceNewLine                             1
PEAR.Commenting.FunctionComment.Missing                                    1
--------------------------------------------------------------------------------
A TOTAL OF 4 SNIFF VIOLATION(S) WERE FOUND IN 4 SOURCE(S)
--------------------------------------------------------------------------------

Time: 0 seconds, Memory: 4.75Mb
Greg Sherwood
  • 6,992
  • 2
  • 29
  • 24
  • Any way to have both options? I like the -s to show me the sniff that failed with the error, and then the summary view of the --report=source is good for overall coverage. However, the ability to see both would be nice. – Steven Scott Dec 06 '12 at 15:47
  • 3
    You can print 2 reports if you want, or as many as you like. You an also print different reports to different output files if needed. For the example I gave, to show both reports, you would use the command: `phpcs temp.php -s --report=full --report=source` – Greg Sherwood Dec 06 '12 at 21:19
  • Thanks. That second option for the two reports is exactly what I was looking for. If only it was a normal answer so I could vote it up again. Thanks again. – Steven Scott Dec 07 '12 at 21:20