11#! /bin/bash
2+
23# #########
34# Git Pre-Commit file for PHP projects
45# ##
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
1627PHPCS_BIN=/usr/bin/phpcs
1728PHPCS_CODING_STANDARD=PEAR
1829PHPCS_IGNORE=
30+ PHPMD_ACTIVE=1
1931PHPMD_BIN=/usr/bin/phpmd
2032PHPMD_OUTPUT=text
2133PHPMD_PATTERNS_LIST=cleancode,codesize,controversial,design,naming,unusedcode
2234TMP_STAGING=" /tmp/.tmp_staging"
2335
24- # Parse config
36+
37+ # #########
38+ # Parse config file.
39+ # #########
2540CONFIG_FILE=$( dirname $0 ) /config
2641if [ -e $CONFIG_FILE ]; then
2742 . $CONFIG_FILE
2843fi
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+ # #########
3149if [ ! -x $PHPCS_BIN ] || [ ! -x $PHPMD_BIN ]; then
3250 tput setaf 1; echo " Executable not found. Check $PHPCS_BIN and $PHPMD_BIN ."
3351 exit 1
3452fi
3553
36- # Stolen from template file
54+
55+ # #########
56+ # Git Check-up
57+ # #########
3758if git rev-parse --verify HEAD
3859then
3960 against=HEAD
4364fi
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.
4970if [ " $GIT_EXCLUDE " != " " ]; then
5071 GIT_EXCLUDE_LIST=" | grep -v $GIT_EXCLUDE "
5172else
5273 GIT_EXCLUDE_LIST=" "
5374fi
5475
76+
5577FILES=$( git diff-index --name-only --cached --diff-filter=ACMR $against -- $GIT_EXCLUDE_LIST )
5678
5779if [ " $FILES " == " " ]; then
5880 exit 0
5981fi
6082
83+
6184# Create temporary copy of staging area
6285if [ -e $TMP_STAGING ]; then
6386 rm -rf $TMP_STAGING
@@ -80,13 +103,26 @@ if [ "$FILES_TO_CHECK" == "" ]; then
80103 exit 0
81104fi
82105
106+
107+ # #########
83108# Validate PHP CodeSniffer variables
109+ # #########
110+ if [ " $PHPCS_ACTIVE " != " 1" ]; then
111+ PHPCS_ACTIVE=0
112+ fi
113+
84114if [ " $PHPCS_IGNORE " != " " ]; then
85115 IGNORE=" --ignore=$PHPCS_IGNORE "
86116else
87117 IGNORE=" "
88118fi
89119
120+ if [ " $PHPCS_CODING_STANDARD " != " " ]; then
121+ PHPCS_CODING_STANDARD=" --standard=$PHPCS_CODING_STANDARD "
122+ else
123+ PHPCS_CODING_STANDARD=" "
124+ fi
125+
90126if [ " $PHPCS_SNIFFS " != " " ]; then
91127 SNIFFS=" --sniffs=$PHPCS_SNIFFS "
92128else
@@ -105,7 +141,14 @@ else
105141 IGNORE_WARNINGS=" "
106142fi
107143
144+
145+ # #########
108146# Validate PHP Mess Detector variables
147+ # #########
148+ if [ " $PHPMD_ACTIVE " != " 1" ]; then
149+ PHPMD_ACTIVE=0
150+ fi
151+
109152if [ " $PHPMD_OUTPUT_MODE " != " " ]; then
110153 PHPMD_OUTPUT=" $PHPMD_OUTPUT_MODE "
111154else
@@ -130,57 +173,82 @@ else
130173 PHPMD_EXCLUDE_LIST=" "
131174fi
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+ # #########
136182STAGED_FILES=" "
137183for FILE in $FILES_TO_CHECK
138184do
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 "
146193done
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
161215else
162- tput setaf 2; echo " -> Inspection is OK!"
216+ echo " "
217+ tput setaf 8; echo " ➔ PHP CodeSniffer inspection is OFF."
163218fi
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
177240else
178- tput setaf 2; echo " -> Inspection is OK!"
241+ echo " "
242+ tput setaf 8; echo " ➔ PHP Mess Detector inspection is OFF."
179243fi
180244
181- tput setaf 7;
245+ tput setaf 12;
246+
247+
182248
183249rm -rf $TMP_STAGING
184250
251+
252+
185253echo " "
186254exit 0;
0 commit comments