php质量检查工具 phpmd的使用

PHPMD简介

团队在使用php-cs-fixer 代码格式自动式化工具之后,在格式,代码错误等方面带来了很大便利,不过在命名,代码质量,代码复杂度,缺少一些检查,在网上搜索后,发现PHPMD 一个PHP代码静态分析工具.

安装

官方网站 github
你可以直接到下载页面封装好的 phar 包:phpmd.phar

unix:

wget http://static.phpmd.org/php/latest/phpmd.phar -O phpmd.phar
chmod a+x phpmd.phar
mv phpmd.phar /usr/local/bin/phpmd.phar

windows

下载phpmd.phar
把phpmd.phar 放入php目录,然后把php安装目录加入系统PATH变量

composer

composer global require phpmd/phpmd

使用

/usr/local/bin/phpmd.phar

Mandatory arguments:
1) A php source code filename or directory. Can be a comma-separated string
2) A report format
3) A ruleset filename or a comma-separated string of rulesetfilenames

Available formats: xml, text, html.
Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.

Optional arguments that may be put after the mandatory arguments:
--minimumpriority: rule priority threshold; rules with lower priority than this will not be used
--reportfile: send report output to a file; default to STDOUT
--suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml
--exclude: comma-separated string of patterns that are used to ignore directories
--strict: also report those nodes with a @SuppressWarnings annotation
--ignore-violations-on-exit: will exit with a zero code, even if any violations are found

发现命令行有三个参数

# phpmd 源代码路径 报告的格式 规则列表
# 源代码路径 支持
   一个文件 /path/to/file
   一个目录 /path/to/source 
# 报告的格式 支持 
    xml:以XML格式输出;
    text:简单的文本格式;
    html:输出到单个的html;
# 规则列表 支持
    phpmd_ruleset.xml 文件格式
    codesize,unusedcode,naming 单个命令集合
# 附加参数
  --exclude - 忽略的目录,以逗号分隔多个目录。
# 例子
phpmd /path/to/source html ./phpmd_ruleset.xml

规则集合列表:rules

Clean Code Rules: 强化代码整洁度的规则集。
Code Size Rules: 代码尺寸规则集.
Controversial Rules: 有争议的代码规则.
Design Rules: 软件设计的相关问题规则集.
Naming Rules: 名称太长,规则太短,等等规则集.
Unused Code Rules: 找到未使用的代码的规则集.

更多规则 中文参考手册

具体实例

一般在团队开发项目中,需要根据团队规范制定不同的规则文件,所以在项目根目录添加一个 phpmd.xml 规则文件后,之后自动部署的时候会运行脚本检查项目中的php代码。 下面是phpmd.xml一个例子

<?xml version="1.0"?>
<ruleset name="customer/php"
         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/codesize.xml/TooManyFields" />
    <!--- 一个类中不能有太多fields字段,默认值15 -->
    <rule ref="rulesets/codesize.xml/NPathComplexity" />
    <!--- 一个方法中NPath复杂性,默认值200 -->
    <rule ref="rulesets/codesize.xml/ExcessiveMethodLength" />
    <!--- 相同的方法执行同一功能,默认值100 -->
    <rule ref="rulesets/codesize.xml/ExcessiveClassLength" />
    <!--- 一个类的最大行数,默认值1000 -->
    <rule ref="rulesets/codesize.xml/TooManyMethods" />
    <!--- 一个类的最大方法数,默认值25 -->
    <rule ref="rulesets/controversial.xml/CamelCaseClassName" />
    <!--- 以驼峰式命名类 -->
    <rule ref="rulesets/controversial.xml/CamelCaseParameterName" />
    <!--- 以驼峰式命名参数名字 -->
    <rule ref="rulesets/controversial.xml/CamelCaseVariableName" />
    <!--- 以驼峰式命名变量名字 -->
    <rule ref="rulesets/design.xml/DevelopmentCodeFragment" />
    <!--  代码中不要有var_dump(), print_r()等这种调试输出结构 -->
    <rule ref="rulesets/design.xml/EvalExpression" />
    <!--  代码中不要有eval()这种结构 -->
    <rule ref="rulesets/design.xml/GotoStatement" />
    <!--  代码中不要有goto这种结构 -->
    <rule ref="rulesets/naming.xml/BooleanGetMethodName" />
    <!--  返回值为布尔型而以'getX()'格式命名的方法改写成'isX()' 或者 'hasX()'-->
    <rule ref="rulesets/naming.xml/ConstantNamingConventions" />
    <!--  类/接口常量必须用大写字母定义-->
	<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" />
    <!--   存在已声明或赋值的局部变量,但是却未使用-->
    <rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
    <!--  存在已声明或赋值的局部变量,但是却未使用-->
</ruleset>
phpmd.phar ./application html ./phpmd.xml --exclude application/cache,vendor > phpmd.html

编辑器插件

参考文档

PHPMD官网
phpmd中文手册
面向对象5个基本原则 phpmd-extension

转载于:https://my.oschina.net/2688/blog/909269

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值