Skip to content

Excluding a class of rules via <exclude name="xxx"/>, inside a ruleset ... globally excludes them ... #1081

Open
@EliW

Description

@EliW

Describe the bug

I have been attempting to use the exclude functionality as shown in the documentation here:
https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset

Wherein I exclude a class of rules inside of the section that is pulling in a ruleset. Specifically the part of documentation/example here:

<!--
    You can even exclude a whole standard. This example includes
    all sniffs from the Squiz standard, but excludes any that come
    from the Generic standard.
 -->
 <rule ref="Squiz">
  <exclude name="Generic"/>
 </rule>

As it says, it will 'include all of Squiz but remove any from Squiz that come from Generic"

This is not working for me, as (for above case), it would be completely removing ALL Generic.* rules, even if included by another ruleset. Example below:

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
	<description>Coding Standards</description>

	<rule ref="WordPress-Extra">
		<!-- Include the WordPress-Extra ruleset (from a separate package, not important here though) -->
		<exclude name="PSR2"/>
	</rule>
	<rule ref="PSR12">
		<!-- Right, now declare PSR-12 please -->
	</rule>

</ruleset>

To reproduce

  1. Create a standard similar to above.
  2. Issue a phpcs -s -e
  3. See that all PSR2 rules are removed, even if added by PSR12

Expected behavior

Only declarations for PSR2 rules that exist in 'WordPress-Extra' and it's sub-rule-sets it includes ... would be excluded. But since later PSR12 is added in as another ruleset to include enmass ... PSR2 and PSR1 rules, as configured by the PSR12 file(s) ... would apply ... and therefore a -s -e would show them.

Right now, since you exclude them in the 'WordPress-Extra' context ... they are completely excluded, even if added in by PSR12 ruleset later.

Versions (please complete the following information)

Operating System MacOS 15.4.1
PHP version 8.1
PHP_CodeSniffer version 3.12.2
Standard doesn't matter
Install type Composer (local)

Additional context

n/a

Please confirm

  • [x ] I have searched the issue list and am not opening a duplicate issue.
  • [x ] I have read the Contribution Guidelines and this is not a support question.
  • [x ] I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • [x ] I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions