Skip to content

Commit 36cb29e

Browse files
author
agtong
committed
Use php and phpcs through docker.
2 parents e97475d + 5b8e5d6 commit 36cb29e

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
## About
44

55
Auto installed git pre-commit hook for running [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer)
6-
code checking to PSR2 coding standard compliance. It checks only files that are to be committed.
6+
code checking to PSR12 coding standard compliance. It checks only files that are to be committed.
77

88
Inspired by [Enforce code standards with composer, git hooks, and phpcs](http://tech.zumba.com/2014/04/14/control-code-quality/)
99

10+
## Requirement
11+
12+
You need to have a docker service named `php` that is running `php-fpm`, eg. `yiisoftware/yii2-php:7.4-fpm`.
13+
1014
## Installation
1115

1216
Install `agtong/phpcs-git-pre-commit` with composer require command:
1317

14-
composer require "agtong/phpcs-git-pre-commit"
18+
composer require --dev "agtong/phpcs-git-pre-commit"
1519

1620
Or alternatively, include a dependency for `agtong/phpcs-git-pre-commit` in your composer.json file manually:
1721

@@ -36,7 +40,7 @@ Then run `composer install` or `composer update`. `pre-commit` hook will be inst
3640

3741
Run `git commit` and pre-commit hook will check your committed files like if you run
3842

39-
./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 --ignore=*/tests/* -n -p /path/to/file.php
43+
docker-compose exec php /app/vendor/bin/phpcs --standard=PSR12 /path/to/file.php
4044

4145
Note: the `tests` directory is excluded form this check.
4246

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
}
1111
],
1212
"require": {
13-
"squizlabs/php_codesniffer": "3.*"
13+
"squizlabs/php_codesniffer": "^2.7 || ^3.0"
1414
}
1515
}

src/pre-commit

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/sh
22

3-
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
4-
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
3+
PROJECT=$(docker compose exec -T php php -r "echo dirname(dirname(dirname(realpath('$0'))));")
4+
STAGED_FILES_CMD=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
5+
UNSTAGED_FILES_CMD=$(git diff --name-only --diff-filter=ACMR | grep \\.php)
56

67
# Determine if a file list is passed
78
if [ "$#" -eq 1 ]
@@ -12,12 +13,24 @@ then
1213
SFILES="$1"
1314
IFS=$oIFS
1415
fi
16+
1517
SFILES=${SFILES:-$STAGED_FILES_CMD}
1618

19+
STAGED_BUT_MODIFIED_FILES=$(docker compose exec -T php php -r "\$sfiles=(explode(\"\\n\", '$SFILES'));\$usfiles=(explode(\"\\n\", '$UNSTAGED_FILES_CMD'));echo implode(\"\\n\",array_intersect(\$usfiles,\$sfiles));")
20+
21+
if [ -z "$STAGED_BUT_MODIFIED_FILES" ]; then
22+
echo "OK"
23+
else
24+
echo "Files staged but then modified:\n"
25+
echo "${STAGED_BUT_MODIFIED_FILES}"
26+
exit 1
27+
fi
28+
29+
1730
echo "Checking PHP Lint..."
1831
for FILE in $SFILES
1932
do
20-
php -l -d display_errors=0 $PROJECT/$FILE
33+
docker compose exec -T php php -l -d display_errors=0 $PROJECT/$FILE
2134
if [ $? != 0 ]
2235
then
2336
echo "Fix the error before commit."
@@ -28,16 +41,14 @@ done
2841

2942
if [ "$FILES" != "" ]
3043
then
31-
echo "Running Code Sniffer. Code standard PSR2."
32-
./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 --ignore=*/tests/* -n -p $FILES
44+
echo "Running Code Sniffer."
45+
docker-compose exec -T php /app/vendor/bin/phpcs --standard=PSR12 $FILES
46+
3347
if [ $? != 0 ]
3448
then
3549
echo "Fix the error before commit!"
36-
echo "Run"
37-
echo " ./vendor/bin/phpcbf $FILES"
38-
echo "for automatic fix or fix it manually."
3950
exit 1
4051
fi
4152
fi
4253

43-
exit $?
54+
exit $?

0 commit comments

Comments
 (0)