php - 在 phpmd 中设置规则的异常(exception)

标签 php phpmd

我正在尝试为 phpmd 中的规则设置一个异常(exception),以允许一个特定函数名称的长度仅为 2 个字符

之前的 phpmd.xml.dist 文件运行得非常愉快:

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

最佳答案

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

关于php - 在 phpmd 中设置规则的异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48523086/

相关文章:

php - 何时使用 call_user_func_array

php - 如何用 PHP 删除空值?

php - 汇总百分比所需的算法帮助

php - 是否可以将所有 PHP 困惑检测器检查替换为 PHP 代码嗅探器检查?

php - 即使代码工作正常也会出现未定义索引错误

PHPMD 表示对于具有 bool 默认值的参数违反了单一责任原则

php - 避免使用静态访问异常

PHPMD - 包含整个规则集并配置属性

PhpStorm - 如何检测 PHP 错误 "Non-static method should not be called statically"?

php - Laravel - 根据 Laravel 中相似的 id 对记录进行分组