任何語(yǔ)言的語(yǔ)法規(guī)范至關(guān)重要,特別是在團(tuán)隊(duì)協(xié)作過(guò)程中。
以下以php的laravel框架為主,通過(guò)git的pre-commit鉤子(hooks)來(lái)實(shí)現(xiàn)php語(yǔ)法規(guī)范檢測(cè)。
其流程無(wú)非是
- 添加語(yǔ)法檢測(cè)器
- 添加配置
pre-commit(在git執(zhí)行g(shù)it commit操作開(kāi)始前所執(zhí)行的腳本)
開(kāi)始
-
使用composer安裝php_codesniffer
composer global require "squizlabs/php_codesniffer=*" --dev 添加
pre-commit
使用s0enke大神的git-hooks ,將pre-commit (文件名沒(méi)有任何后綴)下載到本地項(xiàng)目的.git/hooks/下-
配置
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
其他
-
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!!!