开发规范
PSR-4 基础目录使用小写,其它依次使用大驼峰法命名,例如。
其中 composer 配置
"autoload": {
"psr-4": {
"App\\" : "application/app",
"Admin\\" : "application/admin",
"Common\\" : "common"
}
}
不存在类文件,请使用小写目录,其文件也一样:
/data/codes/queryphp/option/
/data/codes/queryphp/option/app.php
统一代码风格
在使用前您需要安装 ,这样子才能够进行下面的工作。
可以通过下面的方式来格式化代码风格:
$cd /data/codes/queryphp
$php-cs-fixer fix --config=.php_cs.dist
/data/codes/queryphp/build/pre-commit.sh
/data/codes/queryphp/vendor/hunzhiwange/framework/build/pre-commit.sh
应用脚本 /data/codes/queryphp/build/pre-commit.sh
核心包脚本 /data/codes/queryphp/build/pre-commit.sh
#!/bin/bash
#
# check PHP code syntax error and standard with phpcs
# https://blog.csdn.net/xsgnzb/article/details/52222366?locationNum=4&fps=1
# https://blog.csdn.net/ljihe/article/details/80826071
# =================== how to use ====================
# git commit -h
# ==================== end ==========================
PROJECT=$(git rev-parse --show-toplevel)
cd $PROJECT
SFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
# Determine if a file list is passed
if [ "$#" -ne 0 ]
then
exit 0
fi
echo "Checking PHP Lint..."
for FILE in $SFILES
do
php -l -d display_errors=0 $FILE
if [ $? != 0 ]
then
echo "Fix the php error before commit."
exit 1
fi
FILES="$FILES $PROJECT/$FILE"
done
then
echo "Running Code Sniffer..."
isCheck=""
for FILE in $SFILES
do
result=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`
if [ "$result" != "" ]
then
echo $result
isCheck=$result
git add $FILE
fi
done
if [ "$isCheck" != "" ]
then
echo "The file has been automatically formatted."
fi
fi
git update-index -g
exit $?
上述脚本就会自动运行帮助你格式化代码,你也可以忽略脚本。
这样子我们再也不需要浪费时间在无意义的代码风格的讨论上了。