Skip to content

Commit a037b50

Browse files
authored
Update pre-commit to lint code (codeigniter4#4960)
1 parent f58a15c commit a037b50

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

admin/pre-commit

+40-14
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,75 @@ PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
44
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php$`
55

66
# Determine if a file list is passed
7-
if [ "$#" -eq 1 ]
8-
then
7+
if [ "$#" -eq 1 ]; then
98
oIFS=$IFS
109
IFS='
1110
'
1211
SFILES="$1"
1312
IFS=$oIFS
1413
fi
14+
1515
SFILES=${SFILES:-$STAGED_FILES_CMD}
1616

1717
echo "Starting CodeIgniter precommit..."
1818

19-
if [ "$SFILES" != "" ]
20-
then
19+
if [ "$SFILES" != "" ]; then
2120
echo "Linting PHP code..."
22-
for FILE in $SFILES
23-
do
21+
for FILE in $SFILES; do
2422
php -l -d display_errors=0 "$PROJECT/$FILE"
25-
if [ $? != 0 ]
26-
then
23+
24+
if [ $? != 0 ]; then
2725
echo "Fix the error(s) before commit."
2826
exit 1
2927
fi
28+
3029
FILES="$FILES $FILE"
3130
done
3231
fi
3332

34-
if [ "$FILES" != "" ]
35-
then
33+
if [ "$FILES" != "" ]; then
3634
echo "Running PHPStan..."
35+
3736
# Run on whole codebase
38-
if [ -d /proc/cygdrive ]
39-
then
37+
if [ -d /proc/cygdrive ]; then
4038
./vendor/bin/phpstan analyse
4139
else
4240
php ./vendor/bin/phpstan analyse
4341
fi
4442

45-
if [ $? != 0 ]
46-
then
43+
if [ $? != 0 ]; then
4744
echo "Fix the phpstan error(s) before commit."
4845
exit 1
4946
fi
5047
fi
5148

49+
if [ "$FILES" != "" ]; then
50+
echo "Running PHP CS Fixer..."
51+
52+
# Run on whole codebase to skip on unnecessary filtering
53+
# Run first on app, admin, public
54+
if [ -d /proc/cygdrive ]; then
55+
./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --diff --config=.no-header.php-cs-fixer.dist.php
56+
else
57+
php ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --diff --config=.no-header.php-cs-fixer.dist.php
58+
fi
59+
60+
if [ $? != 0 ]; then
61+
echo "Files in app, admin, or public are not following the coding standards. Please fix them before commit."
62+
exit 1
63+
fi
64+
65+
# Next, run on system, tests, utils, and root PHP files
66+
if [ -d /proc/cygdrive ]; then
67+
./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --diff
68+
else
69+
php ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --diff
70+
fi
71+
72+
if [ $? != 0]; then
73+
echo "Files in system, tests, utils, or root are not following the coding standards. Please fix them before commit."
74+
exit 1
75+
fi
76+
fi
77+
5278
exit $?

admin/setup.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

33
# Install a pre-commit hook that
4-
# automatically runs phpcs to fix styles
4+
# automatically runs php-cs-fixer to lint code
55
mkdir -p .git/hooks
66
cp admin/pre-commit .git/hooks/pre-commit
77
chmod +x .git/hooks/pre-commit

0 commit comments

Comments
 (0)