Search code examples
phpregexphpcodesnifferphpcs

How to exclude everything except some directories from PHP Code Sniffer inspections in .phpcs.xml?


I would like to exclude everything except some directories from PHP Code Sniffer inspection in my project. Something similar like git repo exclusions with .gitignore

I had the idea that regex with negative lookahead could be used with <exclude-pattern>. I tried:

<exclude-pattern>^(?!wp-content\/plugins\/customplugin\/*$|wp-content\/themes\/customtheme\/*$).+</exclude-pattern>

Consequently, I expected PHPCS to only scan the wp-content/plugins/customplugin/ and wp-content/themes/customtheme/. Unfortunately it doesn't work:

Registering sniffs in the Project standard... DONE (138 sniffs registered)
Creating file list... DONE (0 files in queue)

How to achieve this in the right way?


Solution

  • To achieve the intended goal, we must modify the regular expression:

    <exclude-pattern>^(?!.*\/wp-content\/plugins\/customplugin|.*\/wp-content\/themes\/customtheme).*</exclude-pattern>
    

    Note that it may be possible to resolve this issue in a different way in the future thanks to this ticket in the CodeSniffer repository