-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodesniffer-run
executable file
·141 lines (114 loc) · 3.83 KB
/
codesniffer-run
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
136
137
138
139
140
141
#!/usr/bin/env bash
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
Convenience wrapper around code sniffing tool. If \$1 is provided, a
summary and full reports will be generated, rather than being output
to the standard output.
Usage:
bin/${0##*/} [-f|h|n|s|x] [-r StndName]|[-r rules.xml] [dirs|files to sniff]
f - Write full and summary report out to files.
h - Print this help information.
n - Suppress warnings during the sniff run.
s - Strict mode. Reduces the threshold for WARNINGS, making them
visible for review.
x - Always exit zero regardless of sniff results.
r - Use an explicit ruleset.xml file path or coding standard name.
Environment:
CODESNIFFER_RUN_STANDARD - If set, overrides the name of the Coding
Standard to use. The standard is assumed
to be "installed" already. The default
standard (and this env var) can also be
overriden using the -r option to
explicitly name a ruleset.xml file or
coding standard name.
(Default: phpcs.xml if present, otherwise
'Loadsys')
EOT
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
# Set up local variables.
umask a+rw
TMP_DIR="tmp"
REPORT_DIR="${TMP_DIR}/code-sniffs"
FULL_REPORT_FILE="${REPORT_DIR}/report-full.txt"
SUMMARY_REPORT_FILE="${REPORT_DIR}/report-summary.txt"
CODE_STANDARD=${CODESNIFFER_RUN_STANDARD:-Loadsys}
if [ -r "phpcs.xml" ]; then
CODE_STANDARD="phpcs.xml"
fi
CODE_STANDARDS=("vendor/cakephp/cakephp-codesniffer" "vendor/loadsys/loadsys_codesniffer")
SNIFF_FOLDERS=("./src" "./tests" "./config" "./webroot" "./plugins")
SNIFF_FAIL_CAUSES_SCRIPT_FAIL=0 # 0 = true. Script will exit with phpcs's return code.
SAVE_REPORTS=1 # 1 = false. DON'T save reports when no args provided.
COVERAGE="--report-full --report-summary"
STRICT_MODE=""
SUPPRESS_WARNINGS=""
# Process command line options.
while getopts ":fhnr:sx" opt; do
case $opt in
f)
SAVE_REPORTS=0 # 0 = true. Save reports to files, not print to screen.
COVERAGE="--report-full=${FULL_REPORT_FILE} --report-summary=${SUMMARY_REPORT_FILE}"
mkdir -p "$REPORT_DIR"
;;
h)
usage 0
;;
n)
SUPPRESS_WARNINGS="-n"
;;
r)
CODE_STANDARD="$OPTARG"
;;
s)
STRICT_MODE="--warning-severity=1"
;;
x)
SNIFF_FAIL_CAUSES_SCRIPT_FAIL=1 # 1 = false. Always exit 0;
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage 1
;;
esac
done
# Override the files/folders to sniff if any args are left.
shift $(expr $OPTIND - 1 )
if [ $# -gt 0 ]; then
SNIFF_FOLDERS=("$@")
fi
# Make sure phpcs has the sniffs configured.
INSTALLED_ALREADY=$( bin/phpcs --config-show | grep installed_paths )
INSTALLED_ALREADY=${INSTALLED_ALREADY#installed_paths: }
for STANDARD in "${CODE_STANDARDS[@]}"; do
if [[ ! $INSTALLED_ALREADY == *"$STANDARD"* ]]; then
TO_INSTALL="${TO_INSTALL-},${STANDARD}"
fi
done
TO_INSTALL=${TO_INSTALL#,}
if [ -n "$TO_INSTALL" ] && [ "$TO_INSTALL" != "$INSTALLED_ALREADY" ]; then
echo "## Adding required coding standards installed paths."
bin/phpcs --config-set installed_paths $TO_INSTALL > /dev/null
fi
# Run the sniffs.
echo "## Executing code sniffer:"
bin/phpcs -ps $SUPPRESS_WARNINGS $STRICT_MODE \
--colors \
--extensions=php \
--standard="$CODE_STANDARD" \
${COVERAGE} \
${SNIFF_FOLDERS[@]}
SNIFF_RESULT=$?
if [ $SAVE_REPORTS -eq 0 ]; then
echo "## Full report created at: ${FULL_REPORT_FILE}"
echo "## Summary report created at: ${SUMMARY_REPORT_FILE}"
fi
# Exit based on whether sniff fails should count as a "failure".
if [ $SNIFF_FAIL_CAUSES_SCRIPT_FAIL -eq 0 ]; then
exit $SNIFF_RESULT
else
exit 0
fi