- 开发规范
- 文件和目录
- 统一代码风格
- 手工优化
- 结合
GitHooks 来格式化代码: - Git Commit
开发规范
QueryPHP 遵循 PSR-2 命名规范和 PSR-4 自动加载规范。
文件和目录
PSR-4 基础目录使用小写,其它依次使用大驼峰法命名,例如。
/data/codes/queryphp/application/app/Domain/Entity//data/codes/queryphp/application/app/Domain/Entity/Test.php
其中 composer 配置
"autoload": {"psr-4": {"App\\" : "application/app","Admin\\" : "application/admin","Common\\" : "common"}}
不存在类文件,请使用小写目录,其文件也一样:
/data/codes/queryphp/option//data/codes/queryphp/option/app.php
统一代码风格
为了屏蔽不同用户的不同代码风格习惯,QueryPHP 设置一个统一的代码格式化配置来规范团队的代码风格,这符合 PSR-2 规范并且可以通过 StyleCI 规范。
手工优化
在使用前您需要安装 php-cs-fixer,这样子才能够进行下面的工作。
/data/codes/queryphp/.php_cs.dist # 应用/data/codes/queryphp/vendor/hunzhiwange/framework/.php_cs.dist # 框架核心包
可以通过下面的方式来格式化代码风格:
$cd /data/codes/queryphp$php-cs-fixer fix --config=.php_cs.dist
结合 Git Hooks 来格式化代码:
/data/codes/queryphp/build/pre-commit.sh/data/codes/queryphp/vendor/hunzhiwange/framework/build/pre-commit.sh
应用脚本 /data/codes/queryphp/build/pre-commit.sh
这里脚本也包含一段 JS 的脚本,这个用于格式化 QueryPHP 的通用前端后台的 JS 代码风格,跟 PHP 差不多。
#!/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 ====================# ln -s pre-commit.sh ./../.git/hooks/pre-commit# git commit -h# git commit -n -m 'pass hook' #bypass pre-commit and commit-msg hooks# ==================== end ==========================PROJECT=$(git rev-parse --show-toplevel)cd $PROJECTSFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)# Determine if a file list is passedif [ "$#" -ne 0 ]thenexit 0fiecho "Checking PHP Lint..."for FILE in $SFILESdophp -l -d display_errors=0 $FILEif [ $? != 0 ]thenecho "Fix the php error before commit."exit 1fiFILES="$FILES $PROJECT/$FILE"done# format code styleif [ "$FILES" != "" ]thenecho "Running Code Sniffer..."isCheck=""for FILE in $SFILESdoresult=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`if [ "$result" != "" ]thenecho $resultisCheck=$resultgit add $FILEfidoneif [ "$isCheck" != "" ]thenecho "The file has been automatically formatted."fifi# for jsjsfiles=$(git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.vue" "*.css" "*.less" | tr '\n' ' ')[ -z "$jsfiles" ] && exit 0# Prettify all staged .js filesecho "$jsfiles" | xargs ./frontend/node_modules/.bin/prettier --config frontend/.prettierrc.js --ignore-path frontend/.prettierignore --write# Add back the modified/prettified files to stagingecho "$jsfiles" | xargs git addgit update-index -gexit $?
核心包脚本 /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 ====================# ln -s pre-commit.sh ./../.git/hooks/pre-commit# git commit -h# git commit -n -m 'pass hook' #bypass pre-commit and commit-msg hooks# ==================== end ==========================PROJECT=$(git rev-parse --show-toplevel)cd $PROJECTSFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)# Determine if a file list is passedif [ "$#" -ne 0 ]thenexit 0fiecho "Checking PHP Lint..."for FILE in $SFILESdophp -l -d display_errors=0 $FILEif [ $? != 0 ]thenecho "Fix the php error before commit."exit 1fiFILES="$FILES $PROJECT/$FILE"done# format code styleif [ "$FILES" != "" ]thenecho "Running Code Sniffer..."isCheck=""for FILE in $SFILESdoresult=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`if [ "$result" != "" ]thenecho $resultisCheck=$resultgit add $FILEfidoneif [ "$isCheck" != "" ]thenecho "The file has been automatically formatted."fifigit update-index -gexit $?
Git Commit
git commit -m 'pass hook'
上述脚本就会自动运行帮助你格式化代码,你也可以忽略脚本。
git commit -n -m 'pass hook'
这样子我们再也不需要浪费时间在无意义的代码风格的讨论上了。
