11

I am using PMD plugin (version 4.0.2) for Eclipse (Eclipse Kepler Java EE). I have configured a naming rule: ShortVariable.

This works fine except for parameters like "id" and "e". I want PMD to ignore these. So I searched for a way to ignore certain parameters. I found this link (although it's for phpmd) and tried it, yet I can't seem to get it working. My config file looks like this (XML):

<?xml version="1.0"?>
<ruleset name="My PMD ruleset"
 xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>
        My PMD
    </description>
    <rule ref="rulesets/java/naming.xml/ShortVariable">
        <property name="exceptions" value="id" />
    </rule>
</ruleset>

When I try to import this ruleset using the eclipse plugin, it shows no possible rules to import. Any ideas?

SanderDN
  • 496
  • 6
  • 19
  • [Found a solution](http://zavyn.blogspot.be/2011/09/solution-modify-pmds-shortvariable-rule.html) (maybe not the greatest) after a bit of searching. – SanderDN Dec 23 '13 at 11:55
  • 1
    Your linked solution is very good! Please post it as an answer and accept it after the grace period. Thanks. – barfuin Dec 23 '13 at 13:34

3 Answers3

16

I found a solution to my problem here.

The resulting xml looks like this:

<?xml version="1.0"?>
<ruleset name="My PMD ruleset"
 xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>
        My PMD
    </description>
    <rule ref="rulesets/java/naming.xml/ShortVariable">
        <properties>
            <property name="xpath">
                <value>
                    //VariableDeclaratorId[(string-length(@Image) &lt; 3) and (not (@Image='id'))]
                    [not(ancestor::ForInit)]
                    [not((ancestor::FormalParameter) and (ancestor::TryStatement))]
                </value>
            </property>
        </properties>
    </rule>
</ruleset>

To be able to ignore more variable names, repeat the following part:

and (not (@Image='myVariableToIgnore'))
SanderDN
  • 496
  • 6
  • 19
3

The folowing XML is valid for PHP tool PHPMD 2.2.3

<?xml version="1.0"?>
<!DOCTYPE ruleset>
<ruleset
    name="My PMD ruleset for symfony 2.5"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"
>

    <rule ref="rulesets/unusedcode.xml" />
    <rule ref="rulesets/codesize.xml" />
    <rule ref="rulesets/cleancode.xml" />
    <rule ref="rulesets/controversial.xml" />
    <rule ref="rulesets/design.xml" />
    <rule ref="rulesets/naming.xml">
       <exclude name="ShortVariable" />
    </rule>
    <rule ref="rulesets/naming.xml/ShortVariable">
        <properties>
            <property name="exceptions" value="id,em" />
        </properties>
    </rule>
</ruleset>
Simon
  • 43
  • 6
  • 1
    Not working with PMD 5.1.3: java.lang.IllegalArgumentException: Cannot set non-existant property 'exceptions' on Rule ShortVariable – SanderDN Jan 20 '15 at 12:29
  • Sorry, I'm using PHPMD 2.2.3, I didn't realize it was Eclipse (JAVA World) question. – Simon Aug 12 '15 at 07:09
0

Update xml

 <rule ref="category/java/codestyle.xml/ShortVariable">
    <properties>
        <property name="xpath">
            <value>
                //VariableDeclaratorId[(string-length(@Image) &lt; 3) and (not (@Name='id'))]
                [not(ancestor::ForInit)]
                [not((ancestor::FormalParameter) and (ancestor::TryStatement))]
            </value>
        </property>
    </properties>
</rule>