PHP基本要求
- 版本: php7.4 开发环境, 不强制,推荐使用phpstorm,
- 或者vscode。但请不要将ide配置上传到代码库。(如:.idea) 需要安装pre-
- commit来做提交前代码检查。
- PHP编码规范
PHP编码规范请参考:PHP-FIG
安装php_CodeSniffer:
composer global require "squizlabs/php_codesniffer=*"
安装提交前pre-commit
检查PSR-2的代码规范的pre-commit hook(项目目录下的.git/hooks创建pre-commit插入一下代码):
#!/bin/sh
# @source: https://gist.github.com/ronanguilloux/11f6a788358577474ab4
# @link http://tech.zumba.com/2014/04/14/control-code-quality/
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
oIFS=$IFS
IFS='
'
SFILES="$1"
IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}
echo "Checking PHP Lint..."
for FILE in $SFILES
do
php -l -d display_errors=0 $PROJECT/$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..."
phpcs --standard=PSR2 $PROJECT/$FILE -n
#bin/php-cs-fixer fix . --config=sf23 --dry-run -vv
if [ $? != 0 ]
then
echo "Fix the error before commit."
exit 1
fi
fi
exit $?
环境变量配置 (~/.zshrc)
配置文件生效
source ~/.zshrc