Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP_CodeSniffer - Show Sniff that failed

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.

like image 316
Steven Scott Avatar asked Dec 05 '12 19:12

Steven Scott


People also ask

How do I use Phpcs?

In the Settings dialog, go to Editor > Inspections. From the inspections screen, expand the PHP | Quality tools node and enable “PHP CodeSniffer validation”. In the configuration pane that is now enabled, select “Custom” from the “Coding standard” dropdown, locate the ruleset configuration ( phpcs.

How do I run code sniffer?

In the Settings/Preferences dialog ( Ctrl+Alt+S ), navigate to PHP | Quality Tools. next to the Configuration list. In the PHP_CodeSniffer dialog that opens, empty the PHP_CodeSniffer path field. Update the project Composer dependencies by clicking Update on top of the composer.

What is Phpcbf?

PHP Code Beautifier and Fixer for Visual Studio Code. This extension provides the PHP Code Beautifier and Fixer ( phpcbf ) command for Visual Studio Code. phpcbf is the lesser known sibling of phpcs (PHP_CodeSniffer). phpcbf will try to fix and beautify your code according to a coding standard.

What is CodeSniffer?

Code Sniffer is a static test that uses static code analysis to detect violations of the Magento Coding Standard to prevent common coding errors.


1 Answers

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
like image 111
Greg Sherwood Avatar answered Oct 16 '22 10:10

Greg Sherwood