PHPMD - include a whole ruleset and configure the properties

1.7k views Asked by At

I am using the PHPMD (http://phpmd.org/) and I am quite new to this. The MD works, I am now writing a ruleset to configure what metrics should be used. Instead of including each rule individually, I load the whole rulesets. But now I have the problem that I don't know how to configure the properties of single rules if I include the whole set.

For example, I want to use the rule to check the cyclomatic complexity. I can use

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

But if I want to use all rules from that ruleset, I can simply write

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

Now how can I use the configuration of the property (in my case the reportLevel for cyclomatic complexity) when I include the whole ruleset? I tried something like

[...]
    <rule ref="rulesets/codesize.xml">
        <properties>
            <property name="CyclomaticComplexity.reportLevel" value="11" />
        </properties>
    </rule>
[...]

But that didn't work. I searched in the documentation but never found an example for this anywhere.

1

There are 1 answers

1
Alban On

The only way I have found to achieve this is using the exclude element, including all rules from the ruleset except the one you want to customize, and then including it separately.

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