-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathrun-phpcs-tests
More file actions
executable file
·106 lines (93 loc) · 3.41 KB
/
Copy pathrun-phpcs-tests
File metadata and controls
executable file
·106 lines (93 loc) · 3.41 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
#!/bin/sh
EXIT_CODE=0
# 1. Run standard PHP code style check if a configuration file exists.
if [ -f ".phpcs.xml" ] || [ -f "phpcs.xml" ] || [ -f ".phpcs.xml.dist" ] || [ -f "phpcs.xml.dist" ]
then
EXTRA_ARGS=""
# Compact, one-line-per-violation output without the progress ticker.
# Useful for CI logs and for AI coding agents, which pay for every token of
# the report they read back.
if [ -n "${WP_CLI_TEST_QUIET}" ]; then
EXTRA_ARGS="-q"
# A report asked for on the command line wins. PHPCS does not let a
# later --report replace an earlier one -- 3.x prints both reports and
# 4.x keeps only the first it saw -- so the compact one has to stay out
# of the way rather than rely on ordering.
case " $* " in
*" --report="*|*" --report-"*) ;;
*) EXTRA_ARGS="${EXTRA_ARGS} --report=emacs" ;;
esac
fi
# shellcheck disable=SC2086 # Intentional word splitting of the optional arguments.
vendor/bin/phpcs $EXTRA_ARGS "$@" || EXIT_CODE=$?
fi
# 2. Run PHPCS over the PHP blocks in .feature files.
# Composer installs this script as a symlink in the vendor binary directory, so
# it has to be resolved before the root of this package can be derived from it.
SOURCE="$0"
while [ -h "$SOURCE" ]
do
SOURCE_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
# A relative symlink is resolved against the directory holding the symlink.
case "$SOURCE" in
/*) ;;
*) SOURCE="$SOURCE_DIR/$SOURCE" ;;
esac
done
DIR="$(cd -P "$(dirname "$SOURCE")/.." && pwd)"
# A ruleset of the same purpose in the package root replaces the defaults
# wholesale. Both scripts use the same ruleset otherwise, so that the check and
# the fixer cannot disagree over which sniff applies to a block.
FEATURE_STANDARD=""
for CANDIDATE in "phpcs-feature-files.xml" "phpcs-feature-files.xml.dist"
do
if [ -f "$CANDIDATE" ]
then
FEATURE_STANDARD="$(pwd)/$CANDIDATE"
break
fi
done
if [ -z "$FEATURE_STANDARD" ]
then
FEATURE_STANDARD="WP_CLI_CS_Feature_Files"
fi
# An argument naming what to check applies to the files of the package itself,
# so the blocks are left alone once a run has been narrowed down to a path.
CHECK_BLOCKS=1
for ARG in "$@"
do
case "$ARG" in
-*) ;;
*) CHECK_BLOCKS=0 ;;
esac
done
if [ "$CHECK_BLOCKS" -eq 1 ] && [ -d "features" ] \
&& [ -f "$DIR/utils/extract-feature-php.php" ]
then
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'feature_phpcs')
PHPCS_OUTPUT=$(mktemp 2>/dev/null || mktemp -t 'feature_phpcs_output')
trap 'rm -rf "$TEMP_DIR" "$PHPCS_OUTPUT"' EXIT HUP INT TERM
# Results are only reported when the extraction they are based on succeeded.
if php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR"
then
if [ -n "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
then
# The report is written to a file so that the status of PHPCS itself
# is preserved instead of the status of the command rewriting it.
# `--basepath` reduces the reported paths to the part that is worth
# showing, which also keeps PHPCS from truncating them from the left
# once they grow past the width of the report.
vendor/bin/phpcs --standard="$FEATURE_STANDARD" \
--basepath="$TEMP_DIR" "$TEMP_DIR" >"$PHPCS_OUTPUT" 2>&1 || EXIT_CODE=$?
# Findings are reported against the feature files they came from.
sed -E \
-e 's|^FILE: |FILE: features/|' \
-e 's/\.feature_L[0-9]+_E[0-9]+_(HASPHP|NOPHP)\.php/.feature/g' \
"$PHPCS_OUTPUT"
fi
else
EXIT_CODE=1
fi
fi
exit $EXIT_CODE