PHPMD - enable entire ruleset and set properties

I am using PHPMD ( http://phpmd.org/ ) and I am completely new to this. MD is working, now I am writing a set of rules to tune which metrics to use. Instead of including each rule separately, I load all the rulesets. But now I have a problem that I don't know how to set the properties of individual rules if I include the whole set.

For example, I want to use a rule to test for cyclical 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 the rules from this rule set, I can just 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 property configuration (in my case reportLevel for cyclical complexity) when I include the entire ruleset? I tried something like

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

      

But it didn't work. I have searched in the documentation but couldn't find an example for this anywhere.

+3


source to share


1 answer


The only way I've found to do this is to use the exclude element, include all the rules from the ruleset except the ones you want to configure, and then include them 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>

      

+4


source







All Articles