2

I am attempting to configure my phpcs.xml (PHP CodeSniffer configuration file) so that I can exclude all directories from inspection within a folder except for those that I specify.

For those familiar with .gitignore, an equivalent would be something like this so that "ButIncludeThisOne" is included in version control.

/ignoreContentsWithin/*
!/ignoreContentsWithin/ButIncludeThisOne

What I have tried

Here is my phpcs.xml file:

<?xml version="1.0"?>
<ruleset name="MyRuleset">
    ...
    <!-- Exclude Plugin Folders. -->
    <exclude-pattern>/cms/wp-content/plugins/*</exclude-pattern>
    <!-- But Inspect My Plugin Folder. -->
    <include-pattern>/cms/wp-content/plugins/myplugin/*</include-pattern>
</ruleset>

Using the example above, I can specify every single folder within the plugins folder except for myplugin and this works, but it is not ideal. (I would have to remember to exclude any new plugin from inspection)

If I remove the exclude-pattern directive, files within the myplugin folder are sniffed, so I know that it is working otherwise.

Phil Birnie
  • 1,104
  • 2
  • 12
  • 29

1 Answers1

2

TLTR; for your specific example:

<exclude-pattern type="relative">^(?!cms/wp-content/plugins/myplugin).+</exclude-pattern>

You can use regex in there but * will be used as .*

Check out this regex with negative lookahead. The type="relative" is important here.

<exclude-pattern type="relative">^(?!plugins/(arve-|advanced-re|edd-)).+</exclude-pattern>
redanimalwar
  • 1,229
  • 1
  • 12
  • 32
  • Thanks for the answer - gave it a test and still can't verify that this works. Maybe I'm having trouble understanding the type="relative" attribute/can't find anything useful in the documentation. Is this relative to the configuration file? – Phil Birnie Feb 09 '21 at 20:31
  • Yes its relative to the config file, and it works without slash prefix. – redanimalwar Feb 10 '21 at 22:04