2019-11-06 Windows系統(tǒng)下使用PHPCS+PHPMD+GIT鉤子

前言

使用PHPCS+GIT鉤子保障團(tuán)隊(duì)開發(fā)中代碼風(fēng)格一致性實(shí)踐
使用PHPMD提高代碼質(zhì)量與可讀性

0.介紹

  • PHP_CodeSniffer php代碼嗅探器
    • 是一個(gè)代碼風(fēng)格檢測工具,著重代碼規(guī)范
    • 它包含兩類腳本,phpcs 和 phpcbf
      • 包含phpcs(php code standard 代碼標(biāo)準(zhǔn))
      • phpcbf(php code beautify fix 代碼美化修復(fù))

1.安裝

composer global require "squizlabs/php_codesniffer=*"

2.驗(yàn)證是否安裝成功并查看幫助

phpcs --help
image.png

3.使用

phpcs path/file.php
image.png

4.集成到git
4.1 新增鉤子文件
在 .git\hooks\目錄下執(zhí)行下面的命令

cp pre-commit.sample  pre-commit

修改其中內(nèi)容為

#!/bin/bash

#

# check PHP code syntax error and standard with phpcs

# author : star[github.com/star1989]

# date : 2017-02-24

PROJECT=$(git rev-parse --show-toplevel)

cd $PROJECT

SFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)

TMP_DIR=$PROJECT."/tmp"

# Determine if a file list is passed

if [ "$#" -ne 0 ]

then

    exit 0

fi

echo "Checking PHP Lint..."

for FILE in $SFILES

do

#    echo "php -l -d display_errors=0 ${FILE}"

#  echo "git show :$FILE > $TMP_DIR/$FILE"

    php -l -d display_errors=0 $FILE

    if [ $? != 0  ]

    then

        echo "Fix the error before commit."

        exit 1

    fi

    FILES="$FILES $PROJECT/$FILE"

done

if [ "$FILES" != "" ]

then

    echo "Running Code Sniffer..."

    TMP_DIR=/tmp/$(uuidgen)

    mkdir -p $TMP_DIR

    for FILE in $SFILES

    do

        mkdir -p $TMP_DIR/$(dirname $FILE)

        git show :$FILE > $TMP_DIR/$FILE

    done

    phpcs --standard=PSR2 --encoding=utf-8 -n $TMP_DIR

    PHPCS_ERROR=$?

    rm -rf $TMP_DIR

    if [ $PHPCS_ERROR != 0 ]

    then

        echo "Fix the error before commit."

        exit 1

    fi

fi

exit $?

5.在git下使用(git觸發(fā)檢測)

執(zhí)行g(shù)it commit 后會自動(dòng)檢測待提交代碼的格式

image.png

6.自定義phpcs規(guī)則

有些情況我們需要忽略一些規(guī)則或者添加一些自定義的規(guī)則,比如有些類不需要命名空間(遷移類),不希望在檢測代碼時(shí)拋出該類型錯(cuò)誤

image

6.1 添加標(biāo)準(zhǔn)


$ phpcs --config-set installed_paths ruleset.xml

6.2 編輯規(guī)則內(nèi)容

將ruleset.xml放置在項(xiàng)目根目錄下,并寫入具體規(guī)則


<?xml version="1.0"?>

<ruleset name="CustomStandard">

  <!-- 代碼標(biāo)準(zhǔn)為PSR2 -->

<rule ref="PSR2">

    <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>

</rule>

</ruleset>

6.3 修改pre-commit檢測方式

將其中的


phpcs --standard=PSR2 --encoding=utf-8 -n $TMP_DIR

改為


phpcs --standard=ruleset.xml -s --encoding=utf-8 -n $TMP_DIR

這樣就可以跳過命名空間的檢測了!快去試試吧

7.常用命令

檢查單個(gè)文件:phpcs /path/to/code
檢查目錄下的文件:phpcs /path/to/code/
查看已經(jīng)安裝的標(biāo)準(zhǔn):phpcs -i
設(shè)置默認(rèn)檢查標(biāo)準(zhǔn):phpcs --config-set default_standard /path/to/standard_file
查看配置:phpcs --config-show
指定報(bào)告格式:phpcs --report=summary /path/to/code ;可用的報(bào)告格式有full, xml, checkstyle, csv, json, emacs, source, summary, diff, svnblame, gitblame, hgblame, notifysend,默認(rèn)為full
查看幫助:phpcs -h
自動(dòng)修復(fù):phpcbf /path/to/code

8.phpmd 介紹

  • PHP Mess Detector PHP混亂探測器

    • 是一個(gè)代碼質(zhì)量檢測工具,著重代碼質(zhì)量

9.安裝

composer global require phpmd/phpmd

10.使用

$phpmd path\code text codesize,unusedcode,naming,design

參數(shù)說明
# phpmd 源代碼路徑 報(bào)告的格式 規(guī)則列表
# 源代碼路徑 支持
   一個(gè)文件 /path/to/file
   一個(gè)目錄 /path/to/source 
# 報(bào)告的格式 支持 
    xml:以XML格式輸出;
    text:簡單的文本格式;
    html:輸出到單個(gè)的html;
# 規(guī)則列表 支持
    phpmd_ruleset.xml 文件格式
    codesize,unusedcode,naming 單個(gè)命令集合
# 附加參數(shù)
  --exclude - 忽略的目錄,以逗號分隔多個(gè)目錄。
# 例子
phpmd /path/to/source html ./phpmd_ruleset.xml

11.添加到git鉤子

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

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