后端开发规范检查包安装-PHP

PHP基本要求

  • 版本: php7.4 开发环境, 不强制,推荐使用phpstorm,
  • 或者vscode。但请不要将ide配置上传到代码库。(如:.idea) 需要安装pre-
  • commit来做提交前代码检查。

- PHP编码规范

PHP编码规范请参考:PHP-FIG

安装php_CodeSniffer:

composer global require "squizlabs/php_codesniffer=*"

安装提交前pre-commit

检查PSR-2的代码规范的pre-commit hook(项目目录下的.git/hooks创建pre-commit插入一下代码):

#!/bin/sh
 
# @source: https://gist.github.com/ronanguilloux/11f6a788358577474ab4
# @link http://tech.zumba.com/2014/04/14/control-code-quality/
 
 
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
 
# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
    oIFS=$IFS
    IFS='
    '
    SFILES="$1"
    IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}
 
echo "Checking PHP Lint..."
for FILE in $SFILES
do
    php -l -d display_errors=0 $PROJECT/$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..."
    phpcs --standard=PSR2 $PROJECT/$FILE -n
    #bin/php-cs-fixer fix . --config=sf23 --dry-run -vv
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
fi
 
exit $?

环境变量配置 (~/.zshrc)

在这里插入图片描述

配置文件生效

source ~/.zshrc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值