命令
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/