Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore "id" in Short Variable rule using PMD

Tags:

java

eclipse

pmd

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?

like image 457
SanderDN Avatar asked Dec 23 '13 10:12

SanderDN


2 Answers

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'))
like image 170
SanderDN Avatar answered Oct 18 '22 16:10

SanderDN


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>
like image 39
Simon Avatar answered Oct 18 '22 17:10

Simon