Skip to content

Rule <exclude-pattern> with type="relative" don't work #863

Open
@eduardovillao

Description

@eduardovillao

Describe the bug

The rule <exclude-pattern type="relative">don't work when is used with type="relative".

I have a local env that have the /public/ folder in base directory and also in my project I have another directory called /public/. Example: User/Sites/public/source/development/public/example.php.

So I set a custom ruleset file as example below but PHPCS is not considering any file to check (independe of the path, not just to /public/ folder as expected behavior).

Custom ruleset

(...)
    <description>Example Description</description>

    <arg name="basepath" value="./"/>

    <exclude-pattern>/vendor/*</exclude-pattern>

    <exclude-pattern>/cypress/*</exclude-pattern>
    <exclude-pattern>/.github/*</exclude-pattern>
    <exclude-pattern>/tests/*</exclude-pattern>
    <exclude-pattern type="relative">^public/.*</exclude-pattern>
(...)

Checking the PHPCS source code inside a src/Filters/Filter.php I found the root cause. This class expect receive a $basedir as a second parameter.

Image

But in all cases that is called from src/Files/FileList.php in fact it is receiving a file path as value to parameter.

Image

Image

So this will impact directly on method shouldIgnorePath inside src/Filters/Filter.php that is trying to build the relative path to be used to check the rule to ignore the file or not. At this part of the method it try to use substr to remove the $basedir from $path but as the both has the same value the $relativePath will be an empty string and for sure the file won't be ignored as expected.

Image

Image

To reproduce

Steps to reproduce the behavior:

  1. Create any <exclude-pattern> with the type="relative".
  2. Go to vendor/squizlabs/php_codesniffer/src/Filters/Filter.php file at the line 237. And add a simple code check the output.
var_dump('DEBUG: $relativePath after substr :', $relativePath);
exit;

Image

  1. Run ./vendor/bin/phpcs -p . or phpcs -p . depends on how are configured in you env.
  2. So you will see the output with an empty string

Image

Expected behavior

Expects that rule <exclude-pattern type="relative"> ignore the files that match with it. The $relativePath at the method shouldIgnorePath inside src/Filters/Filter.php don't be empty to work as expected.

Versions

Operating System MacOS 15.3
PHP version 8.0
PHP_CodeSniffer version 3.7.2
Standard MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, PHPCompatibility, PHPCompatibilityParagonieRandomCompat, PHPCompatibilityParagonieSodiumCompat, PHPCompatibilityWP, PHPCSUtils, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
Install type Composer - local

Additional context

None.

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • 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