php强制对齐,[强迫症福利] 使用 PHPStorm 对齐数组的键值对

用 php-cs-fixer 自动格式化,一个配置配合 Git 自动格式化,补充楼上。

示例 Git Hook

忽略前端的代码格式化,修改一下就可以用。

#!/bin/bash

#

# check PHP code syntax error and standard with phpcs

# https://blog.csdn.net/xsgnzb/article/details/52222366?locationNum=4&fps=1

# https://blog.csdn.net/ljihe/article/details/80826071

# =================== how to use ====================

# cp ./build/pre-commit.sh ./.git/hooks/pre-commit

# chmod 777 ./.git/hooks/pre-commit

# git commit -h

# git commit -n -m 'pass hook' #bypass pre-commit and commit-msg hooks

# ==================== end ==========================

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

cd $PROJECT

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

# Determine if a file list is passed

if [ "$#" -ne 0 ]

then

exit 0

fi

echo "Checking PHP Lint..."

for FILE in $SFILES

do

php -l -d display_errors=0 $FILE

if [ $? != 0 ]

then

echo "Fix the php error before commit."

exit 1

fi

FILES="$FILES $PROJECT/$FILE"

done

phpcsfixer_path=$(cd `dirname $0`; pwd)"/../../build/php-cs-fixer"

gulp_path=$(cd `dirname $0`; pwd)"/../../frontend/node_modules/.bin/gulp"

prettier_path=$(cd `dirname $0`; pwd)"/../../frontend/node_modules/.bin/prettier"

# format code style

if [ "$FILES" != "" ]

then

echo "Running Code Sniffer..."

isCheck=""

for FILE in $SFILES

do

IGNORE_PATH=(

'vendor'

)

for IGNORE_ITEM in ${IGNORE_PATH[@]}

do

if [[ ${FILE} =~ ${IGNORE_ITEM} ]]

then

echo "Ignore file of "${IGNORE_ITEM}

continue 2

fi

done

#result=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`

#result=`php-cs-fixer fix $FILE --config=.php_cs.dist`

result=`php $phpcsfixer_path fix $FILE --config=.php_cs.dist`

if [ "$result" != "" ]

then

echo $result

isCheck=$result

git add $FILE

fi

done

if [ "$isCheck" != "" ]

then

echo "The file has been automatically formatted."

fi

fi

# for js

jsfiles=$(git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.vue" "*.css" "*.less" | tr '\n' ' ')

[ -z "$jsfiles" ] && exit 0

# format iview

$gulp_path iview --gulpfile frontend/gulpfile.js

# Prettify all staged .js files

echo "$jsfiles" | xargs $prettier_path --config frontend/.prettierrc.js --ignore-path frontend/.prettierignore --write

# Add back the modified/prettified files to staging

echo "$jsfiles" | xargs git add

git update-index -g

exit $?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值