美文网首页
php如何添加php-psr2语法规范检测

php如何添加php-psr2语法规范检测

作者: Fidding | 来源:发表于2018-04-17 19:28 被阅读0次

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

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

其流程无非是

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

开始

  1. 使用composer安装php_codesniffer

    composer global 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规范检测
  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!!!

相关文章

  • php如何添加php-psr2语法规范检测

    任何语言的语法规范至关重要,特别是在团队协作过程中。 以下以php的laravel框架为主,通过git的pre-c...

  • Git 钩子检查 PHP 语法和代码规范

    场景 每次提交代码都要手动执行 php 的语法检查和代码规范,如何在客户端把这些工作进行自动化。 方案 利用 gi...

  • flutter: 编码规范及工具监测

    序言 google 提供了 pedantic 工具库用于检测编码规范。 使用 yaml 添加引用pedantic:...

  • HTML5 - 类名API

    给当前元素添加类样式 语法 示例 给当前元素移除类样式 语法 示例 检测当前元素添是否包含类样式 语法 示例 给当...

  • vue 的 eslint 语法

    我们在项目中启用了eslint语法规范检测,往往为了代码能够满足这样的规范,我们可以这样处理在vs code里面设...

  • (转载)如何写出优雅耐看的PHP代码?

    如何写出优雅耐看的PHP代码?本篇文章带大家了解一下PHP代码的基本书写规范和框架规范,了解它们让你的PHP代码优...

  • 2019-11-06 Windows系统下使用PHPCS+PHP

    前言 0.介绍 PHP_CodeSniffer php代码嗅探器是一个代码风格检测工具,着重代码规范它包含两类脚本...

  • jsx语法

    什么是jsx语法:就是符合xml规范的js语法(语法格式相对来说,要比HTML严谨) 1、如何启用jsx语法? 安...

  • PHP中addslashes()和stripslashes()实

    PHP addcslashes()函数定义和用法addcslashes()函数在指定的字符前添加反斜杠。语法add...

  • PHP(一)

    PHP is_numeric()函数is_numeric() 函数用于检测变量是否为数字或数字字符串。语法:boo...

网友评论

      本文标题:php如何添加php-psr2语法规范检测

      本文链接:https://www.haomeiwen.com/subject/gfbpkftx.html