Skip to content

Commit 238517f

Browse files
committed
Refactor
1 parent 355e5a6 commit 238517f

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

pre-commit

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
#!/bin/sh
22
#
3-
# == Installation
3+
# PHP Coding Standards Fixer pre-commit hook
44
#
5-
# Create a Git hooks directory:
5+
# https://github.com/gerardroche/php-cs-fixer-pre-commit-hook
66
#
7-
# mkdir ~/.githooks
8-
#
9-
# Download the `pre-commit` hook to:
10-
#
11-
# ~/.githooks/pre-commit
12-
#
13-
# Make sure the file is executable:
14-
#
15-
# chmod 744 ~/.githooks/pre-commit
16-
#
17-
# Configure Git to use the hooks:
18-
#
19-
# git config --global core.hooksPath ~/.githooks
7+
# @see https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath
8+
# @see https://git-scm.com/docs/githooks#_pre_commit)
9+
# @see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer
10+
# @see https://cs.symfony.com
2011

2112
set -e
2213

@@ -25,10 +16,12 @@ unset CDPATH
2516
IFS='
2617
'
2718

28-
php_cs_fixer_cfg() {
19+
get_config() {
20+
# https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/config.rst
21+
# https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/UPGRADE-v3.md
2922
for candidate in .php-cs-fixer.php .php-cs-fixer.dist.php .php_cs .php_cs.dist; do
30-
if test -f "$1/$candidate"; then
31-
echo "$1/$candidate"
23+
if test -f "./$candidate"; then
24+
echo "./$candidate"
3225
return
3326
fi
3427
done
@@ -37,12 +30,12 @@ php_cs_fixer_cfg() {
3730
run_php_cs_fixer() {
3831
php_cs_fixer_bin=./vendor/bin/php-cs-fixer
3932
if test ! -f "$php_cs_fixer_bin"; then
40-
return
33+
return # Noop, php-cs-fixer not found.
4134
fi
4235

43-
php_cs_fixer_cfg="$(php_cs_fixer_cfg .)"
44-
if test -z "$php_cs_fixer_cfg"; then
45-
return
36+
config="$(get_config)"
37+
if test -z "$config"; then
38+
return # Noop, php-cs-fixer config not found.
4639
fi
4740

4841
# Only do a full check if the PHP Coding Standards fixer configuration file
@@ -56,11 +49,11 @@ run_php_cs_fixer() {
5649
extra_args=''
5750
fi
5851

59-
rerun_with_details() {
52+
rerun_with_violation_details() {
6053
echo >&2 "\033[0;33mCoding guidelines pre-commit check failed.\033[1;0m"
6154
echo >&2 "\033[0;33mRunning check again in verbose mode ...\033[1;0m"
6255

63-
"$php_cs_fixer_bin" fix --config="$php_cs_fixer_cfg"\
56+
"$php_cs_fixer_bin" fix --config="$config"\
6457
-vvv \
6558
--diff \
6659
--dry-run \
@@ -75,12 +68,12 @@ run_php_cs_fixer() {
7568
# again in verbose mode to provide more information about which rules and
7669
# which files are failing the check.
7770
"$php_cs_fixer_bin" fix \
78-
--config="$php_cs_fixer_cfg" \
71+
--config="$config" \
7972
-q \
8073
--dry-run \
8174
--stop-on-violation \
8275
--using-cache=no \
83-
${extra_args} || rerun_with_details
76+
${extra_args} || rerun_with_violation_details
8477
}
8578

8679
run_php_cs_fixer

0 commit comments

Comments
 (0)