任何语言的语法规范至关重要,特别是在团队协作过程中。

以下以php的laravel框架为主,通过gitpre-commit钩子(hooks)来实现php语法规范检测。

其流程无非是

  1. 添加语法检测器
  2. 添加配置pre-commit(在git执行git commit操作开始前所执行的脚本)

开始

  1. 使用composer安装php_codesniffer

    composer require "squizlabs/php_codesniffer=*" --dev
  2. 添加pre-commit

    使用s0enke大神的git-hooks ,将pre-commit (文件名没有任何后缀)下载到本地项目的.git/hooks/

  3. 配置pre-commit

    修改pre-commit配置信息如下:

    // 检测程序
    PHPCS_BIN=./vendor/bin/phpcs
    // 检测php语法标准
    PHPCS_CODING_STANDARD=PSR2
    /// 忽略检测文件
    PHPCS_IGNORE="*.blade.php,*.js,*.css"

    修改pre-commit权限(很重要)

    chmod -R 755 .git/hooks/pre-commit

其他

  1. Php-psr2规范检测

  2. pre-commit文件代码(为了方便读者使用)

    #!/bin/bash
    # PHP CodeSniffer pre-commit hook for git
    #
    # @author Soenke Ruempler <[email protected]>
    # @author Sebastian Kaspari <[email protected]>
    #
    # see the README
    
    PHPCS_BIN=./vendor/bin/phpcs
    PHPCS_CODING_STANDARD=PSR2
    PHPCS_IGNORE="*.blade.php,*.js,*.css"
    TMP_STAGING=".tmp_staging"
    
    # parse config
    CONFIG_FILE=$(dirname $0)/config
    if [ -e $CONFIG_FILE ]; then
        . $CONFIG_FILE
    fi
    
    # simple check if code sniffer is set up correctly
    if [ ! -x $PHPCS_BIN ]; then
        echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
        exit 1
    fi
    
    # stolen from template file
    if git rev-parse --verify HEAD
    then
        against=HEAD
    else
        # Initial commit: diff against an empty tree object
        against=897dfbffbfe18a52174d999c6d83bd16219d6e08
    fi
    
    # this is the magic: 
    # retrieve all files in staging area that are added, modified or renamed
    # but no deletions etc
    FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- )
    
    if [ "$FILES" == "" ]; then
        exit 0
    fi
    
    # create temporary copy of staging area
    if [ -e $TMP_STAGING ]; then
        rm -rf $TMP_STAGING
    fi
    mkdir $TMP_STAGING
    
    # match files against whitelist
    FILES_TO_CHECK=""
    for FILE in $FILES
    do
        echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
        RETVAL=$?
        if [ "$RETVAL" -eq "0" ]
        then
            FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
        fi
    done
    
    if [ "$FILES_TO_CHECK" == "" ]; then
        exit 0
    fi
    
    # execute the code sniffer
    if [ "$PHPCS_IGNORE" != "" ]; then
        IGNORE="--ignore=$PHPCS_IGNORE"
    else
        IGNORE=""
    fi
    
    if [ "$PHPCS_SNIFFS" != "" ]; then
        SNIFFS="--sniffs=$PHPCS_SNIFFS"
    else
        SNIFFS=""
    fi
    
    if [ "$PHPCS_ENCODING" != "" ]; then
        ENCODING="--encoding=$PHPCS_ENCODING"
    else
        ENCODING=""
    fi
    
    if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
        IGNORE_WARNINGS="-n"
    else
        IGNORE_WARNINGS=""
    fi
    
    # Copy contents of staged version of files to temporary staging area
    # because we only want the staged version that will be commited and not
    # the version in the working directory
    STAGED_FILES=""
    for FILE in $FILES_TO_CHECK
    do
      ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
    
      # create staged version of file in temporary staging area with the same
      # path as the original file so that the phpcs ignore filters can be applied
      mkdir -p "$TMP_STAGING/$(dirname $FILE)"
      git cat-file blob $ID > "$TMP_STAGING/$FILE"
      STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
    done
    
    OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS $STAGED_FILES)
    RETVAL=$?
    
    # delete temporary copy of staging area
    rm -rf $TMP_STAGING
    
    if [ $RETVAL -ne 0 ]; then
        echo "$OUTPUT" | less
    fi
    
    exit $RETVAL

happy coding!