首页 > 解决方案 > 使用 PHP CodeSniffer 检查排除文件夹的子目录

问题描述

我正在尝试配置我的phpcs.xml(PHP CodeSniffer 配置文件),以便我可以将文件夹中的所有目录排除在检查之外,但我指定的目录除外。

对于熟悉 的人来说.gitignore,等价物是这样的,因此“ButIncludeThisOne”包含在版本控制中。

/ignoreContentsWithin/*
!/ignoreContentsWithin/ButIncludeThisOne

我试过的

这是我的phpcs.xml文件:

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

使用上面的示例,我可以指定 plugins 文件夹中的每个文件夹,可以使用myplugin,但这并不理想。(我必须记住从检查中排除任何新插件)

如果我删除该exclude-pattern指令,文件myplugin夹中的文件会被嗅探,所以我知道它正在工作。

标签: codesnifferphpcsphpcodesniffer

解决方案


TLTR; 对于您的具体示例:

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

您可以在那里使用正则表达式,但*将用作.*

看看这个带有负前瞻的正则表达式。这里type="relative"很重要。

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

推荐阅读