squizlabs / PHP_CodeSniffer

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

Feature request: Exclude all folders but not X #3208

Closed nextgenthemes closed 3 years ago

nextgenthemes commented 3 years ago

I just found this SO question

<?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>

If I assume wrong based on the fact that it is 2 years old and not answered than let me know how to do this. Otherwise, please treat this as a feature request because this is exactly my case (Also with WordPress plugins, but I was not the one who opened this question)

jrfnl commented 3 years ago

@nextgenthemes Your question is a little unclear.

If all you want to scan is the /cms/wp-content/plugins/myplugin/ folder, use:

<ruleset>
    <file>/cms/wp-content/plugins/myplugin/</file>
</ruleset>

... and PHPCS will only scan that folder.

If you have several plugin folders you want to scan, you can add additional <file> directives, like so:

<ruleset>
    <file>/cms/wp-content/plugins/pluginA/</file>
    <file>/cms/wp-content/plugins/pluginB/</file>
</ruleset>

If you're talking about include/exclude patterns for specific rules, remember that <[include|exclude]-pattern>s are PCRE regular expressions, so you can use regex syntax with one caveat: a * will automatically be replaced with .*.

Also see the documentation:

nextgenthemes commented 3 years ago

I do not get how this is unclear. But let me copy and paste this from the SO question as well. I want to pretty much do exactly what this on .gitignore would do.

    /ignoreContentsWithin/*
    !/ignoreContentsWithin/ButIncludeThisOne
    !/ignoreContentsWithin/ButIncludeThisOneAsWell

Well I have not though of using <file> but I would end up having to add every file, or I want to check in the root, folder. Admittedly there are not much, but I use file <file>.</file> and would like to keep it and not have to add each file manually what I want may add later, and for sure will forget.

I was unaware that I can use a regex in there. Seems I need to come up with one and that will solve this ...

nextgenthemes commented 3 years ago

OK I got it https://regex101.com/r/cCiXqA/1/

The type="relative" was important.

<exclude-pattern type="relative">^(?!plugins/(arve-|advanced-re|edd-)).+</exclude-pattern>
jrfnl commented 3 years ago

Yes, your question was (and still is) unclear as you're piecemeal providing additional information which wasn't included in the original question.

You are now talking about adding files from the root folder, which wasn't mentioned in the original question.

As I already said above, if you literally want this:

    /ignoreContentsWithin/*
    !/ignoreContentsWithin/ButIncludeThisOne
    !/ignoreContentsWithin/ButIncludeThisOneAsWell

Use:

<ruleset>
    <file>/ignoreContentsWithin/ButIncludeThisOne/</file>
    <file>/ignoreContentsWithin/ButIncludeThisOneAsWell/</file>
</ruleset>

The <file> directive allows for adding directories which will be scanned recursively. No need to add each individual file, except when you don't want to scan the whole directory.

Either way, as you closed the issue, I presume you got it working with the regex.

nextgenthemes commented 3 years ago

How is this still unclear to you? No your example does not literally do what that .gitignore would do. It tracks all files by default and then builds excludes on that.

I had a link at the very top to this "piecemeal" (new word to me). And I also already explained above <file> was not a good fit for this. I understood you the first time around when you suggest file.

    /ignoreContentsWithin/*
    !/ignoreContentsWithin/ButIncludeThisOne
    !/ignoreContentsWithin/ButIncludeThisOneAsWell

You do not use this in .gitignore if you not want to track files in the root or any other folders at all, obviously.

The same way I want files in the root tracked I want all .php files checked with PHPCS, actually not even just in root, possibly files in /themes/ or wherever (and again) without having to add every single new file or folder to the config. You get it now?

Anyway thanks regex is a bit more complicated than how .gitignore works but gets the job done. I finally answered that SO question as well.

jrfnl commented 3 years ago

Oh, I get it. But there was no way to give you a good answer to your question without you adding all those assumptions you made which you only just laid out in your last comment.... But never mind, you got it working and that's all that matters.

fabswt commented 2 years ago

I still don't get it.

I'm trying to achieve something similar to the OP:

    <exclude-pattern>*/blog/*</exclude-pattern>
    <include-pattern>*/blog/themes/bilingueanglais/*</include-pattern>

...but the include-pattern is without effect.

As a work around, I ended up using this:

    <exclude-pattern>*/blog/cache/*</exclude-pattern>
    <exclude-pattern>*/blog/plugins/*</exclude-pattern>
    <exclude-pattern>*/blog/uploads/*</exclude-pattern>
    <exclude-pattern>*/blog/themes/twentyseventeen/*</exclude-pattern>
    <include-pattern>*/blog/themes/bilingueanglais/*</include-pattern>

...which works, but is not elegant and requires more maintenance (anytime a folder is added to blog/, I'll have to add a rule for it, though I'd prefer for it to be excluded by default.)