Skip to content

Commit e94758a

Browse files
committed
Address review feedback on feature file analysis
Fixes the defects the review surfaced, all of which are reproducible: * Map the errors PHPStan reports through a canonical path instead of a string prefix. The path it reports is not necessarily spelled the way the extraction wrote it -- macOS resolves `/var` to `/private/var` and Windows has a short and a long form of a directory name -- which made every finding come out as `Unexpected file` and failed the unit tests on both platforms. * Resolve the symlink Composer installs in the vendor binary directory before deriving the root of this package from it. Without that the analysis was silently skipped for every package using the framework, since it looked for the extraction script under `vendor/utils` and never found it. * Keep a package whose feature files hold no PHP block at all from failing. No batch means no results, which left the glob unexpanded and made the report treat the pattern itself as a missing file. * Guard the fields of the PHPStan results before reading them. * Only pass PHPStan's own output through when it produced no results, so its notes about interpreting errors do not bury the report. * Skip the dot entries while looking for feature files. The rule that a docstring opening with `<?php` counts as PHP whichever step precedes it is kept: it covers PHP files that are not named `*.php`, such as the `.maintenance` file. The documentation now says so instead of claiming that only a `.php` file step is considered. Also corrects the documented way to ignore an error, which matched nothing: the pattern applies to the extracted file, whose name carries the line range and is relative to the `features` directory. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M4oxq4G2FxhwJfHd3w6VLw
1 parent 515ed32 commit e94758a

5 files changed

Lines changed: 238 additions & 43 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,49 @@ those snippets as well. The blocks are extracted into standalone PHP files that
118118
line numbers match the feature file, which is what allows errors to be reported against the feature
119119
file itself:
120120
121-
```
121+
```text
122122
features/command.feature
123123
438 Parameter #1 $message of static method WP_CLI::log() expects string, int<0, max> given.
124124
🪪 argument.type
125125
```
126126
127127
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
128-
it wants to change. An empty set of parameters is enough to run with the defaults:
128+
it wants to change. An empty file is enough to run with the defaults, and a level of its own looks
129+
like this:
129130
130-
```yaml
131+
```neon
131132
parameters:
132-
level: 5
133+
level: 4
133134
```
134135
135136
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
136137
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
137-
not actually wrong, such as functions a scenario deliberately leaves undefined:
138+
not actually wrong, such as functions a scenario deliberately leaves undefined.
139+
140+
An ignore matches against the extracted file rather than the feature file it came from. Those files
141+
are named `<feature file>_L<first line>_E<last line>.php`, with the feature file relative to the
142+
`features` directory, so ignoring an error for a whole feature file takes a pattern:
138143
139-
```yaml
144+
```neon
140145
parameters:
141-
level: 5
142146
ignoreErrors:
143147
-
144148
identifier: function.notFound
145-
path: features/shutdown-handler.feature
149+
path: */shutdown-handler.feature_L*.php
146150
```
147151
152+
Since the blocks are analysed in more than one run (see below), an ignore that no run matches is not
153+
reported. A pattern that matches nothing at all therefore goes unnoticed, so it is worth checking
154+
that the error it targets is really gone.
155+
148156
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
149157
150158
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
151159
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
152160
stops analysing altogether when a single file fails to parse, so these have to be skipped.
153-
* Docstrings that do not belong to a step creating a `.php` file, since those are not necessarily
154-
PHP at all.
161+
* Docstrings that neither belong to a step creating a `.php` file nor open with `<?php`, since those
162+
are not necessarily PHP at all. The second rule covers PHP files that are not named `*.php`, such
163+
as the `.maintenance` file of a WordPress installation.
155164
156165
Blocks that declare the same class or function as another block are analysed separately from each
157166
other, so that PHPStan does not resolve a name to the wrong block's declaration.

‎README.md‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,40 +129,49 @@ those snippets as well. The blocks are extracted into standalone PHP files that
129129
line numbers match the feature file, which is what allows errors to be reported against the feature
130130
file itself:
131131
132-
```
132+
```text
133133
features/command.feature
134134
438 Parameter #1 $message of static method WP_CLI::log() expects string, int<0, max> given.
135135
🪪 argument.type
136136
```
137137
138138
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
139-
it wants to change. An empty set of parameters is enough to run with the defaults:
139+
it wants to change. An empty file is enough to run with the defaults, and a level of its own looks
140+
like this:
140141
141-
```yaml
142+
```neon
142143
parameters:
143-
level: 5
144+
level: 4
144145
```
145146
146147
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
147148
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
148-
not actually wrong, such as functions a scenario deliberately leaves undefined:
149+
not actually wrong, such as functions a scenario deliberately leaves undefined.
150+
151+
An ignore matches against the extracted file rather than the feature file it came from. Those files
152+
are named `<feature file>_L<first line>_E<last line>.php`, with the feature file relative to the
153+
`features` directory, so ignoring an error for a whole feature file takes a pattern:
149154
150-
```yaml
155+
```neon
151156
parameters:
152-
level: 5
153157
ignoreErrors:
154158
-
155159
identifier: function.notFound
156-
path: features/shutdown-handler.feature
160+
path: */shutdown-handler.feature_L*.php
157161
```
158162
163+
Since the blocks are analysed in more than one run (see below), an ignore that no run matches is not
164+
reported. A pattern that matches nothing at all therefore goes unnoticed, so it is worth checking
165+
that the error it targets is really gone.
166+
159167
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
160168
161169
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
162170
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
163171
stops analysing altogether when a single file fails to parse, so these have to be skipped.
164-
* Docstrings that do not belong to a step creating a `.php` file, since those are not necessarily
165-
PHP at all.
172+
* Docstrings that neither belong to a step creating a `.php` file nor open with `<?php`, since those
173+
are not necessarily PHP at all. The second rule covers PHP files that are not named `*.php`, such
174+
as the `.maintenance` file of a WordPress installation.
166175
167176
Blocks that declare the same class or function as another block are analysed separately from each
168177
other, so that PHPStan does not resolve a name to the wrong block's declaration.

‎bin/run-phpstan-tests‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@ then
99
fi
1010

1111
# 2. Run PHPStan over the PHP blocks in .feature files if the package opted in.
12-
DIR="$(cd "$(dirname "$0")/.." && pwd)"
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)"
1326
FEATURE_CONFIG=""
1427
for CANDIDATE in "phpstan-feature-files.neon" "phpstan-feature-files.neon.dist"
1528
do
@@ -49,21 +62,32 @@ then
4962
do
5063
[ -d "$BATCH" ] || continue
5164

65+
NAME="$(basename "$BATCH")"
66+
5267
vendor/bin/phpstan --memory-limit=2048M analyse \
5368
--configuration="$TEMP_DIR/phpstan-feature-files.neon" \
5469
--error-format=json \
5570
--no-progress \
56-
"$BATCH" > "$TEMP_DIR/$(basename "$BATCH").json" 2>"$TEMP_DIR/$(basename "$BATCH").stderr"
71+
"$BATCH" > "$TEMP_DIR/$NAME.json" 2>"$TEMP_DIR/$NAME.stderr"
5772

58-
# Anything PHPStan itself has to say goes to the terminal, while its
59-
# findings are reported against the feature files they came from.
60-
if [ -s "$TEMP_DIR/$(basename "$BATCH").stderr" ]
73+
# The findings are reported against the feature files they came from,
74+
# so PHPStan's own output is only of interest when it produced none.
75+
if [ ! -s "$TEMP_DIR/$NAME.json" ] && [ -s "$TEMP_DIR/$NAME.stderr" ]
6176
then
62-
cat "$TEMP_DIR/$(basename "$BATCH").stderr" >&2
77+
cat "$TEMP_DIR/$NAME.stderr" >&2
6378
fi
6479
done
6580

66-
php "$DIR/utils/phpstan-feature-files.php" report "$TEMP_DIR/blocks" "$TEMP_DIR"/batch*.json || EXIT_CODE=$?
81+
# The glob stays unexpanded when no batch produced results, which is the
82+
# case for a package whose feature files hold no PHP block at all. The
83+
# report is still worth running, as it lists the blocks that were skipped.
84+
set -- "$TEMP_DIR"/batch*.json
85+
if [ -f "$1" ]
86+
then
87+
php "$DIR/utils/phpstan-feature-files.php" report "$TEMP_DIR/blocks" "$@" || EXIT_CODE=$?
88+
else
89+
php "$DIR/utils/phpstan-feature-files.php" report "$TEMP_DIR/blocks" || EXIT_CODE=$?
90+
fi
6791
else
6892
EXIT_CODE=1
6993
fi

‎tests/tests/TestPhpStanFeatureFiles.php‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,119 @@ public function test_report_maps_errors_back_onto_the_feature_file(): void {
633633
$this->assertStringContainsString( 'Found 1 error(s)', $result['output'] );
634634
}
635635

636+
/**
637+
* The path PHPStan reports is not necessarily spelled the way the extraction
638+
* wrote it: macOS resolves `/var` to `/private/var`, Windows has a short and
639+
* a long form of a directory name.
640+
*/
641+
public function test_report_maps_errors_from_a_differently_spelled_path(): void {
642+
$this->create_feature_file(
643+
'example.feature',
644+
"Feature: Example\n"
645+
. " Scenario: A PHP block\n"
646+
. " Given a test.php file:\n"
647+
. " \"\"\"\n"
648+
. " <?php\n"
649+
. " echo \$undefined;\n"
650+
. " \"\"\"\n"
651+
);
652+
653+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
654+
655+
$resolved = realpath( $this->target_dir );
656+
657+
$this->assertNotFalse( $resolved );
658+
659+
// Report the file through a spelling that differs from the one the
660+
// extraction was given, the way the platforms above do it. The `/./`
661+
// segment reproduces that on every platform.
662+
file_put_contents(
663+
$this->temp_dir . '/batch0.json',
664+
(string) json_encode(
665+
array(
666+
'totals' => array(
667+
'errors' => 0,
668+
'file_errors' => 1,
669+
),
670+
'files' => array(
671+
str_replace( '\\', '/', $resolved ) . '/./batch0/example.feature_L4_E7.php' => array(
672+
'errors' => 1,
673+
'messages' => array(
674+
array(
675+
'message' => 'Variable $undefined might not be defined.',
676+
'line' => 6,
677+
'ignorable' => true,
678+
'identifier' => 'variable.undefined',
679+
),
680+
),
681+
),
682+
),
683+
'errors' => array(),
684+
)
685+
)
686+
);
687+
688+
$result = $this->run_script( array( 'report', 'extracted', 'batch0.json' ) );
689+
690+
$this->assertSame( 1, $result['exit_code'] );
691+
$this->assertStringNotContainsString( 'Unexpected file', $result['output'] );
692+
$this->assertStringContainsString( 'features/example.feature', str_replace( '\\', '/', $result['output'] ) );
693+
}
694+
695+
public function test_report_tolerates_results_without_messages(): void {
696+
$this->create_feature_file(
697+
'example.feature',
698+
"Feature: Example\n"
699+
. " Scenario: A PHP block\n"
700+
. " Given a test.php file:\n"
701+
. " \"\"\"\n"
702+
. " <?php\n"
703+
. " \$foo = 'bar';\n"
704+
. " \"\"\"\n"
705+
);
706+
707+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
708+
709+
file_put_contents(
710+
$this->temp_dir . '/batch0.json',
711+
(string) json_encode(
712+
array(
713+
'files' => array(
714+
$this->target_dir . '/batch0/example.feature_L4_E7.php' => array( 'errors' => 1 ),
715+
),
716+
)
717+
)
718+
);
719+
720+
$result = $this->run_script( array( 'report', 'extracted', 'batch0.json' ) );
721+
722+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
723+
$this->assertStringNotContainsString( 'Warning', $result['output'] );
724+
}
725+
726+
public function test_report_without_results_lists_skipped_blocks(): void {
727+
$this->create_feature_file(
728+
'example.feature',
729+
"Feature: Example\n"
730+
. " Scenario: A block holding a Behat placeholder\n"
731+
. " Given a placeholder.php file:\n"
732+
. " \"\"\"\n"
733+
. " <?php\n"
734+
. " var_export( get_the_title( {POST_ID} ) );\n"
735+
. " \"\"\"\n"
736+
);
737+
738+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
739+
740+
// A package whose feature files hold no analysable block produces no
741+
// PHPStan results at all, which must not be reported as a failure.
742+
$result = $this->run_script( array( 'report', 'extracted' ) );
743+
744+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
745+
$this->assertStringContainsString( 'Skipped 1 PHP block(s)', $result['output'] );
746+
$this->assertStringContainsString( 'No errors', $result['output'] );
747+
}
748+
636749
public function test_report_succeeds_without_errors(): void {
637750
$this->create_feature_file(
638751
'example.feature',

0 commit comments

Comments
 (0)