Skip to content

Commit 9c870c6

Browse files
committed
Merge pull request #4 from devdrops/phpmd-validations
Merged #4.
2 parents 02a906b + 4cc0077 commit 9c870c6

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

config.dist

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
# path to binaries
1+
# Path to binaries.
22
PHPCS_BIN=/usr/local/bin/phpcs
33
PHPMD_BIN=/usr/local/bin/phpmd
44

5-
# the coding standard, you can also specify a path to your own standard here
5+
# The PHP CodeSniffer coding standard, you can also specify a path to your
6+
# own standard here.
67
# e. g. /path/to/my/standard/dir/
78
PHPCS_CODING_STANDARD=PSR2
89

9-
# comma-separated list of file patterns being ignored
10+
# Comma-separated list of file patterns being ignored by PHP CodeSniffer.
1011
PHPCS_IGNORE=
1112

12-
# comma-seperated list of sniffs from the standard that should be used
13-
# use `phpcs --standard=PSR1 -e` to list sniffs for your standard
13+
# Comma-seperated list of sniffs from PHP CodeSniffer that should be used.
14+
# Use `phpcs --standard=PSR1 -e` to list sniffs for your standard.
1415
PHPCS_SNIFFS=Generic.Files.ByteOrderMark,Generic.PHP.DisallowShortOpenTag
1516

16-
# egrep compatible pattern of files to be checked
17+
# Egrep compatible pattern of files to be checked by PHP CodeSniffer.
1718
PHPCS_FILE_PATTERN="\.(php)$"
1819

19-
# ignore warnings
20+
# Ignore PHP CodeSniffer warnings.
2021
PHPCS_IGNORE_WARNINGS=1
2122

22-
# encoding
23+
# PHP CodeSniffer Encoding.
2324
PHPCS_ENCODING=utf-8
2425

25-
# PHP Mess Detector output mode
26+
# PHP Mess Detector output mode (default: text).
2627
PHPMD_OUTPUT_MODE=text
2728

28-
# PHP Mess Detector patterns
29+
# PHP Mess Detector patterns (defaults: cleancode).
2930
PHPMD_PATTERNS=cleancode,codesize,controversial,design,naming,unusedcode

pre-commit

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,27 @@
1313
#
1414
##########
1515

16-
GRAY='\033[0;37m'
17-
RED='\033[0;31m'
18-
GREEN='\033[0;32m'
19-
2016
PHPCS_BIN=/usr/bin/phpcs
21-
PHPMD_BIN=/usr/bin/phpmd
2217
PHPCS_CODING_STANDARD=PEAR
23-
PHPMD_OUTPUT_MODE=text
24-
PHPMD_PATTERNS=cleancode,codesize,controversial,design,naming,unusedcode
2518
PHPCS_IGNORE=
19+
PHPMD_BIN=/usr/bin/phpmd
20+
PHPMD_OUTPUT=text
21+
PHPMD_PATTERNS_LIST=cleancode,codesize,controversial,design,naming,unusedcode
2622
TMP_STAGING=".tmp_staging"
2723

28-
# parse config
24+
# Parse config
2925
CONFIG_FILE=$(dirname $0)/config
3026
if [ -e $CONFIG_FILE ]; then
3127
. $CONFIG_FILE
3228
fi
3329

34-
# simple check if code sniffer is set up correctly
35-
if [ ! -x $PHPCS_BIN ]; then
36-
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
30+
# Simple check if code sniffer is set up correctly
31+
if [ ! -x $PHPCS_BIN ] || [ ! -x $PHPMD_BIN ]; then
32+
tput setaf 1; echo "Executable not found. Check $PHPCS_BIN and $PHPMD_BIN."
3733
exit 1
3834
fi
3935

40-
# stolen from template file
36+
# Stolen from template file
4137
if git rev-parse --verify HEAD
4238
then
4339
against=HEAD
@@ -46,22 +42,22 @@ else
4642
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
4743
fi
4844

49-
# this is the magic:
50-
# retrieve all files in staging area that are added, modified or renamed
45+
# This is the magic:
46+
# Retrieve all files in staging area that are added, modified or renamed
5147
# but no deletions etc
5248
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- )
5349

5450
if [ "$FILES" == "" ]; then
5551
exit 0
5652
fi
5753

58-
# create temporary copy of staging area
54+
# Create temporary copy of staging area
5955
if [ -e $TMP_STAGING ]; then
6056
rm -rf $TMP_STAGING
6157
fi
6258
mkdir $TMP_STAGING
6359

64-
# match files against whitelist
60+
# Match files against whitelist
6561
FILES_TO_CHECK=""
6662
for FILE in $FILES
6763
do
@@ -77,7 +73,7 @@ if [ "$FILES_TO_CHECK" == "" ]; then
7773
exit 0
7874
fi
7975

80-
# execute the code sniffer
76+
# Validate PHP CodeSniffer variables
8177
if [ "$PHPCS_IGNORE" != "" ]; then
8278
IGNORE="--ignore=$PHPCS_IGNORE"
8379
else
@@ -102,16 +98,30 @@ else
10298
IGNORE_WARNINGS=""
10399
fi
104100

101+
# Validate PHP Mess Detector variables
102+
if [ "$PHPMD_OUTPUT_MODE" != "" ]; then
103+
PHPMD_OUTPUT="$PHPMD_OUTPUT_MODE"
104+
else
105+
PHPMD_OUTPUT="text"
106+
fi
107+
108+
if [ "$PHPMD_PATTERNS" != "" ]; then
109+
PHPMD_PATTERNS_LIST="$PHPMD_PATTERNS"
110+
else
111+
PHPMD_PATTERNS_LIST="cleancode"
112+
fi
113+
114+
105115
# Copy contents of staged version of files to temporary staging area
106116
# because we only want the staged version that will be commited and not
107-
# the version in the working directory
117+
# the version in the working directory.
108118
STAGED_FILES=""
109119
for FILE in $FILES_TO_CHECK
110120
do
111121
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
112122

113-
# create staged version of file in temporary staging area with the same
114-
# path as the original file so that the phpcs ignore filters can be applied
123+
# Create staged version of file in temporary staging area with the same
124+
# path as the original file so that the phpcs ignore filters can be applied.
115125
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
116126
git cat-file blob $ID > "$TMP_STAGING/$FILE"
117127
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
@@ -136,7 +146,7 @@ fi
136146

137147
echo ""
138148
tput setaf 7; echo " :: PHP Mess Detector inspection :: "
139-
PHPMD_OUTPUT=$($PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT_MODE $PHPMD_PATTERNS)
149+
PHPMD_OUTPUT=$($PHPMD_BIN $STAGED_FILES $PHPMD_OUTPUT $PHPMD_PATTERNS_LIST)
140150
PHPMD_RETVAL=$?
141151

142152
if [ $PHPMD_RETVAL -ne 0 ]; then
@@ -152,4 +162,5 @@ fi
152162

153163
tput setaf 7;
154164

165+
echo ""
155166
exit 0;

0 commit comments

Comments
 (0)