Skip to content

Commit 6447666

Browse files
committed
Merge pull request #69 from loadsys/f/codesniffer-custom-ruleset
Enhances codesniffer-run to detect and use a local phpcs.xml.
2 parents 91431c8 + 3842740 commit 6447666

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

codesniffer-run

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ ${0##*/}
1111
to the standard output.
1212
1313
Usage:
14-
bin/${0##*/} [-f|h|n|x]
14+
bin/${0##*/} [-f|h|n|x] [-r StndName]|[-r rules.xml] [dirs|files to sniff]
1515
1616
f - Write full and summary report out to files.
1717
h - Print this help information.
1818
n - Suppress warnings during the sniff run.
1919
x - Always exit zero regardless of sniff results.
2020
21+
r - Use an explicit ruleset.xml file path or coding standard name.
22+
2123
Environment:
2224
CODESNIFFER_RUN_STANDARD - If set, overrides the name of the Coding
2325
Standard to use. The standard is assumed
24-
to be "installed" already.
25-
(Default: Loadsys)
26+
to be "installed" already. The default
27+
standard (and this env var) can also be
28+
overriden using the -r option to
29+
explicitly name a ruleset.xml file or
30+
coding standard name.
31+
(Default: phpcs.xml if present, otherwise
32+
'Loadsys')
2633
2734
EOT
2835

@@ -39,8 +46,11 @@ FULL_REPORT_FILE="${REPORT_DIR}/report-full.txt"
3946
SUMMARY_REPORT_FILE="${REPORT_DIR}/report-summary.txt"
4047

4148
CODE_STANDARD=${CODESNIFFER_RUN_STANDARD:-Loadsys}
49+
if [ -r "phpcs.xml" ]; then
50+
CODE_STANDARD="phpcs.xml"
51+
fi
4252
CODE_STANDARDS=("vendor/cakephp/cakephp-codesniffer" "vendor/loadsys/loadsys_codesniffer")
43-
SNIFF_FOLDERS=("./src" "./plugins" "./tests" "./config" "./webroot")
53+
SNIFF_FOLDERS=("./src" "./tests" "./config" "./webroot" "./plugins")
4454
SNIFF_FAIL_CAUSES_SCRIPT_FAIL=0 # 0 = true. Script will exit with phpcs's return code.
4555

4656
SAVE_REPORTS=1 # 1 = false. DON'T save reports when no args provided.
@@ -49,7 +59,7 @@ SUPPRESS_WARNINGS=""
4959

5060

5161
# Process command line options.
52-
while getopts ":fhnx" opt; do
62+
while getopts ":fhnr:x" opt; do
5363
case $opt in
5464
f)
5565
SAVE_REPORTS=0 # 0 = true. Save reports to files, not print to screen.
@@ -62,6 +72,9 @@ while getopts ":fhnx" opt; do
6272
n)
6373
SUPPRESS_WARNINGS="-n"
6474
;;
75+
r)
76+
CODE_STANDARD="$OPTARG"
77+
;;
6578
x)
6679
SNIFF_FAIL_CAUSES_SCRIPT_FAIL=1 # 1 = false. Always exit 0;
6780
;;
@@ -73,10 +86,16 @@ while getopts ":fhnx" opt; do
7386
done
7487

7588

89+
# Override the files/folders to sniff if any args are left.
90+
shift $(expr $OPTIND - 1 )
91+
if [ $# -gt 0 ]; then
92+
SNIFF_FOLDERS=("$@")
93+
fi
94+
95+
7696
# Make sure phpcs has the sniffs configured.
7797
INSTALLED_ALREADY=$( bin/phpcs --config-show | grep installed_paths )
7898
INSTALLED_ALREADY=${INSTALLED_ALREADY#installed_paths: }
79-
#echo "old = $INSTALLED_ALREADY"
8099

81100
for STANDARD in "${CODE_STANDARDS[@]}"; do
82101
if [[ ! $INSTALLED_ALREADY == *"$STANDARD"* ]]; then
@@ -85,7 +104,6 @@ for STANDARD in "${CODE_STANDARDS[@]}"; do
85104
done
86105

87106
TO_INSTALL=${TO_INSTALL#,}
88-
#echo "new = $TO_INSTALL"
89107
if [ -n "$TO_INSTALL" ] && [ "$TO_INSTALL" != "$INSTALLED_ALREADY" ]; then
90108
echo "## Adding required coding standards installed paths."
91109
bin/phpcs --config-set installed_paths $TO_INSTALL > /dev/null

0 commit comments

Comments
 (0)