PHPMD - 包括整个规则集并配置属性

A K*_*A K 3 php phpmd

我正在使用PHPMD(http://phpmd.org/),我对此很新.MD工作,我现在正在编写一个规则集来配置应该使用的指标.我没有单独包含每个规则,而是加载整个规则集.但是现在我遇到的问题是,如果我包含整个集合,我不知道如何配置单个规则的属性.

例如,我想使用该规则来检查圈复杂度.我可以用

<?xml version="1.0"?>
<ruleset name="Demo PHPMD 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> custom ruleset that checks the code </description> 
    <rule ref="rulesets/codesize.xml/CyclomaticComplexity">
        <properties>
            <property name="reportLevel" value="11" />
        </properties>
    </rule>
</ruleset>
Run Code Online (Sandbox Code Playgroud)

但是,如果我想使用该规则集中的所有规则,我可以简单地写

<?xml version="1.0"?>
<ruleset name="Demo PHPMD 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> custom ruleset that checks the code </description> 
    <rule ref="rulesets/codesize.xml" />
</ruleset>
Run Code Online (Sandbox Code Playgroud)

现在,当我包含整个规则集时,如何使用属性的配置(在我的情况下,reportLevel用于圈复杂度)?我试过类似的东西

[...]
    <rule ref="rulesets/codesize.xml">
        <properties>
            <property name="CyclomaticComplexity.reportLevel" value="11" />
        </properties>
    </rule>
[...]
Run Code Online (Sandbox Code Playgroud)

但那没用.我搜索了文档,但从未在任何地方找到过这样的例子.

小智 5

我发现实现此目的的唯一方法是使用exclude元素,包括规则集中除要自定义的规则之外的所有规则,然后单独包含它.

<?xml version="1.0"?>
<ruleset name="Demo PHPMD 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> custom ruleset that checks the code </description>
    <rule ref="rulesets/codesize.xml">
        <exclude name="CyclomaticComplexity"/>
    </rule> 
    <rule ref="rulesets/codesize.xml/CyclomaticComplexity">
        <properties>
            <property name="reportLevel" value="11" />
        </properties>
    </rule>
</ruleset>
Run Code Online (Sandbox Code Playgroud)