Skip to content

Commit 6a3fbc8

Browse files
committed
Address review feedback on feature file PHP checks
Fixes for the PHP block extraction and synchronization: * Place an added `<?php` tag on the docstring delimiter line, so that it is no longer overwritten by the first line of code and blocks without an opening tag are actually linted as PHP. * Only treat a docstring as PHP when it follows a step creating a PHP file or starts with `<?php`. Expectations such as `Then the wp-config.php file should contain:` are not necessarily valid PHP and must not be reformatted. * Determine the indentation to restore from the first line holding code, as a leading empty line would otherwise be used as indentation. * Remove only the padding added during extraction instead of all leading and trailing empty lines, preserving intentional ones. * Keep empty lines leading up to an opening tag, so that reported line numbers keep matching. * Only remove files from the target directory that a previous extraction created, and refuse target directories that hold the feature files themselves. * Only treat the first argument as an action when it is one, so that passing two directories does not swap source and target. * Report failures on STDERR and through the exit code. * Skip a block instead of writing it back when the extracted file no longer lines up with the feature file. Fixes for the scripts running the checks: * Propagate failures of the extraction, PHPCS, PHPCBF and synchronization steps, and skip synchronization when extraction failed. * Preserve the PHPCS exit status by writing the report to a file instead of piping it through `sed`. * Keep nested directories when normalizing reported feature file paths. Adds unit tests covering extraction and synchronization. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KVnFXuhCGs4NT2A7FmDo4e
1 parent b32a615 commit 6a3fbc8

4 files changed

Lines changed: 833 additions & 64 deletions

File tree

‎bin/run-phpcbf-cleanup‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ then
1515
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'feature_phpcbf')
1616
trap 'rm -rf "$TEMP_DIR"' EXIT HUP INT TERM
1717

18-
php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR" >/dev/null 2>&1
19-
20-
if [ -d "$TEMP_DIR" ] && [ "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
18+
# 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
2120
then
22-
vendor/bin/phpcbf --standard=WP_CLI_CS \
23-
--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 \
24-
"$TEMP_DIR" >/dev/null 2>&1
21+
if [ -d "$TEMP_DIR" ] && [ "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
22+
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 \
25+
"$TEMP_DIR" >/dev/null || EXIT_CODE=$?
2526

26-
php "$DIR/utils/extract-feature-php.php" update features "$TEMP_DIR" >/dev/null 2>&1
27+
php "$DIR/utils/extract-feature-php.php" update features "$TEMP_DIR" >/dev/null || EXIT_CODE=$?
28+
fi
29+
else
30+
EXIT_CODE=1
2731
fi
2832
fi
2933

‎bin/run-phpcs-tests‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,30 @@ DIR="$(cd "$(dirname "$0")/.." && pwd)"
1313
if [ -d "features" ] && [ -f "$DIR/utils/extract-feature-php.php" ]
1414
then
1515
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'feature_phpcs')
16-
trap 'rm -rf "$TEMP_DIR"' EXIT HUP INT TERM
16+
PHPCS_OUTPUT=$(mktemp 2>/dev/null || mktemp -t 'feature_phpcs_output')
17+
trap 'rm -rf "$TEMP_DIR" "$PHPCS_OUTPUT"' EXIT HUP INT TERM
1718

18-
php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR" >/dev/null 2>&1
19-
20-
if [ -d "$TEMP_DIR" ] && [ "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
19+
if php "$DIR/utils/extract-feature-php.php" extract features "$TEMP_DIR" >/dev/null
2120
then
22-
vendor/bin/phpcs --standard=WP_CLI_CS --warning-severity=0 \
23-
--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 \
24-
"$TEMP_DIR" 2>&1 | sed -E 's/\.feature_L[0-9]+_E[0-9]+_(HASPHP|NOPHP)\.php/.feature/g' | sed -E "s|FILE: .*/([^/]+\.feature)|FILE: features/\1|g" || EXIT_CODE=$?
21+
if [ -d "$TEMP_DIR" ] && [ "$(ls -A "$TEMP_DIR" 2>/dev/null)" ]
22+
then
23+
# 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)
31+
32+
sed -E \
33+
-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" \
36+
"$PHPCS_OUTPUT"
37+
fi
38+
else
39+
EXIT_CODE=1
2540
fi
2641
fi
2742

0 commit comments

Comments
 (0)