Skip to content

Commit 94222b2

Browse files
committed
Make the feature file code style check work outside this package
The root of this package was derived from `dirname "$0"`, but Composer installs these scripts as a symlink in the vendor binary directory, where that resolves to `vendor` instead. `vendor/utils/extract-feature-php.php` does not exist, so the check was skipped in every package using the testing framework and only ever ran here, where `bin` and `utils` are siblings. The symlink is now resolved first, the way `run-phpstan-tests` already does. PHPCS truncates a reported path from the left once it grows past the width of the report, which happened before the temporary directory was rewritten out of it and left findings pointing at `...N1dTe6Y/some/feature.feature`. Passing `--basepath` reduces the paths to the part worth showing, so the report needs a single anchored substitution and no longer depends on the length of a path it does not control. The list of sniffs that do not apply to a block was spelled out in both scripts. A future edit to one of them would have had the fixer rewrite feature files over something the check never reports, so the list moves to `phpcs/feature-files.sh`, which both read. That also gets the fixer the `--warning-severity=0` only the check was passing. A package replaces the defaults by adding a `phpcs-feature-files.xml` ruleset to its root. The blocks are left alone when a run is narrowed down to a path, as in `composer phpcs -- src/`, since such an argument is about the files of the package itself rather than about its feature files.
1 parent 3c60271 commit 94222b2

3 files changed

Lines changed: 175 additions & 21 deletions

File tree

‎bin/run-phpcbf-cleanup‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,68 @@ then
88
vendor/bin/phpcbf "$@" || EXIT_CODE=$?
99
fi
1010

11-
# 2. Run PHPCBF over extracted PHP blocks in .feature files and sync back fixes.
12-
DIR="$(cd "$(dirname "$0")/.." && pwd)"
13-
if [ -d "features" ] && [ -f "$DIR/utils/extract-feature-php.php" ]
11+
# 2. Run PHPCBF over the PHP blocks in .feature files and sync back fixes.
12+
# Composer installs this script as a symlink in the vendor binary directory, so
13+
# it has to be resolved before the root of this package can be derived from it.
14+
SOURCE="$0"
15+
while [ -h "$SOURCE" ]
16+
do
17+
SOURCE_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
18+
SOURCE="$(readlink "$SOURCE")"
19+
# A relative symlink is resolved against the directory holding the symlink.
20+
case "$SOURCE" in
21+
/*) ;;
22+
*) SOURCE="$SOURCE_DIR/$SOURCE" ;;
23+
esac
24+
done
25+
DIR="$(cd -P "$(dirname "$SOURCE")/.." && pwd)"
26+
27+
# A ruleset of the same purpose in the package root replaces the defaults
28+
# wholesale. Both scripts read the defaults from the same file, so that the
29+
# check and the fixer cannot disagree over which sniff applies to a block.
30+
FEATURE_STANDARD=""
31+
for CANDIDATE in "phpcs-feature-files.xml" "phpcs-feature-files.xml.dist"
32+
do
33+
if [ -f "$CANDIDATE" ]
34+
then
35+
FEATURE_STANDARD="$(pwd)/$CANDIDATE"
36+
break
37+
fi
38+
done
39+
40+
FEATURE_ARGS=""
41+
if [ -z "$FEATURE_STANDARD" ] && [ -f "$DIR/phpcs/feature-files.sh" ]
42+
then
43+
. "$DIR/phpcs/feature-files.sh"
44+
FEATURE_STANDARD="$WP_CLI_TESTS_FEATURE_STANDARD"
45+
# Holds no path, so leaving it unquoted below splits it into arguments.
46+
FEATURE_ARGS="$WP_CLI_TESTS_FEATURE_ARGS"
47+
fi
48+
49+
# An argument naming what to fix applies to the files of the package itself, so
50+
# the blocks are left alone once a run has been narrowed down to a path.
51+
FIX_BLOCKS=1
52+
for ARG in "$@"
53+
do
54+
case "$ARG" in
55+
-*) ;;
56+
*) FIX_BLOCKS=0 ;;
57+
esac
58+
done
59+
60+
if [ "$FIX_BLOCKS" -eq 1 ] && [ -d "features" ] && [ -n "$FEATURE_STANDARD" ] \
61+
&& [ -f "$DIR/utils/extract-feature-php.php" ]
1462
then
1563
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'feature_phpcbf')
1664
trap 'rm -rf "$TEMP_DIR"' EXIT HUP INT TERM
1765

1866
# Fixes are only synced back when the extraction they are based on succeeded.
19-
if php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR" >/dev/null
67+
if php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR"
2068
then
21-
if [ -d "$TEMP_DIR" ] && [ "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
69+
if [ -n "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
2270
then
23-
vendor/bin/phpcbf --standard=WP_CLI_CS \
24-
--exclude=Generic.Files.InlineHTML,Generic.Files.LineEndings,WordPress.Files.FileName,Squiz.Commenting.FileComment,Universal.WhiteSpace.PrecisionAlignment,PSR2.Files.EndFileNewline,PSR2.Methods.FunctionClosingBrace,Generic.PHP.CharacterBeforePHPOpenTag,Generic.PHP.RequireStrictTypes,Squiz.WhiteSpace.SuperfluousWhitespace,WordPress.NamingConventions.PrefixAllGlobals,Universal.Files.SeparateFunctionsFromOO,Generic.Files.OneObjectStructurePerFile,WordPress.WP.GlobalVariablesOverride,Universal.Namespaces.OneDeclarationPerFile,Universal.Namespaces.DisallowCurlyBraceSyntax,WordPress.PHP.YodaConditions,Universal.Namespaces.DisallowDeclarationWithoutName,PSR12.Files.FileHeader,Generic.CodeAnalysis.EmptyStatement \
71+
# shellcheck disable=SC2086 # Intentional word splitting.
72+
vendor/bin/phpcbf --standard="$FEATURE_STANDARD" $FEATURE_ARGS \
2573
"$TEMP_DIR" >/dev/null || EXIT_CODE=$?
2674

2775
php "$DIR/utils/extract-feature-php.php" update features "$TEMP_DIR" >/dev/null || EXIT_CODE=$?

‎bin/run-phpcs-tests‎

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,80 @@ then
88
vendor/bin/phpcs "$@" || EXIT_CODE=$?
99
fi
1010

11-
# 2. Run PHPCS over extracted PHP blocks in .feature files if features/ directory exists.
12-
DIR="$(cd "$(dirname "$0")/.." && pwd)"
13-
if [ -d "features" ] && [ -f "$DIR/utils/extract-feature-php.php" ]
11+
# 2. Run PHPCS over the PHP blocks in .feature files.
12+
# Composer installs this script as a symlink in the vendor binary directory, so
13+
# it has to be resolved before the root of this package can be derived from it.
14+
SOURCE="$0"
15+
while [ -h "$SOURCE" ]
16+
do
17+
SOURCE_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
18+
SOURCE="$(readlink "$SOURCE")"
19+
# A relative symlink is resolved against the directory holding the symlink.
20+
case "$SOURCE" in
21+
/*) ;;
22+
*) SOURCE="$SOURCE_DIR/$SOURCE" ;;
23+
esac
24+
done
25+
DIR="$(cd -P "$(dirname "$SOURCE")/.." && pwd)"
26+
27+
# A ruleset of the same purpose in the package root replaces the defaults
28+
# wholesale. Both scripts read the defaults from the same file, so that the
29+
# check and the fixer cannot disagree over which sniff applies to a block.
30+
FEATURE_STANDARD=""
31+
for CANDIDATE in "phpcs-feature-files.xml" "phpcs-feature-files.xml.dist"
32+
do
33+
if [ -f "$CANDIDATE" ]
34+
then
35+
FEATURE_STANDARD="$(pwd)/$CANDIDATE"
36+
break
37+
fi
38+
done
39+
40+
FEATURE_ARGS=""
41+
if [ -z "$FEATURE_STANDARD" ] && [ -f "$DIR/phpcs/feature-files.sh" ]
42+
then
43+
. "$DIR/phpcs/feature-files.sh"
44+
FEATURE_STANDARD="$WP_CLI_TESTS_FEATURE_STANDARD"
45+
# Holds no path, so leaving it unquoted below splits it into arguments.
46+
FEATURE_ARGS="$WP_CLI_TESTS_FEATURE_ARGS"
47+
fi
48+
49+
# An argument naming what to check applies to the files of the package itself,
50+
# so the blocks are left alone once a run has been narrowed down to a path.
51+
CHECK_BLOCKS=1
52+
for ARG in "$@"
53+
do
54+
case "$ARG" in
55+
-*) ;;
56+
*) CHECK_BLOCKS=0 ;;
57+
esac
58+
done
59+
60+
if [ "$CHECK_BLOCKS" -eq 1 ] && [ -d "features" ] && [ -n "$FEATURE_STANDARD" ] \
61+
&& [ -f "$DIR/utils/extract-feature-php.php" ]
1462
then
1563
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'feature_phpcs')
1664
PHPCS_OUTPUT=$(mktemp 2>/dev/null || mktemp -t 'feature_phpcs_output')
1765
trap 'rm -rf "$TEMP_DIR" "$PHPCS_OUTPUT"' EXIT HUP INT TERM
1866

19-
if php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR" >/dev/null
67+
# Results are only reported when the extraction they are based on succeeded.
68+
if php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR"
2069
then
21-
if [ -d "$TEMP_DIR" ] && [ "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
70+
if [ -n "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
2271
then
2372
# The report is written to a file so that the status of PHPCS itself
24-
# is preserved instead of the status of the commands rewriting it.
25-
vendor/bin/phpcs --standard=WP_CLI_CS --warning-severity=0 \
26-
--exclude=Generic.Files.InlineHTML,Generic.Files.LineEndings,WordPress.Files.FileName,Squiz.Commenting.FileComment,Universal.WhiteSpace.PrecisionAlignment,PSR2.Files.EndFileNewline,PSR2.Methods.FunctionClosingBrace,Generic.PHP.CharacterBeforePHPOpenTag,Generic.PHP.RequireStrictTypes,Squiz.WhiteSpace.SuperfluousWhitespace,WordPress.NamingConventions.PrefixAllGlobals,Universal.Files.SeparateFunctionsFromOO,Generic.Files.OneObjectStructurePerFile,WordPress.WP.GlobalVariablesOverride,Universal.Namespaces.OneDeclarationPerFile,Universal.Namespaces.DisallowCurlyBraceSyntax,WordPress.PHP.YodaConditions,Universal.Namespaces.DisallowDeclarationWithoutName,PSR12.Files.FileHeader,Generic.CodeAnalysis.EmptyStatement \
27-
"$TEMP_DIR" >"$PHPCS_OUTPUT" 2>&1 || EXIT_CODE=$?
28-
29-
# The temporary directory is reported through its resolved path.
30-
TEMP_DIR_REAL=$(cd "$TEMP_DIR" && pwd -P)
73+
# is preserved instead of the status of the command rewriting it.
74+
# `--basepath` reduces the reported paths to the part that is worth
75+
# showing, which also keeps PHPCS from truncating them from the left
76+
# once they grow past the width of the report.
77+
# shellcheck disable=SC2086 # Intentional word splitting.
78+
vendor/bin/phpcs --standard="$FEATURE_STANDARD" $FEATURE_ARGS \
79+
--basepath="$TEMP_DIR" "$TEMP_DIR" >"$PHPCS_OUTPUT" 2>&1 || EXIT_CODE=$?
3180

81+
# Findings are reported against the feature files they came from.
3282
sed -E \
83+
-e 's|^FILE: |FILE: features/|' \
3384
-e 's/\.feature_L[0-9]+_E[0-9]+_(HASPHP|NOPHP)\.php/.feature/g' \
34-
-e "s|$TEMP_DIR_REAL/|features/|g" \
35-
-e "s|$TEMP_DIR/|features/|g" \
3685
"$PHPCS_OUTPUT"
3786
fi
3887
else

‎phpcs/feature-files.sh‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Defaults for the code style check of the PHP blocks embedded in Behat feature
2+
# files, shared by `run-phpcs-tests` and `run-phpcbf-cleanup`.
3+
#
4+
# Keeping the list in one place is what makes the check and the fixer agree: a
5+
# sniff excluded for one but not the other would have the fixer rewrite feature
6+
# files over something the check never reports, or have the check report
7+
# something the fixer refuses to touch.
8+
#
9+
# The exclusions are passed on the command line rather than declared in a
10+
# ruleset because a ruleset aborts the whole run over a sniff that the installed
11+
# PHP_CodeSniffer does not know, while `--exclude` passes over it. The list
12+
# spans several major versions of PHP_CodeSniffer and of the standards it
13+
# builds on, and not every entry exists in all of them.
14+
#
15+
# A package replaces these defaults wholesale by adding a
16+
# `phpcs-feature-files.xml` (or `phpcs-feature-files.xml.dist`) ruleset to its
17+
# root, which is then used as the standard instead.
18+
19+
WP_CLI_TESTS_FEATURE_STANDARD="WP_CLI_CS"
20+
21+
# Warnings are advisory, and the fixer must not rewrite a feature file over
22+
# something the check does not report.
23+
WP_CLI_TESTS_FEATURE_ARGS="--warning-severity=0"
24+
25+
# A block is not a file. It is padded with one empty line per preceding line of
26+
# the feature file so that reported line numbers match it, and one that does not
27+
# bring its own opening tag is given one. Neither is part of the snippet, and
28+
# the shared docstring indentation is taken off before the check and put back
29+
# afterwards, so none of the sniffs looking at a file as a whole apply.
30+
WP_CLI_TESTS_FEATURE_EXCLUDES="Generic.Files.InlineHTML"
31+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Generic.PHP.CharacterBeforePHPOpenTag"
32+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Generic.Files.LineEndings"
33+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,PSR2.Files.EndFileNewline"
34+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,PSR12.Files.FileHeader"
35+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Squiz.Commenting.FileComment"
36+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Generic.PHP.RequireStrictTypes"
37+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,WordPress.Files.FileName"
38+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Universal.WhiteSpace.PrecisionAlignment"
39+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Squiz.WhiteSpace.SuperfluousWhitespace"
40+
41+
# A block is a fixture, not production code. Snippets exist to set up a
42+
# scenario, run inside a throwaway WordPress installation, are written to be
43+
# read at a glance, and are routinely a single class or function on their own.
44+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,WordPress.NamingConventions.PrefixAllGlobals"
45+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,WordPress.WP.GlobalVariablesOverride"
46+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,WordPress.PHP.YodaConditions"
47+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Universal.Files.SeparateFunctionsFromOO"
48+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Generic.Files.OneObjectStructurePerFile"
49+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Universal.Namespaces.OneDeclarationPerFile"
50+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Universal.Namespaces.DisallowCurlyBraceSyntax"
51+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Universal.Namespaces.DisallowDeclarationWithoutName"
52+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,PSR2.Methods.FunctionClosingBrace"
53+
54+
# A snippet testing error handling is deliberately incomplete.
55+
WP_CLI_TESTS_FEATURE_EXCLUDES="$WP_CLI_TESTS_FEATURE_EXCLUDES,Generic.CodeAnalysis.EmptyStatement"
56+
57+
WP_CLI_TESTS_FEATURE_ARGS="$WP_CLI_TESTS_FEATURE_ARGS --exclude=$WP_CLI_TESTS_FEATURE_EXCLUDES"

0 commit comments

Comments
 (0)