php - 如何忽略PHPMD中特定目录的特定规则

标签 php phpmd

命令

phpmd app,config,routes text phpmd.xml
我在 Laravel 项目上使用 phpmd,有一条规则叫 CouplingBetweenObjects在design.xml下,我想忽略app/Http/Controllers目录下的所有文件到这个规则,phpmd只提供--exclude选项将使目录通过 所有 规则,是否可以通过 XML 文件或命令行忽略特定目录的规则
phpmd.xml
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Netask rule set"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Netask code style rule set
</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveParameterList"/>
<rule ref="rulesets/codesize.xml/ExcessivePublicCount">
    <properties>
        <property name="minimum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyFields">
    <properties>
        <property name="maxfields" value="20"/>
    </properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
    <properties>
        <property name="maximum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/controversial.xml"/>
<rule ref="rulesets/design.xml"/>
<rule ref="rulesets/naming.xml/ShortVariable"/>
<rule ref="rulesets/naming.xml/LongVariable">
    <properties>
        <property name="maximum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">
    <properties>
        <property name="minimum" value="2"/>
    </properties>
</rule>
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass"/>
<rule ref="rulesets/naming.xml/ConstantNamingConventions"/>
<rule ref="rulesets/naming.xml/BooleanGetMethodName"/>
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" />

<exclude-pattern>app/Console/Kernel.php</exclude-pattern>
<exclude-pattern>app/Services/Service.php</exclude-pattern>
<exclude-pattern>tests/TestCase.php</exclude-pattern>
</ruleset>

最佳答案

我建议以稍微不同的方式查看它,即您想忽略特定文件中的特定 PHPMD 规则,PHPMD 通过注释中的注释支持该规则。
例如

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
https://phpmd.org/documentation/suppress-warnings.html
我认为这是使用 PHPMD 的惯用方式。如果这是不可能的,您可以为运行 phpmd 的新命令添加别名。两次(在命令之间使用 ||)并在第二个规则集 xml 中排除不需要的规则。

关于php - 如何忽略PHPMD中特定目录的特定规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62806765/

相关文章:

PHPMD @bin_dir@ 错误

PHP API访问多次调用

php - php中的搜索框与mysql

php - sql 更新查询未运行

php - 无法打开流 : HTTP wrapper does not support writeable connections in Wp-Content(0755)

PHPMD 无法带参数运行

php - 在 php 中的 n 行之后重复 html 表头

php - 如何编写具有项目范围上下文而不是仅在类级别的 PHP Mess Detector 规则?

php - else 永远不是必需的,您可以简化代码以在没有 else 的情况下工作

PHP 困惑检测器给出误报