-
实现:
php-cs可以用来检测代码是否符合PSR-2规范,同时支持对不符合规范的代码自动格式化,让其转成PSR-2的编码风格。
-
安装:
- 安装composer
php-cs依赖于composer,所以笔者需要先安装composer,安装的方法有很多种,这里提供mac操作系统和手动安装方法
brew install composer
手动安装composer命令为:
wget https://getcomposer.org/download/1.7.1/composer.phar && chmod 777 composer.phar && mv composer.phar /usr/local/bin/composer
- 安装PHP-CS
安装好composer之后,可以用composer快速安装php-cs,安装命令如下
composer global require "squizlabs/php_codesniffer=*"
当命令执行完成之后,会在笔者当前用户的主目录下创建一个 .composer 目录,在目录中包含了需要的php-cs,此时可以执行下方命令来验证是否安装成功
~/.composer/vendor/bin/phpcs --help
当命令执行后,如果能看到下方的一些信息,那么就代表安装成功
- Check STDIN instead of local files and directories
-n Do not print warnings (shortcut for --warning-severity=0)
-w Print both warnings and errors (this is the default)
-l Local directory only, no recursion
-s Show sniff codes in all reports
-a Run interactively
-e Explain a standard by showing the sniffs it includes
-p Show progress of the run
-q Quiet mode; disables progress and verbose output
-m Stop error messages from being recorded
(saves a lot of memory, but stops many reports from being used)
-v Print processed files
-vv Print ruleset and token output
-vvv Print sniff processing information
-i Show a list of installed coding standards
-d Set the [key] php.ini value to [value] or [true] if value is omitted
- 全局使用
路径加入到全局中,加入的命令如下
ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin/phpcs
ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin/phpcbf
当执行完成之后,可以使用短命令来验证是否加入全局成功,可以用下方的命令
phpcs --help
执行成功之后,返回结果应该和上方完整路径返回的一致。
- 设置默认标准
phpcs默认的编码格式并不是php-cs,所以当不指定标准的时候,检测的结果并不准确,但每次都手动指定也挺麻烦,所以可以设置一个默认标准,命令如下:
phpcs --config-set default_standard PSR2
phpcbf --config-set default_standard PSR2
- PHPCS检测
现在可以用phpcs来真实的试验了,笔者先准备一个PHP文件,文件里面的内容如下代码示例,可以看出这份代码并不符合PSR-2的风格规范
<?php
function test_test(){
echo 'daxia';
}
test();
通过PHP-CS检测编码风格,命令如下
phpcs /Users/mycode/test.php
命令执行完成之后,可以看到如下代码提示,在提示中能看到具体哪一行,提示级别,以及具体的提示原因
FILE: /Users/mycode/test.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | WARNING | [ ] A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is
| | defined on line 3 and the first side effect is on line 8.
3 | ERROR | [x] Opening brace should be on a new line
8 | ERROR | [x] Expected 1 newline at end of file; 0 found
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Time: 79ms; Memory: 4Mb
- PHPCBF格式化
自动格式化编码风格命令如下
phpcbf /Users/mycode/test.php
命令执行完成之后,可以看到如下返回提示,处理了哪一些文件,以及类型
PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE FIXED REMAINING
----------------------------------------------------------------------
/Users/mycode/test.php 2 1
----------------------------------------------------------------------
A TOTAL OF 2 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------
Time: 68ms; Memory: 4Mb
再次使用PHP-CS检测
phpcs /Users/mycode/test.php
执行完成之后,通过命令再次查看结果
FILE: /Users/mycode/test.php
----------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------------------
1 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute
| | logic with side effects, but should not do both. The first symbol is defined on line 3 and the first side effect is on line 9.
----------------------------------------------------------------------------------------------------------------------------------------------
Time: 71ms; Memory: 4Mb
能看到最开始检测有三次不合格,但现在只剩下一处了;这里说一下为什么phpcbf没有帮完全处理呢,因为phpcbf只能处理代码风格等方式,而不能帮你处理里面的命名与代码实现规则,所以有少部分还需要人为去更正,但并不会太多
-
GIT配置:
当前面一切准备就绪,就可以在git钩子里面增加强制的策略了,git钩子脚本存放于项目下 .git/hooks/ 文件夹下,按照下面的步骤来添加一个commit事件。
在你的项目根目录下,使用vim命令或其他方式,新增一个文件 ./.git/hooks/pre-commit,然后把下面的脚本放进去,之后再保存。
#!/bin/sh
PHPCS_BIN=/usr/local/bin/phpcs
PHPCS_CODING_STANDARD=PSR2
PHPCS_FILE_PATTERN="\.(php)$"
FILES=$(git diff HEAD^..HEAD --stat)
if [ "$FILES" == "" ]; then
exit 0
fi
for FILE in $FILES
do
echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
RETVAL=$?
if [ "$RETVAL" -eq "0" ]
then
PHPCS_OUTPUT=$($PHPCS_BIN --standard=$PHPCS_CODING_STANDARD $FILE)
PHPCS_RETVAL=$?
if [ $PHPCS_RETVAL -ne 0 ];
then
echo $PHPCS_OUTPUT
exit 1
fi
fi
done
exit 0
需要注意的是让这个文件有可执行权限,最直接的办法就是设置为777,参考命令如下:
chmod 777 .git/hooks/pre-commit
让php代码风格不一致,然后使用git commit来提交,看看git是否会阻止提交,以下面这份代码为例
<?php
function test_test(){
echo 'test';
}
test();
可以很明显的看出来,这份代码没有按照驼峰命名法,大括号也没用换行的两处问题;把它保存在根目录名为test.php文件,然后执行git commit命令,如下
git add test.php && git commit . -m 'test'
命令执行后,git返回了如下信息,便终止了
FILE: /Users/mycode/test.php
----------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
----------------------------------------------------------------------------------------------------------------------------------------------
1 | WARNING | [ ] A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should
| | execute logic with side effects, but should not do both. The first symbol is defined on line 3 and the first side effect
| | is on line 8.
3 | ERROR | [x] Opening brace should be on a new line
8 | ERROR | [x] Expected 1 newline at end of file; 0 found
----------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------------------------------------------
Time: 63ms; Memory: 4Mb
验证一下git是否commit成功,可以执行下面的命令:
git status
返回结果如下
位于分支 develop
您的分支与上游分支 'origin/develop' 一致。
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
新文件: test.php
说明前面的命令只成功执行了 git add . 而后面commit则成功阻挡了。然后使用PHPCBF进行自动格式化代码。
本文转自:tangqingsong 使用PHPCS+GIT钩子保障团队开发中代码风格一致性实践