標籤:
環境:CentOs 6.7
語言:PHP5.4
PHP_CodeSniffer: https://github.com/phpdragon/PHP_CodeSniffer
本地代碼檢測請查看該文章:http://www.cnblogs.com/phpdragon/p/5199011.html
1.從github下載代碼,解壓到 /home/hg/php_codesniffer/ 目錄下。
拷貝 CodeSniffer.conf.dist 檔案為 CodeSniffer.conf,調整內容如下:、
<?php $phpCodeSnifferConfig = array ( ‘php_path‘ => ‘/usr/bin/php‘, ‘default_standard‘ => ‘PHPdragon‘, ‘report_format‘ => ‘summary‘, ‘show_warnings‘ => ‘0‘, ‘show_progress‘ => ‘1‘, ‘report_width‘ => ‘120‘,)?>
2.編輯程式碼程式庫所在的[ 程式碼程式庫/.hg/hgrc ]檔案。
3.添加如下節點。
[hooks]pretxnchangegroup = /home/hg/php_codesniffer/scripts/hg_pretxnchangegroup_hook.sh
4.賦予該指令碼可執行許可權 chmod a+x 。
# chmod a+x /home/hg/php_codesniffer/scripts/hg_pretxnchangegroup_hook.sh
5.配置完畢,提交代碼測試:
=========================================================以下是鉤子的說明內容============================================================
1. 審核標準有兩個:
一個是全量檢測,但允許提交代碼,
一個是強制規範,是這個判斷來決定是否允許推送。
如果發現代碼無法提交,請點擊查看log, log中會提示為何無法提交代碼。
2.代碼檢測提示文字格式設定如下:
==========================Starting PHP Syntax Check==========================
//建議的規範,檢測報錯提示內容
=========================Force Check PHP Code Syntax=========================
//強制要求的規範,檢測報錯提示內容
===============================================================================
3.報錯內容以 =========================Force Check PHP Code Syntax========================= 標記為開始,該標記以上的內容為建議規範檢測的報錯提示。
這是報錯,要求檢查代碼:
FILE: /tmp/php_syntax_files.xxxxx/src/server/userxxxxxxage_update_service.php //表示不符合規範的檔案路徑,那麼實際地址是 工程路徑/src/server/userxxxxxxage_update_service.php
請到如下提示的 45 行,根據 問題原因 做相應的代碼調整。
下面是推送成功提示:
鉤子代碼:
1 #!/bin/bash 2 3 ############################################ 4 # 1.編輯程式碼程式庫所在的[ 程式碼程式庫/.hg/hgrc ]檔案 5 # 2.添加如下節點 6 # [hooks] 7 # pretxnchangegroup = /home/hg/php_codesniffer/scripts/hg_pretxnchangegroup_hook.sh 8 # 9 # 3.賦予該指令碼可執行許可權 chmod a+x 10 # 以上11 ############################################12 13 echo -e "\n==========================Starting PHP Syntax Check==========================\n"14 15 #在/tmp目錄下建立臨時目錄16 TEMP_DIR=`mktemp -dt php_syntax_files.XXXXXX`17 18 #臨時測試19 #TEMP_DIR="/tmp/test/"20 #HG_NODE="d0a1ccf22a26dfe62ae1db932dcc4972509b8f0b"21 22 HG_BIN="/usr/bin/hg"23 CHECK_CMD="/usr/bin/php /home/hg/php_codesniffer/scripts/phpcs --standard=PHPdragon --tab-width=4 --extensions=php $TEMP_DIR"24 FORCE_CHECK_CMD="/usr/bin/php /home/hg/php_codesniffer/scripts/phpcs --standard=PHPdragonForce --tab-width=4 --extensions=php $TEMP_DIR"25 26 #輸出臨時目錄27 echo "temp dir : "$TEMP_DIR28 29 #匯出當前至最新30 #hg archive -r $HG_NODE:tip -t files /tmp/test31 #匯出當前32 #hg archive -r $HG_NODE -t files /tmp/test33 #全量匯出34 #hg archive -r tip -t files /tmp/test35 #匯出修改部分的代碼36 echo $HG_BIN" archive -I \"set:added() or modified()\" -r "$HG_NODE":tip -t files "$TEMP_DIR37 $HG_BIN archive -I "set:added() or modified()" -r $HG_NODE:tip -t files $TEMP_DIR38 39 #檢測代碼,輸出提示40 echo "$CHECK_CMD"41 $CHECK_CMD42 43 echo -e "\n\n\n\n=========================Force Check PHP Code Syntax=========================\n"44 45 #強制檢測代碼46 echo "$FORCE_CHECK_CMD"47 TEST_SYNTAX=`$FORCE_CHECK_CMD`48 echo -e "$TEST_SYNTAX"49 50 #刪除目錄51 echo "rm -rf "$TEMP_DIR52 rm -rf "$TEMP_DIR"53 54 if [ "0" == "${TEST_SYNTAX}" ] || [ "" == "${TEST_SYNTAX}" ];then55 echo -e "\n\nThrough code detection, allowed to push! Push successfully!"56 echo -e "\n===============================================================================\n\n"57 exit 058 fi59 60 echo -e "\n\nServer detected the code has a problem, please check to submit the push again!"61 echo -e "\n===============================================================================\n\n"62 exit 1
PHP_CodeSniffer HG 服務端部署篇