代码之家  ›  专栏  ›  技术社区  ›  Mark Baker

在phpmd中设置规则的异常

  •  3
  • Mark Baker  · 技术社区  · 8 年前

    我试图在phpmd中为规则设置一个例外,以允许一个特定函数名的长度仅为2个字符

    上一个 phpmd.xml.dist file与以下人员合作非常愉快:

    <?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>
    

    我为添加了一个条目 rulesets/naming.xml/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,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>
    

    但现在我在尝试运行它时遇到以下错误:

    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版本为2.6.0

    2 回复  |  直到 8 年前
        1
  •  2
  •   Mark Baker    8 年前

    我找到了答案:编写单独的排除元素,而不是逗号分隔的列表,并确保我有正确的规则名称( ShortMethodName 而不仅仅是 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>
    
        2
  •  1
  •   hemali savaliya    7 年前

    我们还可以创建phpmd自定义规则集,而无需添加其他规则集

    <?xml version="1.0"?>
    <ruleset name="My first PHPMD rule set">
    
        <description>
           My custom rule set that checks my code...
        </description>
        <pmd version="@project.version@" timestamp="2017-09-29T20:00:17+00:00">
         <!-- Import the entire unused code rule set -->
             <rule ref="PHPMD/Rule/CyclomaticComplexity" />
             <rule ref="PHPMD/Rule/Design/NPathComplexity"/>
       </pmd>
    

    无论您想设置什么规则,都可以在usr/share/PHPMD/rule上找到,然后可以在终端上启动命令

    phpmd sourcefile.php xml codesize,phpmdruleset.xml
    
    推荐文章