Skip to content

Commit e2ef8b0

Browse files
ondrejmirtesclaude
andcommitted
Do not count a surviving trait context against entries that already declined it
When the strip phase tries a context of a deduplicated trait error against its class-file entries and the context-scoped path entries, and the context survives, the classic matching pass must not run the same occurrence against those same entries again - a count-limited directory entry would otherwise tally each surviving occurrence twice and report an inflated real count in the ignore.count error. Covered in the e2e script by a directory path over the using classes (the common way to scope ignores below a path), with and without count. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GqdqYaCEx3xAS86gnCQhPq
1 parent e16d776 commit e2ef8b0

10 files changed

Lines changed: 64 additions & 14 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ jobs:
361361
# the same report, the same ignore matching and the same generated baseline on
362362
# every run, independent of result-cache state (cold/warm/partial) and of
363363
# worker arrival order.
364-
cp AMailer.php.dist src/AMailer.php
364+
cp AMailer.php.dist src/Mailers/AMailer.php
365365
# With no ignores: the deduplicated error is reported once, directly in the
366366
# trait file (no "in context of"); errors whose message differs per using
367367
# class stay reported in each class context.
@@ -396,14 +396,24 @@ jobs:
396396
../../bin/phpstan clear-result-cache
397397
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-both-class-paths.neon"
398398
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-both-class-paths.neon"
399+
# A directory path covering the using classes (but not the trait file) accounts
400+
# per-context too: without count it hides all contexts...
401+
../../bin/phpstan clear-result-cache
402+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-dir-path.neon"
403+
# ...and with count: 1 it covers one of the two contexts: the other is reported
404+
# in its context, and the count mismatch reports the true occurrence count.
405+
../../bin/phpstan clear-result-cache
406+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-dir-count.neon")
407+
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\BMailer):17:Right side of && is always true.' "$OUTPUT"
408+
../bashunit -a contains 'is expected to occur 1 time, but occurred 2 times' "$OUTPUT"
399409
# The add-a-using-class flicker from the issue: prime the result cache with
400410
# BMailer as the trait's only user (the class-pathed entry covers the only
401411
# context), then add AMailer. The AMailer context is reported - on the
402412
# partial run and identically on every unchanged run after it.
403-
rm src/AMailer.php
413+
rm src/Mailers/AMailer.php
404414
../../bin/phpstan clear-result-cache
405415
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-class-b-path.neon"
406-
cp AMailer.php.dist src/AMailer.php
416+
cp AMailer.php.dist src/Mailers/AMailer.php
407417
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-b-path.neon")
408418
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\AMailer):17:Right side of && is always true.' "$OUTPUT"
409419
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-b-path.neon")
@@ -422,7 +432,7 @@ jobs:
422432
# context under its class path, and the combination holds on re-run.
423433
../../bin/phpstan clear-result-cache
424434
../../bin/phpstan analyse -c ignore-class-a-path.neon --generate-baseline=partial-baseline.neon
425-
../bashunit -a contains 'path: src/BMailer.php' "$(cat partial-baseline.neon)"
435+
../bashunit -a contains 'path: src/Mailers/BMailer.php' "$(cat partial-baseline.neon)"
426436
../bashunit -a not_contains 'SomeTrait.php' "$(cat partial-baseline.neon)"
427437
../../bin/phpstan clear-result-cache
428438
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-partial-baseline.neon"
@@ -431,8 +441,8 @@ jobs:
431441
# paths for the per-context errors) holds on re-run as well.
432442
../../bin/phpstan clear-result-cache
433443
../../bin/phpstan --generate-baseline=full-baseline.neon
434-
../bashunit -a contains 'path: src/AMailer.php' "$(cat full-baseline.neon)"
435-
../bashunit -a contains 'path: src/BMailer.php' "$(cat full-baseline.neon)"
444+
../bashunit -a contains 'path: src/Mailers/AMailer.php' "$(cat full-baseline.neon)"
445+
../bashunit -a contains 'path: src/Mailers/BMailer.php' "$(cat full-baseline.neon)"
436446
../bashunit -a contains 'path: src/SomeTrait.php' "$(cat full-baseline.neon)"
437447
../../bin/phpstan clear-result-cache
438448
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-full-baseline.neon"

‎e2e/bug-14932/.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
baseline.neon
22
partial-baseline.neon
33
full-baseline.neon
4-
/src/AMailer.php
4+
/src/Mailers/AMailer.php

‎e2e/bug-14932/ignore-both-class-paths.neon‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ parameters:
44
ignoreErrors:
55
-
66
identifier: booleanAnd.rightAlwaysTrue
7-
path: src/AMailer.php
7+
path: src/Mailers/AMailer.php
88
-
99
identifier: booleanAnd.rightAlwaysTrue
10-
path: src/BMailer.php
10+
path: src/Mailers/BMailer.php
1111
-
1212
identifier: identical.alwaysFalse

‎e2e/bug-14932/ignore-class-a-path.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ parameters:
44
ignoreErrors:
55
-
66
identifier: booleanAnd.rightAlwaysTrue
7-
path: src/AMailer.php
7+
path: src/Mailers/AMailer.php
88
-
99
identifier: identical.alwaysFalse

‎e2e/bug-14932/ignore-class-b-path.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ parameters:
44
ignoreErrors:
55
-
66
identifier: booleanAnd.rightAlwaysTrue
7-
path: src/BMailer.php
7+
path: src/Mailers/BMailer.php
88
-
99
identifier: identical.alwaysFalse
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
includes:
2+
- phpstan.neon
3+
parameters:
4+
ignoreErrors:
5+
-
6+
identifier: booleanAnd.rightAlwaysTrue
7+
path: src/Mailers
8+
count: 1
9+
-
10+
identifier: identical.alwaysFalse

‎e2e/bug-14932/ignore-dir-path.neon‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- phpstan.neon
3+
parameters:
4+
ignoreErrors:
5+
-
6+
identifier: booleanAnd.rightAlwaysTrue
7+
path: src/Mailers
8+
-
9+
identifier: identical.alwaysFalse

‎e2e/bug-14932/with-partial-baseline.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ parameters:
55
ignoreErrors:
66
-
77
identifier: booleanAnd.rightAlwaysTrue
8-
path: src/AMailer.php
8+
path: src/Mailers/AMailer.php
99
-
1010
identifier: identical.alwaysFalse

‎src/Analyser/Ignore/IgnoredErrorHelperResult.php‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function count;
1313
use function is_array;
1414
use function is_string;
15+
use function spl_object_id;
1516
use function sprintf;
1617

1718
/**
@@ -156,6 +157,7 @@ public function process(
156157

157158
$ignoredErrors = [];
158159
$notIgnoredErrors = [];
160+
$strippedSurvivors = [];
159161
$errorQueue = $errors;
160162
for ($errorIndex = 0; $errorIndex < count($errorQueue); $errorIndex++) {
161163
$error = $errorQueue[$errorIndex];
@@ -214,7 +216,12 @@ public function process(
214216

215217
if (count($remainingContexts) < count($traitContexts)) {
216218
foreach (array_keys($remainingContexts) as $contextFilePath) {
217-
$errorQueue[] = $error->asReportedInTraitContext($contextFilePath);
219+
$survivor = $error->asReportedInTraitContext($contextFilePath);
220+
// the strip phase above already tried this occurrence against its
221+
// file's entries and the context-scoped path entries - it must not
222+
// be counted against them a second time below
223+
$strippedSurvivors[spl_object_id($survivor)] = true;
224+
$errorQueue[] = $survivor;
218225
}
219226
continue;
220227
}
@@ -223,8 +230,10 @@ public function process(
223230
// in the trait and is matched as a whole below
224231
}
225232

233+
$isStrippedSurvivor = isset($strippedSurvivors[spl_object_id($error)]);
234+
226235
$filePath = $this->fileHelper->normalizePath($error->getFilePath());
227-
if (isset($this->ignoreErrorsByFile[$filePath])) {
236+
if (!$isStrippedSurvivor && isset($this->ignoreErrorsByFile[$filePath])) {
228237
foreach ($this->ignoreErrorsByFile[$filePath] as $ignoreError) {
229238
$i = $ignoreError['index'];
230239
$ignore = $ignoreError['ignoreError'];
@@ -256,6 +265,18 @@ public function process(
256265
$i = $ignoreError['index'];
257266
$ignore = $ignoreError['ignoreError'];
258267

268+
if (
269+
$isStrippedSurvivor
270+
&& is_array($ignore)
271+
&& isset($ignore['path'])
272+
&& $error->getTraitFilePath() !== null
273+
&& !(new FileExcluder($this->fileHelper, [$ignore['path']]))->isExcludedFromAnalysing($error->getTraitFilePath())
274+
) {
275+
// the strip phase already counted this occurrence against
276+
// context-scoped path entries
277+
continue;
278+
}
279+
259280
$result = $processIgnoreError($error, $i, $ignore);
260281
if (!$result) {
261282
$ignoredErrors[] = [$error, $ignore];

0 commit comments

Comments
 (0)