mi-schi/phpmd-extension

包含来自《代码整洁之道》和我的经验最佳实践的额外phpmd规则。

4.3.0 2018-08-15 09:09 UTC

README

GitHub license Build Status Code Coverage Scrutinizer Code Quality Github All Releases

特性

包含来自《代码整洁之道》和我的经验最佳实践的额外phpmd规则。

安装

下载 phpmd-extension.phar

$ curl -OsL https://github.com/mi-schi/phpmd-extension/releases/download/stable/phpmd-extension.phar

或者可以使用 tooly-composer-script 进行安装。

也可以使用composer(不推荐,见 #5

composer require mi-schi/phpmd-extension --dev

用法

  1. 创建一个 phpmd.xml 文件并导入phpmd的基本规则。下面的例子包含了一些有用的更改。之后,你可以使用此仓库中的规则扩展配置。
  2. 然后执行消息检测,使用phpmd-extension.phar [path/to/src] xml [path/to/phpmd.xml]phpmd-extension.phar将所有参数传递给基本的phpmd命令。您无需安装phpmd。phpmd-extension.phar包含了phpmd。

基本规则

<ruleset name="basic-rules"
         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">
    <description>mess detection</description>

    <rule ref="rulesets/cleancode.xml" />
    <rule ref="rulesets/codesize.xml">
        <exclude name="ExcessiveParameterList" />
        <exclude name="ExcessiveMethodLength" />
        <exclude name="ExcessiveClassLength" />
        <exclude name="CyclomaticComplexity" />
    </rule>
    <rule ref="rulesets/codesize.xml/ExcessiveParameterList">
        <properties>
            <property name="minimum" value="4" />
        </properties>
    </rule>
    <rule ref="rulesets/codesize.xml/ExcessiveMethodLength">
        <properties>
            <property name="minimum" value="31" />
            <property name="ignore-whitespace" value="true" />
        </properties>
    </rule>
    <rule ref="rulesets/codesize.xml/ExcessiveClassLength">
        <properties>
            <property name="minimum" value="301" />
            <property name="ignore-whitespace" value="true" />
        </properties>
    </rule>
    <rule ref="rulesets/codesize.xml/CyclomaticComplexity">
        <properties>
            <property name="reportLevel" value="6" />
            <property name="showClassesComplexity" value="true" />
            <property name="showMethodsComplexity" value="true" />
        </properties>
    </rule>
    <rule ref="rulesets/controversial.xml" />
    <rule ref="rulesets/design.xml" />
    <rule ref="rulesets/naming.xml">
        <exclude name="ShortVariable" />
        <exclude name="LongVariable" />
    </rule>
    <rule ref="rulesets/naming.xml/ShortVariable">
        <properties>
            <property name="minimum" value="2" />
        </properties>
    </rule>
    <rule ref="rulesets/naming.xml/LongVariable">
        <properties>
            <property name="maximum" value="30" />
        </properties>
    </rule>
    <rule ref="rulesets/unusedcode.xml" />
</ruleset>

添加额外规则

    <rule ref="../../../../../../rulesets/cleancode.xml" />
    <rule ref="../../../../../../rulesets/naming.xml" />
    <rule ref="../../../../../../rulesets/test.xml" />

您也可以使用自己的属性自定义规则或只使用特定规则。只需查看xml文件。它遵循基本规则集的逻辑。

排除或自定义额外规则

如果您不希望排除Law-of-Demeter规则MemberPrimaryPrefix,请排除它。

    <rule ref="../../../../../../rulesets/cleancode.xml">
        <exclude name="MemberPrimaryPrefix" />
    </rule>

如果您想自定义它,请使用以下代码

    <rule ref="../../../../../../rulesets/cleancode.xml/MemberPrimaryPrefix">
    	<properties>
	    <property name="maxChainCount" value="3" description="max count of method chains" />
            <property name="allowedPrefixes" value="add,set" description="allowed prefixes for the method train" />
            <property name="delimiter" value="," description="delimiter for explode" />
        </properties>
    </rule>