PHPCSStandards / PHP_CodeSniffer

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

Fixed pattern replacements in Filter.php #602

Closed arturm11 closed 1 week ago

arturm11 commented 2 weeks ago

Fixed bad pattern replacements. Eliminates warnings when used as PHP Sniffer extension in VSCode.

Description

If full Windows path is used as ignore filter in shouldIgnorePath() function, then conversion to PREG pattern was buggy. Windows backslash separator should be converted into double backslash (escaped backslash) AND Linux slash separator should be converted into double backslash.

Suggested changelog entry

Add backslash into double backslash replacement for Windows.

Related issues/external references

PHP Sniffer extension for VSCode generates warnings on Windows without this fix.

Types of changes

PR checklist

jrfnl commented 2 weeks ago

@arturm11 Thank you for this PR.

I've been able to reproduce the issue with PHPCS itself via the command line using something along the lines of:

phpcs --ignore=D:\path\to\PHP_CodeSniffer\src\Tokenizers\CSS.php

which results in a:

Warning: preg_match(): Compilation failed: unknown property after \P or \p at offset * in D:\path\to\PHP_CodeSniffer\src\Filters\Filter.php on line 278

And the change made in this PR fixes this. :+1:

Before I can accept the PR, could you please have a look at the third checklist item in the "PR checklist" ?

I'd also like to see this change safeguarded via tests. There are some tests for the Filters in the tests/Core/Filters directory already. These could probably be expanded.

arturm11 commented 2 weeks ago

@jrfnl Now I am not sure. Please analyze Ignoring Files and Folders section in the wiki. It is stated, that filter can be treated as regular expression. Backslash character is special character for RE. Therefore Windows paths shouldn't be accepted and backslash should be converted to slash in Windows path on input.

arturm11 commented 1 week ago

There is related bug in vscode-php-sniffer, so I close this request. https://github.com/wongjn/vscode-php-sniffer/pull/73

jrfnl commented 1 week ago

@arturm11 Thanks for getting back to me. Hope that bug gets fixed soon.