php文件检查工具,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一个例子

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"

>

phpmd.phar ./application html ./phpmd.xml --exclude application/cache,vendor > phpmd.html

编辑器插件

参考文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值