squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Can't Exclude Sniffs from Command Line #3895

Closed AntumDeluge closed 1 year ago

AntumDeluge commented 1 year ago

Describe the bug

Using the --exclude option from command line has no effect on output.

Code sample

test.php:

<?php

function foo() {
  echo "Hello world!";
}

?>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs -s --standard=PEAR --exclude=Commenting.FileComment.Missing test.php
  3. See error is not omitted
$ phpcs -s --standard=PEAR test.php

FILE: <redacted>/test.php
------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 3 LINES
------------------------------------------------------------------------------------------------------------------------
 2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing)
 3 | ERROR | [ ] Missing doc comment for function foo() (PEAR.Commenting.FunctionComment.Missing)
 3 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine)
 4 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
   |       |     (PEAR.WhiteSpace.ScopeIndent.Incorrect)
$ phpcs -s --standard=PEAR --exclude=Commenting.FileComment.Missing test.php

FILE: <redacted>/test.php
------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 3 LINES
------------------------------------------------------------------------------------------------------------------------
 2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing)
 3 | ERROR | [ ] Missing doc comment for function foo() (PEAR.Commenting.FunctionComment.Missing)
 3 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine)
 4 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
   |       |     (PEAR.WhiteSpace.ScopeIndent.Incorrect)

Looking through some of the other issues, it appears they are using the full sniff name prefixed with the standard (e.g. --exclude=PEAR.Commenting.FileComment.Missing). But if I do that it reports sniff code is invalid:

$ phpcs -s --standard=PEAR --exclude=PEAR.Commenting.FileComment.Missing test.php
ERROR: The specified sniff code "PEAR.Commenting.FileComment.Missing" is invalid

It works if I include the standard & omit the last (fourth) part:

$ phpcs -s --standard=PEAR --exclude=PEAR.Commenting.FileComment test.php

FILE: <redacted>/test.php
------------------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------
 3 | ERROR | [ ] Missing doc comment for function foo() (PEAR.Commenting.FunctionComment.Missing)
 3 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine)
 4 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
   |       |     (PEAR.WhiteSpace.ScopeIndent.Incorrect)

But then doesn't that disable all sniffs under PEAR.Commenting.FileComment? E.g. it will also exclude PEAR.Commenting.FileComment.WrongStyle.

Expected behavior

PEAR.Commenting.FileComment.Missing errors not shown.

Versions (please complete the following information)

Operating System Ubuntu 22.04.3
PHP version 8.1.2
PHP_CodeSniffer version 3.7.2
Standard PEAR
Install type PEAR

Additional context

I have confirmed that using a .phpcs.xml configuration with <exclude name="PEAR.Commenting.FileComment.Missing"/> does work.

Issues that appear to be related:

Please confirm:

jrfnl commented 1 year ago

@AntumDeluge This is not a bug, but the documented, expected behaviour.

An error code is build up like so: StandardName.CategoryName.SniffName.ErrorCode.

From a custom ruleset you can in-/exclude individual error codes (four part name), sniffs (three part name), categories (two part name) or even all sniffs from a standard (one part name) when one standard includes another.

However, from the command-line you can only in-/exclude complete sniffs (three part name).

Omitting the StandardName will not turn an error code into a sniff name and will not work.

But then doesn't that disable all sniffs under PEAR.Commenting.FileComment ?

There is only one PEAR.Commenting.FileComment sniff (= file containing the code for the check(s)), though that one sniff can have, and in this case does have, multiple error codes.

And yes, excluding the PEAR.Commenting.FileComment sniff from the command line will exclude all error codes from that sniff.

In other words, this is not a bug. I can imagine this could be seen as a feature request along the same lines as #1969, in which case, I'd suggest closing this ticket as a duplicate and continuing the feature request discussion there.

AntumDeluge commented 1 year ago

@jrfnl I appreciate the information. I will use a configuration file for more precise management & close this issue. Thank you for the reply.