Search code examples
phpphpmd

Setting exceptions to a rule in phpmd


I'm trying to set an exception to a rule in phpmd, to allow one specific function name to be just 2 characters in length

The previous phpmd.xml.dist file was working quite happily with:

<?xml version="1.0" encoding="UTF-8" ?>
<ruleset name="Complex PHP Mess Detector 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">
    <rule ref="rulesets/naming.xml">
        <exclude name="LongVariable" />
    </rule>
    <rule ref="rulesets/naming.xml/LongVariable">
        <properties>
            <property name="maximum" value="32" />
        </properties>
    </rule>
    <rule ref="rulesets/design.xml/ExitExpression" />
    <rule ref="rulesets/design.xml/EvalExpression" />
    <rule ref="rulesets/design.xml/GotoStatement" />
    <rule ref="rulesets/design.xml/DepthOfInheritance" />
    <rule ref="rulesets/design.xml/CouplingBetweenObjects" />
    <rule ref="rulesets/design.xml/NumberOfChildren" />
    <rule ref="rulesets/unusedcode.xml" />
    <rule ref="rulesets/controversial.xml" />
</ruleset>

I've added an entry for rulesets/naming.xml/ShortMethod, so it now looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<ruleset name="Complex PHP Mess Detector 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">
    <rule ref="rulesets/naming.xml">
        <exclude name="LongVariable,ShortMethod" />
    </rule>
    <rule ref="rulesets/naming.xml/LongVariable">
        <properties>
            <property name="maximum" value="32" />
        </properties>
    </rule>
    <rule ref="rulesets/naming.xml/ShortMethod">
        <properties>
            <property name="minimum" value="3" />
            <property name="exceptions" value="ln" />
        </properties>
    </rule>
    <rule ref="rulesets/design.xml/ExitExpression" />
    <rule ref="rulesets/design.xml/EvalExpression" />
    <rule ref="rulesets/design.xml/GotoStatement" />
    <rule ref="rulesets/design.xml/DepthOfInheritance" />
    <rule ref="rulesets/design.xml/CouplingBetweenObjects" />
    <rule ref="rulesets/design.xml/NumberOfChildren" />
    <rule ref="rulesets/unusedcode.xml" />
    <rule ref="rulesets/controversial.xml" />
</ruleset>

but now I'm getting the following error when I try to run it:

Catchable fatal error: Argument 1 passed to PHPMD\RuleSetFactory::parsePropertiesNode() must implement interface PHPMD\Rule, null given,
called in phar://Utilities/PHP/phpmd.phar/src/main/php/PHPMD/RuleSetFactory.php on line 462
and defined in phar://Utilities/PHP/phpmd.phar/src/main/php/PHPMD/RuleSetFactory.php on line 489

phpmd is version 2.6.0


Solution

  • Well I found my answer: write individual exclude elements rather than a comma-separated list, and ensure that I have the correct rule name (ShortMethodName rather than simply ShortMethod):

    <?xml version="1.0" encoding="UTF-8" ?>
    <ruleset name="Complex PHP Mess Detector 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">
        <rule ref="rulesets/naming.xml">
            <exclude name="LongVariable" />
            <exclude name="ShortMethodName" />
        </rule>
        <rule ref="rulesets/naming.xml/LongVariable">
            <properties>
                <property name="maximum" value="32" />
            </properties>
        </rule>
        <rule ref="rulesets/naming.xml/ShortMethodName">
            <properties>
                <property name="minimum" value="3" />
                <property name="exceptions" value="ln" />
            </properties>
        </rule>
        <rule ref="rulesets/design.xml/ExitExpression" />
        <rule ref="rulesets/design.xml/EvalExpression" />
        <rule ref="rulesets/design.xml/GotoStatement" />
        <rule ref="rulesets/design.xml/DepthOfInheritance" />
        <rule ref="rulesets/design.xml/CouplingBetweenObjects" />
        <rule ref="rulesets/design.xml/NumberOfChildren" />
        <rule ref="rulesets/unusedcode.xml" />
        <rule ref="rulesets/controversial.xml" />
    </ruleset>