Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory exclude pattern improperly excludes directories with names that start the same #3007

Closed
SteveTalbot opened this issue Jul 6, 2020 · 0 comments

Comments

@SteveTalbot
Copy link

This issue affects version 3.5.5 and is very closely related to #1920

The problem

Imagine I want to exclude files in bin directories:

<exclude-pattern>*/bin/*</exclude-pattern>

This rule will also match and exclude any directory starting with bin, such as ./src/BingSearch/

The problem appears to be in the same area of code that caused #1920, specifically Filter.php#L221:

                if (substr($pattern, -2) === '/*') {
                    // Need to check this pattern for dirs as well as individual file paths.
                    $this->ignoreFilePatterns[$pattern] = $type;

                    $pattern = substr($pattern, 0, -2);
                    $this->ignoreDirPatterns[$pattern] = $type;
                } else {
                    // This is a file-specific pattern, so only need to check this
                    // for individual file paths.
                    $this->ignoreFilePatterns[$pattern] = $type;
                }

Removing the trailing slash with substr allows the pattern to match the directory, but it also allows it to match any directory with a name that starts the same.

Proposed solution

A simple fix is to anchor the directory pattern to the end of the string, like this:

                    $pattern = substr($pattern, 0, -2) . '$';
                    $this->ignoreDirPatterns[$pattern] = $type;

Or use a lookahead:

                    $pattern = substr($pattern, 0, -2) . '(?=/|$)';
                    $this->ignoreDirPatterns[$pattern] = $type;

Workaround

To exclude files in bin directories, use a lookahead in the exclude pattern in phpcs.xml:

<exclude-pattern>*/bin(?=/|$)/*</exclude-pattern>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

No branches or pull requests

2 participants