Skip to content

Commit d86ff9f

Browse files
authored
Merge pull request #10 from devdrops/phpcs-improvements
Fixes and improvements
2 parents fc92236 + e610547 commit d86ff9f

File tree

2 files changed

+107
-35
lines changed

2 files changed

+107
-35
lines changed

config.dist

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Enable tools
2+
PHPCS_ACTIVE=1
3+
PHPMD_ACTIVE=1
4+
15
# Path to binaries.
26
PHPCS_BIN=/usr/local/bin/phpcs
37
PHPMD_BIN=/usr/local/bin/phpmd
@@ -36,4 +40,4 @@ PHPMD_SUFFIXES=php
3640
PHPMD_EXCLUDE=
3741

3842
# List of file patterns to exclude (using `grep`)
39-
GIT_EXCLUDE="Test"
43+
GIT_EXCLUDE=

pre-commit

Lines changed: 102 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
23
##########
34
# Git Pre-Commit file for PHP projects
45
###
@@ -13,27 +14,47 @@
1314
#
1415
##########
1516

17+
18+
##########
19+
# DEFAULT SETTINGS
20+
###
21+
#
22+
# These variables define the basic values for Code_Sniffer and PHPMD.
23+
# Override these by creating a new variable on the `config` file.
24+
#
25+
##########
26+
PHPCS_ACTIVE=1
1627
PHPCS_BIN=/usr/bin/phpcs
1728
PHPCS_CODING_STANDARD=PEAR
1829
PHPCS_IGNORE=
30+
PHPMD_ACTIVE=1
1931
PHPMD_BIN=/usr/bin/phpmd
2032
PHPMD_OUTPUT=text
2133
PHPMD_PATTERNS_LIST=cleancode,codesize,controversial,design,naming,unusedcode
2234
TMP_STAGING="/tmp/.tmp_staging"
2335

24-
# Parse config
36+
37+
##########
38+
# Parse config file.
39+
##########
2540
CONFIG_FILE=$(dirname $0)/config
2641
if [ -e $CONFIG_FILE ]; then
2742
. $CONFIG_FILE
2843
fi
2944

30-
# Simple check if code sniffer is set up correctly
45+
46+
##########
47+
# First: check if PHP Code_Sniffer and PHPMD bin files are present && executable.
48+
##########
3149
if [ ! -x $PHPCS_BIN ] || [ ! -x $PHPMD_BIN ]; then
3250
tput setaf 1; echo "Executable not found. Check $PHPCS_BIN and $PHPMD_BIN."
3351
exit 1
3452
fi
3553

36-
# Stolen from template file
54+
55+
##########
56+
# Git Check-up
57+
##########
3758
if git rev-parse --verify HEAD
3859
then
3960
against=HEAD
@@ -43,21 +64,23 @@ else
4364
fi
4465

4566
# This is the magic:
46-
# Retrieve all files in staging area that are added, modified or renamed
67+
# Retrieve all files in staging area that are ADDED, MODIFIED or RENAMED,
4768
# but no deletions etc.
48-
# Lets first check if there are any file pattern to exclude from this list
69+
# Lets first check if there are any file pattern to exclude from this list.
4970
if [ "$GIT_EXCLUDE" != "" ]; then
5071
GIT_EXCLUDE_LIST="| grep -v $GIT_EXCLUDE"
5172
else
5273
GIT_EXCLUDE_LIST=""
5374
fi
5475

76+
5577
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- $GIT_EXCLUDE_LIST)
5678

5779
if [ "$FILES" == "" ]; then
5880
exit 0
5981
fi
6082

83+
6184
# Create temporary copy of staging area
6285
if [ -e $TMP_STAGING ]; then
6386
rm -rf $TMP_STAGING
@@ -80,13 +103,26 @@ if [ "$FILES_TO_CHECK" == "" ]; then
80103
exit 0
81104
fi
82105

106+
107+
##########
83108
# Validate PHP CodeSniffer variables
109+
##########
110+
if [ "$PHPCS_ACTIVE" != "1" ]; then
111+
PHPCS_ACTIVE=0
112+
fi
113+
84114
if [ "$PHPCS_IGNORE" != "" ]; then
85115
IGNORE="--ignore=$PHPCS_IGNORE"
86116
else
87117
IGNORE=""
88118
fi
89119

120+
if [ "$PHPCS_CODING_STANDARD" != "" ]; then
121+
PHPCS_CODING_STANDARD="--standard=$PHPCS_CODING_STANDARD"
122+
else
123+
PHPCS_CODING_STANDARD=""
124+
fi
125+
90126
if [ "$PHPCS_SNIFFS" != "" ]; then
91127
SNIFFS="--sniffs=$PHPCS_SNIFFS"
92128
else
@@ -105,7 +141,14 @@ else
105141
IGNORE_WARNINGS=""
106142
fi
107143

144+
145+
##########
108146
# Validate PHP Mess Detector variables
147+
##########
148+
if [ "$PHPMD_ACTIVE" != "1" ]; then
149+
PHPMD_ACTIVE=0
150+
fi
151+
109152
if [ "$PHPMD_OUTPUT_MODE" != "" ]; then
110153
PHPMD_OUTPUT="$PHPMD_OUTPUT_MODE"
111154
else
@@ -130,57 +173,82 @@ else
130173
PHPMD_EXCLUDE_LIST=""
131174
fi
132175

176+
177+
##########
133178
# Copy contents of staged version of files to temporary staging area
134179
# because we only want the staged version that will be commited and not
135180
# the version in the working directory.
181+
##########
136182
STAGED_FILES=""
137183
for FILE in $FILES_TO_CHECK
138184
do
139-
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
140-
141-
# Create staged version of file in temporary staging area with the same
142-
# path as the original file so that the phpcs ignore filters can be applied.
143-
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
144-
git cat-file blob $ID > "$TMP_STAGING/$FILE"
145-
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
185+
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
186+
##########
187+
# Create staged version of file in temporary staging area with the same
188+
# path as the original file so that the phpcs ignore filters can be applied.
189+
##########
190+
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
191+
git cat-file blob $ID > "$TMP_STAGING/$FILE"
192+
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
146193
done
147194

148-
echo ""
149-
tput setaf 7; echo " :: PHP CodeSniffer inspection :: "
150-
PHPCS_OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $STAGED_FILES)
151-
PHPCS_RETVAL=$?
152195

153-
if [ $PHPCS_RETVAL -ne 0 ]; then
154-
tput setaf 1; echo " -> Issues found: "
155-
tput setaf 7;
156-
echo "$PHPCS_OUTPUT"
196+
##########
197+
# CODE INSPECTION: PHP CodeSniffer
198+
##########
199+
if [ "$PHPCS_ACTIVE" == "1" ]; then
200+
echo ""
201+
tput setaf 12; echo " :: PHP CodeSniffer inspection :: "
202+
PHPCS_OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS $PHPCS_CODING_STANDARD $ENCODING $IGNORE $STAGED_FILES)
203+
PHPCS_RETVAL=$?
157204

158-
rm -rf $TMP_STAGING
205+
if [ $PHPCS_RETVAL -ne 0 ]; then
206+
tput setaf 1; echo " ✘ Issues found: "
207+
tput setaf 7; echo "$PHPCS_OUTPUT"
159208

160-
exit $PHPCS_RETVAL
209+
rm -rf $TMP_STAGING
210+
211+
exit $PHPCS_RETVAL
212+
else
213+
tput setaf 2; echo " ✔ Inspection is OK!"
214+
fi
161215
else
162-
tput setaf 2; echo " -> Inspection is OK!"
216+
echo ""
217+
tput setaf 8; echo " ➔ PHP CodeSniffer inspection is OFF."
163218
fi
164219

165-
echo ""
166-
tput setaf 7; echo " :: PHP Mess Detector inspection :: "
167-
PHPMD_OUTPUT=$($PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST $PHPMD_SUFFIXES_LIST $PHPMD_EXCLUDE_LIST)
168-
PHPMD_RETVAL=$?
169220

170-
if [ $PHPMD_RETVAL -ne 0 ]; then
171-
tput setaf 1; echo " -> Issues found: "
172-
tput setaf 7; echo "$PHPMD_OUTPUT"
221+
##########
222+
# CODE INSPECTION: PHP Mess Detector
223+
##########
224+
if [ "$PHPMD_ACTIVE" == "1" ]; then
225+
echo ""
226+
tput setaf 12; echo " :: PHP Mess Detector inspection :: "
227+
PHPMD_OUTPUT=$($PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST $PHPMD_SUFFIXES_LIST $PHPMD_EXCLUDE_LIST)
228+
PHPMD_RETVAL=$?
173229

174-
rm -rf $TMP_STAGING
230+
if [ $PHPMD_RETVAL -ne 0 ]; then
231+
tput setaf 1; echo " ✘ Issues found: "
232+
tput setaf 7; echo "$PHPMD_OUTPUT"
175233

176-
exit $PHPMD_RETVAL
234+
rm -rf $TMP_STAGING
235+
236+
exit $PHPMD_RETVAL
237+
else
238+
tput setaf 2; echo " ✔ Inspection is OK!"
239+
fi
177240
else
178-
tput setaf 2; echo " -> Inspection is OK!"
241+
echo ""
242+
tput setaf 8; echo " ➔ PHP Mess Detector inspection is OFF."
179243
fi
180244

181-
tput setaf 7;
245+
tput setaf 12;
246+
247+
182248

183249
rm -rf $TMP_STAGING
184250

251+
252+
185253
echo ""
186254
exit 0;

0 commit comments

Comments
 (0)