💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# PHPMD(PHP Mess Detector)规则集 ## 规则集列表 - [Clean Code Rules](#1): 强化代码整洁度的规则集,来自[SOLID](#solid)原则和“[对象健身操](#object_calisthenics)”原则。 - [Code Size Rules](#2): 代码尺寸规则集. - [Controversial Rules](#3): 有争议的代码规则. - [Design Rules](#4): 软件设计的相关问题规则集. - [Naming Rules](#5): 名称太长,规则太短,等等规则集. - [Unused Code Rules](#6): 找到未使用的代码的规则集. ## Clean Code Rules<span id="1"></span> - [BooleanArgumentFlag](http://phpmd.org/rules/cleancode.html#booleanargumentflag): 布尔参数是违反单一责任原则 (SRP) 的可靠指标。你可以通过提取这个标志中的逻辑到其自己的类或方法来解决此问题。 - [ElseExpression](http://phpmd.org/rules/cleancode.html#elseexpression): if表达式中的else分支并不是必须的。你可以重写条件,去掉else,这样代码更简单。你可能在代码中就要使用return,或者把代码分成几个小的方法。对于更为简单的情况,可以直接用三元表达式。 - [StaticAccess](http://phpmd.org/rules/cleancode.html#staticaccess): 静态访问(Static acccess)导致对其他类无法改变的依赖关系,而且难以测试代码。应该想方设法避免静态访问。你可以通过构造器注入依赖的方式,代替静态访问。只有在使用工厂方法的时候,静态访问才是合适的。 ## Code Size Rules<span id="2"></span> - [CyclomaticComplexity](http://phpmd.org/rules/codesize.html#cyclomaticcomplexity):复杂性是由方法内关键点的个数加1来确定的。关键点有'if','while','for'和 'case labels'。一般来说,1-4复杂度低,5-7是适度的复杂性,8-10是高度复杂的,而11+有很高的复杂性。 - [NPathComplexity](http://phpmd.org/rules/codesize.html#npathcomplexity):一个方法的NPath复杂性是通过其中非循环执行路径的数量来确定的。200被认为是必须降低复杂性的阀值。 - [ExcessiveMethodLength](http://phpmd.org/rules/codesize.html#excessivemethodlength): 违反此规则通常表明该方法做的太多了。通过创建辅助方法和消除任何复制/粘贴代码的形式尽量减小方法体。 - [ExcessiveClassLength](http://phpmd.org/rules/codesize.html#excessiveclasslength):过长的类文件表名它试图做的操作太多。可以通过拆分和减小类所管理的对象来重组它。 - [ExcessiveParameterList](http://phpmd.org/rules/codesize.html#excessiveparameterlist):长参数列表表明一个新的对象需要被创建以包容众多参数。基本上,可以尝试分组参数。 - [ExcessivePublicCount](http://phpmd.org/rules/codesize.html#excessivepubliccount):类中有大量的公共方法和属性声明表明这个类需要阻断其增长,并进行彻底的测试。 - [TooManyFields](http://phpmd.org/rules/codesize.html#toomanyfields): 有太多字段的类可以考虑重新设计(可能是将一些信息打包为嵌套对象的方法)来减少字段。比如,类中的city/state/zip字段可以合并成一个Address字段。 - [TooManyMethods](http://phpmd.org/rules/codesize.html#toomanymethods): 一个类有太多的方法,将其重构是最好的选择,以减少其复杂性和找到更细粒度的对象。 - [ExcessiveClassComplexity](http://phpmd.org/rules/codesize.html#excessiveclasscomplexity):确定一个类进行修改和维护需要多少时间和精力时,加权方法计数(WMC:The Weighted Method Count)是一个很好的指标。WMC定义为在类中声明的所有方法复杂度的总和。大量的方法也意味着这个类对其派生类有更大的潜在影响。 ## Controversial Rules<span id="3"></span> - [Superglobals](http://phpmd.org/rules/controversial.html#superglobals):直接访问超级全局变量是一个很差的做法。举个例子,这些变量应该被封装在由框架提供的对象里。 - [CamelCaseClassName](http://phpmd.org/rules/controversial.html#camelcaseclassname):最好使用CamelCase标记法命名类。 - [CamelCasePropertyName](http://phpmd.org/rules/controversial.html#camelcasepropertyname): 最好使用CamelCase标记法命名属性。 - [CamelCaseMethodName](http://phpmd.org/rules/controversial.html#camelcasemethodname):最好使用CamelCase标记法命名方法。 - [CamelCaseParameterName](http://phpmd.org/rules/controversial.html#camelcaseparametername):最好使用CamelCase标记法命名参数。 - [CamelCaseVariableName](http://phpmd.org/rules/controversial.html#camelcasevariablename):最好使用CamelCase标记法命名变量。 ## Design Rules<span id="4"></span> - [ExitExpression](http://phpmd.org/rules/design.html#exitexpression): 正常代码里存在exit-expression就会变得不可测试,所以要避免。可以将exit-expression转化为某种类型的启动脚本,其中错误/异常代码被返回到调用环境。 - [EvalExpression](http://phpmd.org/rules/design.html#evalexpression): eval-expression是不可测试的,有安全风险,而且是一种坏实践。我们要避免它。应该用符合常规的代码替代它。 - [GotoStatement](http://phpmd.org/rules/design.html#gotostatement):Goto使代码难以阅读,无法了解应用程序的控制流结构。因此必须避免。可以用常规的控制结构和分离的方法/函数代替。 - [NumberOfChildren](http://phpmd.org/rules/design.html#numberofchildren):一个类拥有过多的子类,是不平衡的类层次结构的一个指标。此时应该对这个类的层次结构进行重构。 - [DepthOfInheritance](http://phpmd.org/rules/design.html#depthofinheritance):一个类的继承深度过多,是不平衡的类层次结构的一个指标。此时应该对这类层次结构进行重构。 - [CouplingBetweenObjects](http://phpmd.org/rules/design.html#couplingbetweenobjects):一个类存在过多的外部依赖会对其产生几个质量方面的负面影响:如稳定性、可维护性、可理解性。 ## Naming Rules<span id="5"></span> - [ShortVariable](http://phpmd.org/rules/naming.html#shortvariable):字段、局部变量或参数定义名称过短。 - [LongVariable](http://phpmd.org/rules/naming.html#longvariable):字段、局部变量定义名称过长。 - [ShortMethodName](http://phpmd.org/rules/naming.html#shortmethodname): 方法名过短。 - [ConstructorWithNameAsEnclosingClass](http://phpmd.org/rules/naming.html#constructorwithnameasenclosingclass):构造函数方法不应该和封装的类有相同的名称,应参照PHP 5 __construct方法来命名。 - [ConstantNamingConventions](http://phpmd.org/rules/naming.html#constantnamingconventions):类/接口常量必须用大写字母定义。 - [BooleanGetMethodName](http://phpmd.org/rules/naming.html#booleangetmethodname):返回值为布尔型而以'getX()'格式命名的方法。习惯上此类方法的命名格式是 'isX()' 或者 'hasX()'。 ## Unused Code Rules<span id="6"></span> - [UnusedPrivateField](http://phpmd.org/rules/unusedcode.html#unusedprivatefield):存在已声明或赋值的私有字段,但是却未使用。 - [UnusedLocalVariable](http://phpmd.org/rules/unusedcode.html#unusedlocalvariable): 存在已声明或赋值的局部变量,但是却未使用。 - [UnusedPrivateMethod](http://phpmd.org/rules/unusedcode.html#unusedprivatemethod):存在已声明但未使用的私有方法。 - [UnusedFormalParameter](http://phpmd.org/rules/unusedcode.html#unusedformalparameter): 避免将参数传递给方法或构造函数,然后不使用这些参数。 ## Remark This document is based on a ruleset xml-file, that was taken from the original source of the [PMD](http://pmd.sourceforge.net/) project. This means that most parts of the content on this page are the intellectual work of the PMD community and its contributors and not of the PHPMD project. ## 译注 - <dfn id="solid">SOLID原则:</dfn>面向对象5个基本原则,参考http://www.cnblogs.com/shanyou/archive/2009/09/21/1570716.html - <dfn id="object_calisthenics">对象健身操:</dfn>即object calisthenics,由ThoughtWorks提出的一种设计规则集,参考http://blog.csdn.net/chgaowei/article/details/5456201