前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php如何添加php-psr2语法规范检测

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

原创
作者头像
conanma
发布2021-12-05 22:58:02
9170
发布2021-12-05 22:58:02
举报
文章被收录于专栏:正则

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

以下以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规范检测
代码语言:javascript
复制
 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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 开始
  • 其他
相关产品与服务
Prowork 团队协同
ProWork 团队协同(以下简称 ProWork )是便捷高效的协同平台,为团队中的不同角色提供支持。团队成员可以通过日历、清单来规划每⽇的工作,同时管理者也可以通过统计报表随时掌握团队状况。ProWork 摒弃了僵化的流程,通过灵活轻量的任务管理体系,满足不同团队的实际情况,目前 ProWork 所有功能均可免费使用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档