1.安装php_codesniffer:
composer global require "squizlabs/php_codesniffer=*"
阿里云Composer全量镜像:https://developer.aliyun.com/composer
2.配置phpcs和phpcbf命令全局可执行:
执行完上一步操作,phpcs和phpcbf会默认安装到~/.composer/vendor/bin/目录下。为了使phpcs和phpcbf两个命令可以全局被执行,我们可以建立软链接或者配置环境变量,此处我采用软连接的方式:
ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin/phpcs
ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin/phpcbf
注:phpcs是用于检测不符合代码规范的代码,并发出警告或报错(可设置报错等级)用的,
phpcbf是用于自动修正代码格式上不符合规范的部分
3.复制以下内容到项目中的/.git/hooks/pre-commit(直接新建pre-commit文件)下,chmod +x {project_root}/.git/hooks/pre-commit。在提交代码时,不符合PSR2标准的文件将会报错。
#!/bin/bash
# PHP CodeSniffer pre-commit hook for git
#
# @author Soenke Ruempler <soenke@ruempler.eu>
# @author Sebastian Kaspari <s.kaspari@googlemail.com>
#
# see the README
PHPCS_BIN=/usr/local/bin/phpcs
#PHPCS_CODING_STANDARD=PEAR
PHPCS_CODING_STANDARD=PSR2
PHPCS_IGNORE=
PHPCS_IGNORE_WARNINGS=1
TMP_STAGING=".tmp_staging"
# parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
# simple check if code sniffer is set up correctly
if [ ! -x $PHPCS_BIN ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
exit 1
fi
# stolen from template file
if git rev-parse --verify HEAD
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# this is the magic:
# retrieve all files in staging area that are added, modified or renamed
# but no deletions etc
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- )
if [ "$FILES" == "" ]; then
exit 0
fi
# create temporary copy of staging area
if [ -e $TMP_STAGING ]; then
rm -rf $TMP_STAGING
fi
mkdir $TMP_STAGING
# match files against whitelist
FILES_TO_CHECK=""
for FILE in $FILES
do
echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
RETVAL=$?
if [ "$RETVAL" -eq "0" ]
then
FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
fi
done
if [ "$FILES_TO_CHECK" == "" ]; then
exit 0
fi
# execute the code sniffer
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
fi
if [ "$PHPCS_SNIFFS" != "" ]; then
SNIFFS="--sniffs=$PHPCS_SNIFFS"
else
SNIFFS=""
fi
if [ "$PHPCS_ENCODING" != "" ]; then
ENCODING="--encoding=$PHPCS_ENCODING"
else
ENCODING=""
fi
if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
IGNORE_WARNINGS="-n"
else
IGNORE_WARNINGS=""
fi
# Copy contents of staged version of files to temporary staging area
# because we only want the staged version that will be commited and not
# the version in the working directory
STAGED_FILES=""
for FILE in $FILES_TO_CHECK
do
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
# create staged version of file in temporary staging area with the same
# path as the original file so that the phpcs ignore filters can be applied
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
git cat-file blob $ID > "$TMP_STAGING/$FILE"
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
done
OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS $STAGED_FILES)
RETVAL=$?
# delete temporary copy of staging area
rm -rf $TMP_STAGING
if [ $RETVAL -ne 0 ]; then
echo "$OUTPUT" | less
fi
exit $RETVAL
4.PhpStorm中集成phpcs和phpcbf:
(1)配置Code Sniffer
在Preferences -> Languages & Frameworks -> PHP -> Code Sniffer 中对 phpcs 进行配置。
点击 Configuration: Local 旁边的 ...,将当前环境的 phpcs 执行脚本所在路径配置进去,旁边有一个 Validate 按钮可以进行验证,其他两个参数默认就好。
(2)开启phpcs验证
在Preferences -> Editor -> Inspections 中进行开启。找到 PHP -> Quality tools -> PHP Code Sniffer validation 选项,对其打钩,在右侧进行详细配置
我是将 Options -> Show warnings as: Warnning 开启了,当然你也可以选择其他的,提示级别高一点能够强迫自己。
Coding standard 依旧是选择 PSR2,如果找不到这个选项,点一下紧挨着的刷新按钮。
(3)配置phpcbf自动修复
在Preferences-> Tools -> External Tools 中进行添加,下面是我的一个示例,基本可以照搬:
Parameter | Value |
---|---|
Name | phpcbf |
Description | Automatically correct coding standard violations. |
Program | /usr/local/bin/phpcbf |
Arguments | / |
Wroking directory |
现在,点击某个文件,就可以在菜单栏上的 Tools -> External Tools 中找到它并愉快的使用了:)
当然,如果你习惯了使用快捷键的方式,你也可以配置快捷键:
在Preferences -> Keymap -> External Tools -> phpcbf 中进行添加快捷键操作,我设置的是 Option + F,或者 Windows 上的 Alt + F。
5.一些命令:
# 查看配置
$ phpcs -i
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend
# 设置编码标准为 PSR2
$ phpcs --config-set default_standard PSR2
# 隐藏警告提醒
$ phpcs --config-set show_warnings 0
# 开启警告提醒
$ phpcs --config-set show_warnings 1
# 显示颜色
$ phpcs --config-set colors 1
# 关闭颜色显示
$ phpcs --config-set colors 0
修改错误和警告等级
# 显示所有的错误和警告
$ phpcs --config-set severity 1
# 显示所有的错误,部分警告 注意等级可有从 5-8 5 的警告显示会更多,8 的更少
$ phpcs --config-set severity 1
$ phpcs --config-set warning_severity 5
设置默认编码
# 设置 utf-8
$ phpcs --config-set encoding utf-8
参考文章:
【1】https://git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-Git-%E9%92%A9%E5%AD%90
【2】https://segmentfault.com/a/1190000011338132
【3】https://www.cnblogs.com/liluxiang/p/9498926.html