forked from osteel/git-pre-commit-phpcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·61 lines (50 loc) · 1.33 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
STANDARD="PSR12"
BIN="./vendor/bin"
echo
echo "PHP_CodeSniffer pre-commit hook – commit with the --no-verify option to skip the hook"
echo
# Check whether PHP_CodeSniffer can be found
if [ ! -f "$BIN/phpcs" ]
then
echo "PHP_CodeSniffer not found – is it installed? 'composer require --dev squizlabs/php_codesniffer'"
echo
exit 1
fi
# Retrieve staged files
FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD)
# Run the sniffer
echo "Running PHP_CodeSniffer using the $STANDARD standard"
echo
PHPCS=("$BIN/phpcs" "--standard=$STANDARD" "--filter=gitstaged" "--encoding=utf-8" "-p" ".")
"${PHPCS[@]}"
# Syntax OK
if [ $? == 0 ]
then
echo "No violations detected"
echo
exit 0
fi
# Fix automatically?
read -p "Automatically fix violations when possible? [Y/n]: " < /dev/tty
if [[ ! ("$REPLY" == 'y' || "$REPLY" == 'Y' || "$REPLY" == '') ]]
then
echo
exit 1
fi
# Run the beautifier
PHPCBF=("$BIN/phpcbf" "--standard=$STANDARD" "--filter=gitstaged" ".")
"${PHPCBF[@]}"
# Stage the files
echo "Re-staging updated files"
echo
git add ${FILES}
# Run the sniffer again
"${PHPCS[@]}"
# Some violations remain
if [ $? != 0 ]
then
echo "PHP_CodeSniffer was not able to fix all of the violations, please fix the remaining ones manually or commit with the --no-verify option"
echo
exit 1
fi