ObliviousHarmony / vscode-php-codesniffer

A VS Code extension for integrating PHP_CodeSniffer.
https://marketplace.visualstudio.com/items?itemName=obliviousharmony.vscode-php-codesniffer
Other
40 stars 2 forks source link

The extension does not honor exceptions from `.phpcs.xml` #37

Closed kanlukasz closed 2 years ago

kanlukasz commented 2 years ago

Description

In my .phpcs.xml I use <exclude-pattern> using negative regex. Thanks to this, I am able to exclude the entire project except specific folders. (This is very useful especially when you are working with a strange project structure like e.g. Wordpress)

When I run PHP Code Sniffer from the command line, all my files are interpreted correctly - I mean my exclusions works fine. But when i use it in VsCode with your extension, every file is scanned while open

Reproduction Steps

  1. Create settings in Workspace:
    "phpCodeSniffer.executable": "./vendor/bin/phpcs",
    "phpCodeSniffer.standard": "Custom",
    "phpCodeSniffer.standardCustom": "./.phpcs.xml",
  2. Create config file .phpcs.xml with <exclude-pattern>
  3. Open any file

My .phpcs.xml file:

<?xml version="1.0"?>
<ruleset name="xxxxxxx">
    <exclude-pattern type="relative">^(?!wp-content/plugins/xxxxxxxxx|wp-content/themes/xxxxxxxxx).+</exclude-pattern>
    <exclude-pattern type="relative">^(wp-content/themes/xxxxxxxx/inc/*.*)</exclude-pattern>
    <exclude-pattern type="relative">^(?!.*.php).+</exclude-pattern>
    <rule ref="WordPress">
        <exclude name="WordPress.WhiteSpace.ControlStructureSpacing"/>
        <exclude name="WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter"/>
        <exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
        <exclude name="Squiz.Commenting.FileComment.Missing"/>
        <exclude name="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace"/>
        <exclude name="Generic.WhiteSpace.ArbitraryParenthesesSpacing.SpaceAfterOpen"/>
        <exclude name="Generic.WhiteSpace.ArbitraryParenthesesSpacing.SpaceBeforeClose"/>
        <exclude name="WordPress.Arrays.ArrayDeclarationSpacing.NoSpaceAfterArrayOpener"/>
        <exclude name="WordPress.Arrays.ArrayDeclarationSpacing.NoSpaceBeforeArrayCloser"/>
    </rule>
    <rule ref="PEAR.Functions.FunctionCallSignature">
        <properties>
            <property name="requiredSpacesAfterOpen" value="0"/>
            <property name="requiredSpacesBeforeClose" value="0"/>
        </properties>
    </rule>
</ruleset>

Work environment:

ObliviousHarmony commented 2 years ago

Hey @kanlukasz,

When you run PHPCS against a specific file, your exclude-pattern shouldn't work. You've declared the path as relative, but for specific files (instead of directories), there's nothing for the pattern to be relative to. You're checking /path/to/wordpress/wp-content/plugins/content/file.php against a regex that uses a ^ and indicates that the string should begin with something. Since it can't use a relative path (as-per PHPCS itself), it fails to find anything meaningful in the regex.

You should see a similar effect if you ran phpcs /path/to/wordpress/wp-content/plugins/content/file.php.

kanlukasz commented 2 years ago

Thank you for your quick reply and detailed explanations. Yes, I corrected my regex and now works fine directly with cmd and with your extension in VsCode too. Sorry for the confusion 🙏

I am leaving the corrected regex here for others who looking for a similar case:

<exclude-pattern>^(?!.*\/wp-content\/plugins\/my-plugin-dir|.*\/wp-content\/themes\/my-theme-dir).*</exclude-pattern>
<exclude-pattern>^(.*\/wp-content\/themes\/my-theme-dir\/inc)</exclude-pattern>
<exclude-pattern>.*\.(?!(php$))</exclude-pattern>