acanda / eclipse-pmd

eclipse-pmd has been moved to
https://github.com/eclipse-pmd/eclipse-pmd
Other
21 stars 10 forks source link

Eclipse PDT. Cannot change PMD nature of project <projectName> #49

Closed vasilake-v closed 7 years ago

vasilake-v commented 7 years ago

I can't enable and use PMD for my php project in Eclipse PDT.

Error StackTrace is attached.

My ruleset.xml file:

<?xml version="1.0"?>
<ruleset name="Braces"
    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>
    The Braces ruleset contains rules regarding the use and placement of braces.
    </description> 
    <rule name="IfStmtsMustUseBraces" language="php"
        message="The Braces ruleset contains rules regarding the use and placement of braces."
        class="net.sourceforge.pmd.lang.rule.XPathRule">
        <description>
        Avoid using if 123 statements without using braces to surround the code
         block. If the code formatting or indentation is lost then it becomes difficult 
        to separate the code being controlled from the rest.
        </description>
        <priority>3</priority> 
        <properties>
            <property name="xpath">
                <value>
<![CDATA[
//IfStatement[count(*) < 3][not(Statement/Block)]
]]>
                </value>
            </property>
        </properties>

        <example>
<![CDATA[
class Foo {

    public function test(){
        if(bool==1)
            x++;

        if (bool==1){
            x++;
        }
    }

}
]]>
        </example>

    </rule>
    <rule name="WhileLoopsMustUseBraces"
        language="php"
        since="0.7"
        message="Avoid using 'while' statements without curly braces"
        class="net.sourceforge.pmd.lang.rule.XPathRule"
        externalInfoUrl="">
        <description>
        Avoid using 'while' statements without using braces to surround the code 
        block. If the code formatting or indentation is lost then it becomes difficult
        to separate the code being controlled from the rest.
        </description>
        <priority>3</priority>
        <properties>
            <property name="xpath">
                <value>
<![CDATA[
//WhileStatement[not(Statement/Block)]
]]>
                </value>
            </property>
        </properties>
        <example>
<![CDATA[
while (true)    // not recommended
      x++;

while (true) {  // preferred approach
      x++;
}
]]>
        </example>
        </rule>
</ruleset>
[stacktrace.txt](https://github.com/pmd/pmd/files/816944/stacktrace.txt)

Session Data

eclipse.buildId=4.6.2.M20161124-1400
java.version=1.8.0_91
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.php.product -product org.eclipse.epp.package.php.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.php.product -data file:/C:/Users/veaceslav/workspace2/ -product org.eclipse.epp.package.php.product
acanda commented 7 years ago

The project must be a Java project (i.e. have the Java nature). This is a leftover from when PMD was Java only. It's probably time to cut this dependency. You can try to add the Java nature to your project.

Just out of curiosity: does that rule work? PMD claims that it supports only CPD for PHP.

vasilake-v commented 7 years ago

I guess you're right about PMD support for PHP.

I added Java nature to project. And tried to re-Apply PMD rules to project, but I got an exception saying:

Errors occurred during the build.
Errors running builder 'PMD Builder' on project 'mytestproject'.
java.lang.IllegalArgumentException: Language does not have a RuleChainVisitor: LanguageModule:PHP: Hypertext Preprocessor(PhpLanguageModule)

Judging by Laguage module source code net.sourceforge.pmd.lang.php.PhpLanguageModule, I see that php language is defined, but has no RuleChainVisitor class (as error says ) compared to java language module.

That's unfortunate... I could've used the XPath rules to keep my code organized

Thanks for help