前言
使用PHPCS+GIT钩子保障团队开发中代码风格一致性实践
使用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-24
PROJECT=$(git rev-parse --show-toplevel)
cd $PROJECT
SFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
TMP_DIR=$PROJECT."/tmp"
# Determine if a file list is passed
if [ "$#" -ne 0 ]
then
    exit 0
fi
echo "Checking PHP Lint..."
for FILE in $SFILES
do
#    echo "php -l -d display_errors=0 ${FILE}"
#  echo "git show :$FILE > $TMP_DIR/$FILE"
    php -l -d display_errors=0 $FILE
    if [ $? != 0  ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
    FILES="$FILES $PROJECT/$FILE"
done
if [ "$FILES" != "" ]
then
    echo "Running Code Sniffer..."
    TMP_DIR=/tmp/$(uuidgen)
    mkdir -p $TMP_DIR
    for FILE in $SFILES
    do
        mkdir -p $TMP_DIR/$(dirname $FILE)
        git show :$FILE > $TMP_DIR/$FILE
    done
    phpcs --standard=PSR2 --encoding=utf-8 -n $TMP_DIR
    PHPCS_ERROR=$?
    rm -rf $TMP_DIR
    if [ $PHPCS_ERROR != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
fi
exit $?5.在git下使用(git触发检测)
执行git commit 后会自动检测待提交代码的格式

6.自定义phpcs规则
有些情况我们需要忽略一些规则或者添加一些自定义的规则,比如有些类不需要命名空间(迁移类),不希望在检测代码时抛出该类型错误

6.1 添加标准
$ phpcs --config-set installed_paths ruleset.xml6.2 编辑规则内容
将ruleset.xml放置在项目根目录下,并写入具体规则
<?xml version="1.0"?>
<ruleset name="CustomStandard">
  <!-- 代码标准为PSR2 -->
<rule ref="PSR2">
    <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
</rule>
</ruleset>6.3 修改pre-commit检测方式
将其中的
phpcs --standard=PSR2 --encoding=utf-8 -n $TMP_DIR改为
phpcs --standard=ruleset.xml -s --encoding=utf-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 --report=summary /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/phpmd10.使用
$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.xml11.添加到git钩子
done
    phpcs --standard=ruleset.xml -s --encoding=utf-8 -n $TMP_DIR
    phpmd $TMP_DIR text codesize,unusedcode,naming,design