File tree Expand file tree Collapse file tree 6 files changed +83
-36
lines changed Expand file tree Collapse file tree 6 files changed +83
-36
lines changed Original file line number Diff line number Diff line change 11/.github / export-ignore
2+ /.githooks / export-ignore
23/docs / export-ignore
34/tests / export-ignore
45/.gitattributes export-ignore
56/.gitignore export-ignore
67/.php-cs-fixer.dist.php export-ignore
78/compose.yaml export-ignore
89/Dockerfile export-ignore
9- /pre-commit.config.yaml export-ignore
1010/mkdocs.yml export-ignore
1111/phpmd.baseline.xml export-ignore
1212/phpmd.xml export-ignore
Original file line number Diff line number Diff line change 1+ #! /bin/sh
2+
3+ DOCKER_COMPOSE_RUN=" docker compose run -T --rm --no-deps php"
4+
5+ run_command () {
6+ COMMAND_NAME=$1
7+ COMMAND_RUN=$2
8+
9+ (
10+ OUTPUT=$( eval " $DOCKER_COMPOSE_RUN $COMMAND_RUN " 2>&1 )
11+ status_code=$?
12+
13+ if [ $status_code -ne 0 ]; then
14+ printf " \r\033[K❌ %s \033[31m%s\033[0m\n" " $COMMAND_NAME " " failed" >&2
15+ echo " $OUTPUT " > " /tmp/${COMMAND_NAME} _pre-commit"
16+ exit 1
17+ else
18+ printf " \r\033[K✅ %s \033[32m%s\033[0m\n" " $COMMAND_NAME " " passed" >&2
19+ fi
20+ ) &
21+ }
22+
23+ printf " 🐚 \033[33m%s\033[0m\n\n" " Executing pre-commit hook..."
24+
25+ # Staged files
26+ STAGED_FILES=$( git diff --cached --name-only --diff-filter=AMR | grep -E ' \.php$|\.php-cs-fixer(\.dist)?\.php|composer\.lock|phpunit\.xml|phpstan(-baseline)?\.neon' | tr ' \n' ' ' )
27+
28+ HOOK_EXIT_CODE=0
29+ if [ " $STAGED_FILES " ]; then
30+ # PHP-CS-Fixer; run on changed files or on whole project if .php-cs-fixer.dist.php/composer.lock has changed
31+ EXTRA_ARGS=' '
32+ IFS='
33+ '
34+ if ! echo " ${STAGED_FILES} " | grep -qE ' \.php-cs-fixer(\.dist)?\.php|composer\.lock' ; then
35+ EXTRA_ARGS=$( printf -- ' --path-mode=intersection %s' " ${STAGED_FILES} " )
36+ fi
37+
38+ run_command " PHP-CS-Fixer" " vendor/bin/php-cs-fixer fix --dry-run -v --diff --config=.php-cs-fixer.dist.php --show-progress=none $EXTRA_ARGS "
39+ run_command " PHPStan" " vendor/bin/phpstan --memory-limit=1G --error-format=table"
40+ run_command " PHPMD" " vendor/bin/phpmd src/ text phpmd.xml"
41+ run_command " PHPUnit" " vendor/bin/phpunit"
42+
43+ wait
44+
45+ for OUTPUT_FILE in /tmp/* _pre-commit; do
46+ if [ ! -f " $OUTPUT_FILE " ]; then
47+ continue
48+ fi
49+
50+ HOOK_EXIT_CODE=1
51+ printf " \n----------------\n❌ %s\n\n" $( basename $OUTPUT_FILE ' _output' )
52+ cat " $OUTPUT_FILE "
53+ rm " $OUTPUT_FILE "
54+ done
55+
56+ else
57+ printf " ℹ️ No staged PHP files\n"
58+ HOOK_EXIT_CODE=0
59+ fi
60+
61+ exit $HOOK_EXIT_CODE
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -42,7 +42,6 @@ Features and bugfixes should be based on the `master` branch.
4242### Prerequisites
4343
4444* [ Docker Compose] ( https://docs.docker.com/compose/install/ )
45- * [ Pre-commit] ( https://pre-commit.com/#install )
4645* [ Task (optional)] ( https://taskfile.dev/installation/ )
4746
4847### Install dependencies
@@ -51,10 +50,10 @@ Features and bugfixes should be based on the `master` branch.
5150task composer:install
5251```
5352
54- ### Enable pre-commit
53+ ### Enable pre-commit hook
5554
5655``` shell
57- task pre-commit:init
56+ task git:hooks
5857```
5958
6059> Note: Checks for ` phpcs ` , ` phpstan ` , ` phpmd ` and ` phpunit ` are executed when committing.
Original file line number Diff line number Diff line change @@ -63,13 +63,26 @@ tasks:
6363 cmds :
6464 - docker compose run --rm $TTY php composer update {{.PACKAGE}} {{.CLI_ARGS | default "--no-cache"}}
6565
66- # Tools
67- pre-commit:init :
68- desc : Init pre-commit
66+ # Git
67+ git:hooks :
68+ desc : Setup git hooks
6969 silent : true
7070 cmds :
71- - pre-commit install -f
71+ - |
72+ APP_HOOKS_DIR="{{.ROOT_DIR}}/.githooks"
73+ GIT_HOOKS_DIR="{{.ROOT_DIR}}/.git/hooks"
74+
75+ # Create the hooks directory if it does not exist
76+ mkdir -p "$GIT_HOOKS_DIR"
77+
78+ # Symlink hooks
79+ for hook in $(ls "$APP_HOOKS_DIR"); do
80+ ln -sf "$APP_HOOKS_DIR/$hook" "$GIT_HOOKS_DIR/$hook"
81+ done
7282
83+ printf "\033[32m%s\033[0m\n" "Git hooks installed."
84+
85+ # Tools
7386 phpcs :
7487 desc : PHPCS dry run
7588 cmds :
Original file line number Diff line number Diff line change 3939 },
4040 "require-dev" : {
4141 "rector/rector" : " ^1.0" ,
42- "phpmd/phpmd" : " ^2.13 " ,
42+ "phpmd/phpmd" : " ^2.15 " ,
4343 "friendsofphp/php-cs-fixer" : " ^3.64" ,
44- "phpstan/phpstan" : " ^1.9 " ,
44+ "phpstan/phpstan" : " ^1.12 " ,
4545 "phpunit/phpunit" : " ^10.0"
4646 },
4747 "config" : {
You can’t perform that action at this time.
0 commit comments