PHP 团队编码规范&校验

1,220 阅读2分钟

规范约束团队内不同的编码风格,以减少浏览不同作者代码时,因代码风格不同造成的不便、提高团队整体代码的可读性

PSR-2 标准

PSR-2是PHP互操作性框架制定小组(PHP-FIG :PHP Framework Interoperability Group)制定的PHP编码规范(PSR:Proposing a Standards Recommendation)

安装 PHP_CodeSniffer

主要包含俩个工具:phpcs 和 phpcbf。

  • phpcs 主要对 PHPJavaScriptCSS 文件定义了一系列代码规范标准,如我们会使用的 PSR-2标准,能够检测出不符合我们定义的代码规范的代码,并发出警告和错误,当然我们也可以设置报错的级别
  • phpcbf 会根据设置的标准对文件尝试修复
$> brew install php-code-sniffer

安装 PHP Coding Standards Fixer

PHP编码标准修复程序(PHP CS修复程序)工具修复您的代码以遵循标准; 您是否希望遵循PSR-1、PSR-2等中定义的PHP编码标准,或者其他社区驱动的标准(如Symfony标准)。您还可以通过配置来定义(团队的)风格。

$> brew install php-cs-fixer

配置PHPStorm

  • 设置 Code Style

    image-20200305140006434

  • 设置 Quality Tools

    image-20200305142100301

  • 设置 Inspections

    image-20200305142718844

  • 设置 External Tools

    • 添加 php-cs

      image-20200305143012426

      Arguments: --standard=PSR2 "$FileDir$/$FileName$"
      
    • 添加php-cs-fixer

      image-20200305143412138

      Arguments: --rules=-@PSR2,@Symfony fix "$FileDir$/$FileName$"
      
      Ps:取消Open consoler for tool output 选项勾选
      
  • 设置快捷键

    image-20200305143904818

使用效果

现在使用PHPStorm打开PHP项目对于不符合PSR-2规范的代码就有有黄色波浪线标注

  • 生成检测报告

    右键需要检测的PHP文件->External Tools->php-cs

    FILE: filename.php
    ----------------------------------------------------------------------
    FOUND 10 ERRORS AND 2 WARNINGS AFFECTING 8 LINES
    ----------------------------------------------------------------------
      39 | ERROR   | [x] Expected 1 space(s) after ELSEIF keyword; 0
         |         |     found
      47 | ERROR   | [x] Expected 1 space(s) after FOREACH keyword; 0
         |         |     found
      47 | ERROR   | [x] Expected 1 space after "=>"; 0 found
      47 | ERROR   | [x] Expected 1 space(s) after closing parenthesis;
         |         |     found 0
      68 | ERROR   | [x] No space found after comma in argument list
      69 | WARNING | [ ] Line exceeds 120 characters; contains 155
         |         |     characters
      72 | ERROR   | [x] No space found after comma in argument list
      73 | WARNING | [ ] Line exceeds 120 characters; contains 160
         |         |     characters
     131 | ERROR   | [x] Expected 1 space(s) after IF keyword; 0 found
     131 | ERROR   | [x] Expected 1 space(s) after closing parenthesis;
         |         |     found 0
     146 | ERROR   | [x] Expected 1 newline at end of file; 0 found
     146 | ERROR   | [x] The closing brace for the class must go on the
         |         |     next line after the body
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 10 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------
    
    Time: 280ms; Memory: 6MB
    
    
    Process finished with exit code 2
    
  • 修复PHP文件

    对检测的文件使用配置好的的php-cs-fixer快捷键后代码会格式化成PSR-2标准。

Git Hook 配置

  • 本地钩子

    在项目根目录下新增文件 ./.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
    

    授予可行性权限:

    $> chmod +x ./.git/hooks/pre-commit
    

    提交不符合PSR-2规范代码

    FILE: fileName.php 
    ----------------------------------------------------------------FOUND 1 ERROR AFFECTING 1 LINE 
    --------------------------------------------------------------
    46 | ERROR | [x] Expected 1 newline at end of file; 0 found
    ----------------------------------------------------------------PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY 
    ---------------------------------------------------------------- Time: 45ms; Memory: 6MB
    Completed with errors, see above
    
  • 服务端钩子