Skip to content

Let a trait-pathed ignore entry take precedence over the class-pathed ones - #6411

Open
mlocati wants to merge 1 commit into
phpstan:2.2.xfrom
mlocati-forks:trait-context-baseline-own-error
Open

mlocati wants to merge 1 commit into
phpstan:2.2.xfrom
mlocati-forks:trait-context-baseline-own-error

Conversation

@mlocati

@mlocati mlocati commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

When a class using a trait reports its own error with the same message and identifier as an error deduplicated directly into the trait, --generate-baseline records the class occurrence under the class path (count: 1) and the deduplicated error under the trait path (count: 1)
On re-run the class-pathed entry also absorbed the trait context of that class, so it was reported as expected 1 time but occurred 2 times: the generated baseline failed by itself.

An entry scoped to the trait file hides the deduplicated error for every using class, so it is now tried first: the class-pathed entries account only for the errors reported in the class files themselves.

… ones

When a class using a trait reports its own error with the same message and
identifier as an error deduplicated directly into the trait,
--generate-baseline records the class occurrence under the class path
(count: 1) and the deduplicated error under the trait path (count: 1).
On re-run the class-pathed entry also absorbed the trait context of that
class, so it was reported as expected 1 time but occurred 2 times: the
generated baseline failed by itself.

An entry scoped to the trait file hides the deduplicated error for every
using class, so it is now tried first: the class-pathed entries account only
for the errors reported in the class files themselves.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@mlocati
mlocati force-pushed the trait-context-baseline-own-error branch from a53c4e2 to e11628c Compare September 23, 2026 15:38
@mlocati

mlocati commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

The problem this PR fixes, in short: a freshly generated baseline fails on the very next run.

With the provided test files, AMailer and BMailer use SomeTrait, and both AMailer and SomeTrait contain the same error. phpstan analyse reports 2 errors:

src/AMailer.php:19: Right side of && is always true.
src/SomeTrait.php:17: Right side of && is always true.

(the trait error is reported once, although the trait is used by two classes).

phpstan analyse --generate-baseline=baseline.neon accordingly writes 2 entries, both with count: 1: one for src/AMailer.php, one for src/SomeTrait.php.

Then phpstan analyse -c with-baseline.neon fails without this patch:

src/AMailer.php:19: Ignored error pattern ... in path src/AMailer.php is expected to occur 1 time, but occurred 2 times.

because the trait error, when checked against the ignore entries, is also matched as an error of AMailer (the class using the trait), so the AMailer.php entry gets charged 2 errors instead of 1.

With this patch, the SomeTrait.php entry is checked first, so the trait error is matched by it, the AMailer.php entry only matches its own error, and the run passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant