php如何添加php-psr2語(yǔ)法規(guī)范檢測(cè)

任何語(yǔ)言的語(yǔ)法規(guī)范至關(guān)重要,特別是在團(tuán)隊(duì)協(xié)作過(guò)程中。

以下以php的laravel框架為主,通過(guò)gitpre-commit鉤子(hooks)來(lái)實(shí)現(xiàn)php語(yǔ)法規(guī)范檢測(cè)。

其流程無(wú)非是

  1. 添加語(yǔ)法檢測(cè)器
  2. 添加配置pre-commit(在git執(zhí)行g(shù)it commit操作開(kāi)始前所執(zhí)行的腳本)

開(kāi)始

  1. 使用composer安裝php_codesniffer

    composer global require "squizlabs/php_codesniffer=*" --dev
    
  2. 添加pre-commit
    使用s0enke大神的git-hooks ,將pre-commit (文件名沒(méi)有任何后綴)下載到本地項(xiàng)目的.git/hooks/

  3. 配置pre-commit

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

    // 檢測(cè)程序
    PHPCS_BIN=./vendor/bin/phpcs
    // 檢測(cè)php語(yǔ)法標(biāo)準(zhǔn)
    PHPCS_CODING_STANDARD=PSR2
    /// 忽略檢測(cè)文件
    PHPCS_IGNORE="*.blade.php,*.js,*.css"
    

    修改pre-commit權(quán)限(很重要)

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

其他

  1. Php-psr2規(guī)范檢測(cè)
  1. pre-commit文件代碼(為了方便讀者使用)

    #!/bin/bash
    # PHP CodeSniffer pre-commit hook for git
    #
    # @author Soenke Ruempler <soenke@ruempler.eu>
    # @author Sebastian Kaspari <s.kaspari@googlemail.com>
    #
    # 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
    

原文地址:http://www.fidding.me/article/48

happy coding!!!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容