开发规范

    PSR-4 基础目录使用小写,其它依次使用大驼峰法命名,例如。

    其中 composer 配置

    1. "autoload": {
    2. "psr-4": {
    3. "App\\" : "application/app",
    4. "Admin\\" : "application/admin",
    5. "Common\\" : "common"
    6. }
    7. }

    不存在类文件,请使用小写目录,其文件也一样:

    1. /data/codes/queryphp/option/
    2. /data/codes/queryphp/option/app.php

    统一代码风格

    在使用前您需要安装 ,这样子才能够进行下面的工作。

    可以通过下面的方式来格式化代码风格:

    1. $cd /data/codes/queryphp
    2. $php-cs-fixer fix --config=.php_cs.dist
    1. /data/codes/queryphp/build/pre-commit.sh
    2. /data/codes/queryphp/vendor/hunzhiwange/framework/build/pre-commit.sh

    应用脚本 /data/codes/queryphp/build/pre-commit.sh

    核心包脚本 /data/codes/queryphp/build/pre-commit.sh

    1. #!/bin/bash
    2. #
    3. # check PHP code syntax error and standard with phpcs
    4. # https://blog.csdn.net/xsgnzb/article/details/52222366?locationNum=4&fps=1
    5. # https://blog.csdn.net/ljihe/article/details/80826071
    6. # =================== how to use ====================
    7. # git commit -h
    8. # ==================== end ==========================
    9. PROJECT=$(git rev-parse --show-toplevel)
    10. cd $PROJECT
    11. SFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
    12. # Determine if a file list is passed
    13. if [ "$#" -ne 0 ]
    14. then
    15. exit 0
    16. fi
    17. echo "Checking PHP Lint..."
    18. for FILE in $SFILES
    19. do
    20. php -l -d display_errors=0 $FILE
    21. if [ $? != 0 ]
    22. then
    23. echo "Fix the php error before commit."
    24. exit 1
    25. fi
    26. FILES="$FILES $PROJECT/$FILE"
    27. done
    28. then
    29. echo "Running Code Sniffer..."
    30. isCheck=""
    31. for FILE in $SFILES
    32. do
    33. result=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`
    34. if [ "$result" != "" ]
    35. then
    36. echo $result
    37. isCheck=$result
    38. git add $FILE
    39. fi
    40. done
    41. if [ "$isCheck" != "" ]
    42. then
    43. echo "The file has been automatically formatted."
    44. fi
    45. fi
    46. git update-index -g
    47. exit $?

      上述脚本就会自动运行帮助你格式化代码,你也可以忽略脚本。

      这样子我们再也不需要浪费时间在无意义的代码风格的讨论上了。