Skip to content

Commit e16d776

Browse files
ondrejmirtesclaude
andcommitted
Account for deduplicated trait errors per using-class context
An error deduplicated directly into the trait stands for one occurrence per using class. Until now it invisibly carried the file path of a single using class - which one depended on the iteration order of collected data (worker arrival order on a cold run, ksorted result-cache replay on a warm run, append order on a partial run), so ignoreErrors matching and --generate-baseline flickered between runs with no code changes. The deduplicated Error now keeps all of its using-class contexts and is attributed to the trait file itself: - an ignoreErrors path pointing at the trait file hides the error for all using classes (count: 1, as before); - a path pointing at one using class accounts only for that class's context - the remaining contexts stay reported, each "in context of" its class, so a baseline generated by PHPStan <= 2.1 (one entry per class) keeps working with no unmatched entries, while a single class-pathed entry no longer hides the sibling contexts; - --generate-baseline records the trait path for a fully surviving error and the class path for a surviving context, deterministically. Every consumer of the collected-data order is now order-independent, so the winner-picking nondeterminism has no observable effect left. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GqdqYaCEx3xAS86gnCQhPq
1 parent e6357e5 commit e16d776

21 files changed

Lines changed: 432 additions & 19 deletions

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

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,105 @@ jobs:
338338
../../bin/phpstan clear-result-cache
339339
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan --error-format=raw")
340340
../bashunit -a contains 'FooTrait.php:10:Strict comparison using === between int<0, max> and false will always evaluate to false.' "$OUTPUT"
341-
# An ignoreErrors path may target the error by the trait file or by the using-class
342-
# file - both keep working (no BC break).
341+
# An ignoreErrors path may target the error by the trait file (hides it for
342+
# all using classes) or by a using-class file - a class-pathed entry accounts
343+
# only for its own class's context, so the other using class's context is
344+
# still reported (see also the e2e/bug-14932 script).
343345
../../bin/phpstan clear-result-cache
344346
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-trait-path.neon"
345347
../../bin/phpstan clear-result-cache
346-
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-class-path.neon"
348+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-path.neon")
349+
../bashunit -a contains 'FooTrait.php (in context of class Bug14718\Bar):10:Strict comparison using === between int<0, max> and false will always evaluate to false.' "$OUTPUT"
350+
../bashunit -a not_contains 'was not matched' "$OUTPUT"
347351
# A generated baseline suppresses the error on re-run, with both an empty and a
348352
# primed result cache (the issue reported baseline generation as impossible).
349353
../../bin/phpstan --generate-baseline=baseline.neon
350354
../../bin/phpstan clear-result-cache
351355
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon"
352356
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon"
357+
- script: |
358+
cd e2e/bug-14932
359+
# https://github.com/phpstan/phpstan/issues/14932
360+
# An error deduplicated directly into the trait must behave deterministically:
361+
# the same report, the same ignore matching and the same generated baseline on
362+
# every run, independent of result-cache state (cold/warm/partial) and of
363+
# worker arrival order.
364+
cp AMailer.php.dist src/AMailer.php
365+
# With no ignores: the deduplicated error is reported once, directly in the
366+
# trait file (no "in context of"); errors whose message differs per using
367+
# class stay reported in each class context.
368+
../../bin/phpstan clear-result-cache
369+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw")
370+
../bashunit -a contains 'SomeTrait.php:17:Right side of && is always true.' "$OUTPUT"
371+
../bashunit -a not_contains 'SomeTrait.php (in context of' "$OUTPUT"
372+
../bashunit -a contains 'DiffMessageTrait.php (in context of class Bug14932\AMailer):10:Strict comparison' "$OUTPUT"
373+
../bashunit -a contains 'DiffMessageTrait.php (in context of class Bug14932\BMailer):10:Strict comparison' "$OUTPUT"
374+
# An ignoreErrors path targeting the trait file hides the error from all
375+
# using classes - with both an empty and a primed result cache.
376+
../../bin/phpstan clear-result-cache
377+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-trait-path.neon"
378+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-trait-path.neon"
379+
# An ignoreErrors path targeting one using class accounts only for that
380+
# class's context: the entry matches (no ignore.unmatched), and the error is
381+
# still reported in the context of the other class - deterministically, with
382+
# both an empty and a primed result cache.
383+
../../bin/phpstan clear-result-cache
384+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-a-path.neon")
385+
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\BMailer):17:Right side of && is always true.' "$OUTPUT"
386+
../bashunit -a not_contains 'was not matched' "$OUTPUT"
387+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-a-path.neon")
388+
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\BMailer):17:Right side of && is always true.' "$OUTPUT"
389+
../../bin/phpstan clear-result-cache
390+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-b-path.neon")
391+
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\AMailer):17:Right side of && is always true.' "$OUTPUT"
392+
../bashunit -a not_contains 'was not matched' "$OUTPUT"
393+
# Entries covering every using class hide the error completely - this is what
394+
# a baseline generated by PHPStan <= 2.1 recorded (one entry per class
395+
# context), so such baselines keep working with no unmatched entries.
396+
../../bin/phpstan clear-result-cache
397+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-both-class-paths.neon"
398+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-both-class-paths.neon"
399+
# The add-a-using-class flicker from the issue: prime the result cache with
400+
# BMailer as the trait's only user (the class-pathed entry covers the only
401+
# context), then add AMailer. The AMailer context is reported - on the
402+
# partial run and identically on every unchanged run after it.
403+
rm src/AMailer.php
404+
../../bin/phpstan clear-result-cache
405+
../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
407+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-b-path.neon")
408+
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\AMailer):17:Right side of && is always true.' "$OUTPUT"
409+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw -c ignore-class-b-path.neon")
410+
../bashunit -a contains 'SomeTrait.php (in context of class Bug14932\AMailer):17:Right side of && is always true.' "$OUTPUT"
411+
# --generate-baseline attributes the fully deduplicated error to the trait
412+
# file, and the baseline holds on re-run with an empty and a primed cache.
413+
../../bin/phpstan clear-result-cache
414+
../../bin/phpstan analyse -c generate-only.neon --generate-baseline=baseline.neon
415+
../bashunit -a contains 'path: src/SomeTrait.php' "$(cat baseline.neon)"
416+
../bashunit -a not_contains 'AMailer.php' "$(cat baseline.neon)"
417+
../bashunit -a not_contains 'BMailer.php' "$(cat baseline.neon)"
418+
../../bin/phpstan clear-result-cache
419+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon"
420+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon"
421+
# Generating a baseline on top of a one-class ignore records the surviving
422+
# context under its class path, and the combination holds on re-run.
423+
../../bin/phpstan clear-result-cache
424+
../../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)"
426+
../bashunit -a not_contains 'SomeTrait.php' "$(cat partial-baseline.neon)"
427+
../../bin/phpstan clear-result-cache
428+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-partial-baseline.neon"
429+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-partial-baseline.neon"
430+
# A full generated baseline (trait path for the deduplicated error, class
431+
# paths for the per-context errors) holds on re-run as well.
432+
../../bin/phpstan clear-result-cache
433+
../../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)"
436+
../bashunit -a contains 'path: src/SomeTrait.php' "$(cat full-baseline.neon)"
437+
../../bin/phpstan clear-result-cache
438+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-full-baseline.neon"
439+
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-full-baseline.neon"
353440
- script: |
354441
cd e2e/result-cache-meta-extension
355442
composer install

‎build/baseline-8.0.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ parameters:
2222
message: '#^Strict comparison using \=\=\= between list\<non\-falsy\-string\> and false will always evaluate to false\.$#'
2323
identifier: identical.alwaysFalse
2424
count: 1
25-
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php
25+
path: ../src/Type/Php/MbFunctionsReturnTypeExtensionTrait.php

‎e2e/bug-14932/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
baseline.neon
2+
partial-baseline.neon
3+
full-baseline.neon
4+
/src/AMailer.php

‎e2e/bug-14932/AMailer.php.dist‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14932;
4+
5+
final class AMailer
6+
{
7+
8+
public const NAME = 'a';
9+
10+
use SomeTrait;
11+
use DiffMessageTrait;
12+
13+
}

‎e2e/bug-14932/generate-only.neon‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
includes:
2+
- phpstan.neon
3+
parameters:
4+
ignoreErrors:
5+
-
6+
identifier: identical.alwaysFalse
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
includes:
2+
- phpstan.neon
3+
parameters:
4+
ignoreErrors:
5+
-
6+
identifier: booleanAnd.rightAlwaysTrue
7+
path: src/AMailer.php
8+
-
9+
identifier: booleanAnd.rightAlwaysTrue
10+
path: src/BMailer.php
11+
-
12+
identifier: identical.alwaysFalse
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/AMailer.php
8+
-
9+
identifier: identical.alwaysFalse
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/BMailer.php
8+
-
9+
identifier: identical.alwaysFalse
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/SomeTrait.php
8+
-
9+
identifier: identical.alwaysFalse

‎e2e/bug-14932/phpstan.neon‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: 4
3+
paths:
4+
- src
5+
parallel:
6+
maximumNumberOfProcesses: 1

0 commit comments

Comments
 (0)