做问卷调查有哪些网站好,昆明响应式网站,自动化项目外包网,python基础教程怎么样前言
使用PHPCSGIT钩子保障团队开发中代码风格一致性实践
使用PHPMD提高代码质量与可读性
0.介绍 PHP_CodeSniffer php代码嗅探器 包含phpcs(php code standard 代码标准) phpcbf(php code beautify fix 代码美化修复) 是一个代码风格检测工具,着重代码规范 它包含两类脚本…前言
使用PHPCSGIT钩子保障团队开发中代码风格一致性实践
使用PHPMD提高代码质量与可读性
0.介绍 PHP_CodeSniffer php代码嗅探器 包含phpcs(php code standard 代码标准) phpcbf(php code beautify fix 代码美化修复) 是一个代码风格检测工具,着重代码规范 它包含两类脚本phpcs 和 phpcbf
1.安装
composer global require squizlabs/php_codesniffer*
2.验证是否安装成功并查看帮助
phpcs --help 3.使用
phpcs path/file.php 4.集成到git 4.1 新增钩子文件 在 .git\hooks\目录下执行下面的命令
cp pre-commit.sample pre-commit
修改其中内容为
#!/bin/bash## check PHP code syntax error and standard with phpcs# author : star[github.com/star1989]# date : 2017-02-24PROJECT$(git rev-parse --show-toplevel)cd $PROJECTSFILES$(git diff --cached --name-only --diff-filterACMR HEAD | grep \\.php)TMP_DIR$PROJECT./tmp# Determine if a file list is passedif [ $# -ne 0 ]thenexit 0fiecho Checking PHP Lint...for FILE in $SFILESdo# echo php -l -d display_errors0 ${FILE}# echo git show :$FILE $TMP_DIR/$FILEphp -l -d display_errors0 $FILEif [ $? ! 0 ]thenecho Fix the error before commit.exit 1fiFILES$FILES $PROJECT/$FILEdoneif [ $FILES ! ]thenecho Running Code Sniffer...TMP_DIR/tmp/$(uuidgen)mkdir -p $TMP_DIRfor FILE in $SFILESdomkdir -p $TMP_DIR/$(dirname $FILE)git show :$FILE $TMP_DIR/$FILEdonephpcs --standardPSR2 --encodingutf-8 -n $TMP_DIRPHPCS_ERROR$?rm -rf $TMP_DIRif [ $PHPCS_ERROR ! 0 ]thenecho Fix the error before commit.exit 1fifiexit $?
5.在git下使用(git触发检测)
执行git commit 后会自动检测待提交代码的格式 6.自定义phpcs规则
有些情况我们需要忽略一些规则或者添加一些自定义的规则比如有些类不需要命名空间迁移类不希望在检测代码时抛出该类型错误 6.1 添加标准
$ phpcs --config-set installed_paths ruleset.xml
6.2 编辑规则内容
将ruleset.xml放置在项目根目录下并写入具体规则
?xml version1.0?ruleset nameCustomStandard!-- 代码标准为PSR2 --rule refPSR2exclude namePSR1.Classes.ClassDeclaration.MissingNamespace//rule/ruleset
6.3 修改pre-commit检测方式
将其中的
phpcs --standardPSR2 --encodingutf-8 -n $TMP_DIR
改为
phpcs --standardruleset.xml -s --encodingutf-8 -n $TMP_DIR
这样就可以跳过命名空间的检测了快去试试吧
7.常用命令
检查单个文件phpcs /path/to/code 检查目录下的文件phpcs /path/to/code/ 查看已经安装的标准phpcs -i 设置默认检查标准phpcs --config-set default_standard /path/to/standard_file 查看配置phpcs --config-show 指定报告格式phpcs --reportsummary /path/to/code 可用的报告格式有full, xml, checkstyle, csv, json, emacs, source, summary, diff, svnblame, gitblame, hgblame, notifysend默认为full 查看帮助phpcs -h 自动修复phpcbf /path/to/code
8.phpmd 介绍 PHP Mess Detector PHP混乱探测器 是一个代码质量检测工具,着重代码质量
9.安装
composer global require phpmd/phpmd
10.使用
$phpmd path\code text codesize,unusedcode,naming,design参数说明
# 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
11.添加到git钩子
donephpcs --standardruleset.xml -s --encodingutf-8 -n $TMP_DIRphpmd $TMP_DIR text codesize,unusedcode,naming,design https://www.jianshu.com/p/d3f8c4b32719 Windows系统下使用PHPCSPHPMDGIT钩子 - 互联网笔记