Skip to content

Commit 8135d3b

Browse files
committed
WIP
1 parent fc92236 commit 8135d3b

File tree

1 file changed

+94
-33
lines changed

1 file changed

+94
-33
lines changed

pre-commit

Lines changed: 94 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,46 @@
1313
#
1414
##########
1515

16+
##########
17+
# DEFAULT SETTINGS
18+
###
19+
#
20+
# These variables define the basic values for Code_Sniffer and PHPMD.
21+
# Override these by creating a new variable on the `config` file.
22+
#
23+
##########
24+
PHPCS_ACTIVE=1
1625
PHPCS_BIN=/usr/bin/phpcs
1726
PHPCS_CODING_STANDARD=PEAR
1827
PHPCS_IGNORE=
28+
PHPMD_ACTIVE=1
1929
PHPMD_BIN=/usr/bin/phpmd
2030
PHPMD_OUTPUT=text
2131
PHPMD_PATTERNS_LIST=cleancode,codesize,controversial,design,naming,unusedcode
2232
TMP_STAGING="/tmp/.tmp_staging"
2333

24-
# Parse config
34+
35+
##########
36+
# Parse config file.
37+
##########
2538
CONFIG_FILE=$(dirname $0)/config
2639
if [ -e $CONFIG_FILE ]; then
2740
. $CONFIG_FILE
2841
fi
2942

30-
# Simple check if code sniffer is set up correctly
43+
44+
##########
45+
# First: check if PHP Code_Sniffer and PHPMD bin files are present && executable.
46+
##########
3147
if [ ! -x $PHPCS_BIN ] || [ ! -x $PHPMD_BIN ]; then
3248
tput setaf 1; echo "Executable not found. Check $PHPCS_BIN and $PHPMD_BIN."
3349
exit 1
3450
fi
3551

36-
# Stolen from template file
52+
53+
##########
54+
# Git Check-up
55+
##########
3756
if git rev-parse --verify HEAD
3857
then
3958
against=HEAD
@@ -43,9 +62,9 @@ else
4362
fi
4463

4564
# This is the magic:
46-
# Retrieve all files in staging area that are added, modified or renamed
65+
# Retrieve all files in staging area that are ADDED, MODIFIED or RENAMED,
4766
# but no deletions etc.
48-
# Lets first check if there are any file pattern to exclude from this list
67+
# Lets first check if there are any file pattern to exclude from this list.
4968
if [ "$GIT_EXCLUDE" != "" ]; then
5069
GIT_EXCLUDE_LIST="| grep -v $GIT_EXCLUDE"
5170
else
@@ -80,13 +99,26 @@ if [ "$FILES_TO_CHECK" == "" ]; then
8099
exit 0
81100
fi
82101

102+
103+
##########
83104
# Validate PHP CodeSniffer variables
105+
##########
106+
if [ "$PHPCS_ACTIVE" != "1" ]; then
107+
PHPCS_ACTIVE=0
108+
fi
109+
84110
if [ "$PHPCS_IGNORE" != "" ]; then
85111
IGNORE="--ignore=$PHPCS_IGNORE"
86112
else
87113
IGNORE=""
88114
fi
89115

116+
if [ "$PHPCS_CODING_STANDARD" != "" ]; then
117+
PHPCS_CODING_STANDARD="--sniffs=$PHPCS_CODING_STANDARD"
118+
else
119+
PHPCS_CODING_STANDARD=""
120+
fi
121+
90122
if [ "$PHPCS_SNIFFS" != "" ]; then
91123
SNIFFS="--sniffs=$PHPCS_SNIFFS"
92124
else
@@ -105,7 +137,14 @@ else
105137
IGNORE_WARNINGS=""
106138
fi
107139

140+
141+
##########
108142
# Validate PHP Mess Detector variables
143+
##########
144+
if [ "$PHPMD_ACTIVE" != "1" ]; then
145+
PHPMD_ACTIVE=0
146+
fi
147+
109148
if [ "$PHPMD_OUTPUT_MODE" != "" ]; then
110149
PHPMD_OUTPUT="$PHPMD_OUTPUT_MODE"
111150
else
@@ -130,57 +169,79 @@ else
130169
PHPMD_EXCLUDE_LIST=""
131170
fi
132171

172+
##########
133173
# Copy contents of staged version of files to temporary staging area
134174
# because we only want the staged version that will be commited and not
135175
# the version in the working directory.
176+
##########
136177
STAGED_FILES=""
137178
for FILE in $FILES_TO_CHECK
138179
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"
180+
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
181+
##########
182+
# Create staged version of file in temporary staging area with the same
183+
# path as the original file so that the phpcs ignore filters can be applied.
184+
##########
185+
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
186+
git cat-file blob $ID > "$TMP_STAGING/$FILE"
187+
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
146188
done
147189

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=$?
152190

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

158-
rm -rf $TMP_STAGING
204+
rm -rf $TMP_STAGING
159205

160-
exit $PHPCS_RETVAL
206+
exit $PHPCS_RETVAL
207+
else
208+
tput setaf 2; echo " -> Inspection is OK!"
209+
fi
161210
else
162-
tput setaf 2; echo " -> Inspection is OK!"
211+
echo ""
212+
tput setaf 8; echo ":-( PHP CodeSniffer inspection is OFF."
163213
fi
164214

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=$?
169215

170-
if [ $PHPMD_RETVAL -ne 0 ]; then
171-
tput setaf 1; echo " -> Issues found: "
172-
tput setaf 7; echo "$PHPMD_OUTPUT"
216+
##########
217+
# CODE INSPECTION: PHP Mess Detector
218+
##########
219+
if [ "$PHPMD_ACTIVE" == "1" ]; then
220+
echo ""
221+
tput setaf 7; echo " :: PHP Mess Detector inspection :: "
222+
PHPMD_OUTPUT=$($PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST $PHPMD_SUFFIXES_LIST $PHPMD_EXCLUDE_LIST)
223+
PHPMD_RETVAL=$?
173224

174-
rm -rf $TMP_STAGING
225+
if [ $PHPMD_RETVAL -ne 0 ]; then
226+
tput setaf 1; echo " -> Issues found: "
227+
tput setaf 7; echo "$PHPMD_OUTPUT"
228+
229+
rm -rf $TMP_STAGING
175230

176-
exit $PHPMD_RETVAL
231+
exit $PHPMD_RETVAL
232+
else
233+
tput setaf 2; echo " -> Inspection is OK!"
234+
fi
177235
else
178-
tput setaf 2; echo " -> Inspection is OK!"
236+
echo ""
237+
tput setaf 8; echo ":-( PHP Mess Detector inspection is OFF."
179238
fi
180239

181240
tput setaf 7;
182241

242+
183243
rm -rf $TMP_STAGING
184244

245+
185246
echo ""
186247
exit 0;

0 commit comments

Comments
 (0)