-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-code.sh
More file actions
executable file
·135 lines (121 loc) · 3.16 KB
/
validate-code.sh
File metadata and controls
executable file
·135 lines (121 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DEBUG="FALSE"
PHP_CS=${DIR}/vendor/bin/phpcs
PHP_MD=${DIR}/vendor/bin/phpmd
PHP_CPD=${DIR}/vendor/bin/phpcpd
PHP_CS_CODING_STANDARD=${DIR}/vendor/drupal/coder/coder_sniffer
######################################
# Checks if a variable is an array.
#
# @see https://gist.github.com/coderofsalvation/8377369
#
# Returns:
# 0 on sucess, 1 otherwise
######################################
is_array() {
[ -z "$1" ] && return 1
if [ -n "$BASH" ]; then
declare -p ${1} 2> /dev/null | grep 'declare \-a' >/dev/null && echo 0
fi
echo 1
}
######################################
# Prints data if debug is enabled.
######################################
debug() {
if [ "$DEBUG" == "TRUE" ]; then
echo "[STEP] $1"
IS_ARRAY=`is_array "$2"`
if [ "$IS_ARRAY" == "1" ]; then
for DATA in $2
do
echo "- processing ${DATA} ..."
done
else
echo "- processing $2 ..."
fi
fi
}
######################################
# Validates through mess detector
#
# Arguments:
# FILES array of files to check.
# CONTINUE_ON_FAIL if we don't want to stop after the first error.
######################################
validateMessDetector() {
FILES=$1
CONTINUE_ON_FAIL=$2
if [ ! -x $PHP_MD ]; then
echo "PHP Mess detector bin not found or executable -> $PHP_MD"
exit 1
fi
echo "Checking PHP Mess Detector..."
for FILE in $FILES
do
if [[ $FILE != /* ]] ;
then
FILE=$PROJECT/$FILE
fi
echo "- Checking $FILE ..."
$PHP_MD $FILE text ${DIR}/rules.xml
if [ $? != 0 ] && [ "$CONTINUE_ON_FAIL" == "" ]
then
# The PHPMD errors are reviewed, this does not block the commit.
echo "Review the errors."
exit $?
fi
done
}
######################################
# Validates through code sniffer
#
# Arguments:
# FILES array of files to check.
######################################
validateCodeSniffer() {
FILES=$1
if [ ! -x $PHP_CS ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHP_CS"
exit 1
fi
debug "Running Code Sniffer..." "$FILES"
$PHP_CS --config-set installed_paths $PHP_CS_CODING_STANDARD
if [ "$FILES" != "" ]
then
# The PHPCS errors are reviewed, this does not block the commit.
$PHP_CS --standard=Drupal $FILES
if [ $? != 0 ]
then
echo "Review the errors."
exit $?
fi
fi
}
######################################
# Validates through copy paste detector
#
# Globals:
# PROJECT_PATHS to analyse
######################################
validateCopyPasteDetector() {
debug "Running Copy Paste detector..." "$PROJECT_PATHS"
INCLUDE=`echo "$PROJECT_INCLUDE" | sed -e 's|\\\|\*|g' | sed -e "s/|/,/g"`
$PHP_CPD --names=$INCLUDE --names-exclude="*views_default.inc" $PROJECT_PATHS
if [ $? != 0 ]
then
# The PHPCPD errors are never allowed, they block the commit.
echo "Commit blocked. Fix the copy paste errors."
exit 1
fi
exit $?
}
######################################
# Executes a full validation.
######################################
validateCode() {
validateMessDetector "$@"
validateCodeSniffer "$@"
validateCopyPasteDetector
}