Skip to content

fix: stop class constant attributes leaking onto the next member - #2559

Open
benjaminkincses wants to merge 3 commits into
prettier:mainfrom
benjaminkincses:fix/2436-classconstant-attr-duplication
Open

benjaminkincses wants to merge 3 commits into
prettier:mainfrom
benjaminkincses:fix/2436-classconstant-attr-duplication

Conversation

@benjaminkincses

Copy link
Copy Markdown
Contributor

Fixes #2436.

An attribute on a class constant is printed again on the next class member:

#[Deprecated(message: 'use \App\Enums\MoveType')]
public const TYPES = [/* ... */];

#[Deprecated(message: 'use \App\Enums\MoveType')] // not in the source
protected $attributes = [/* ... */];

php-parser does not clear the attribute groups of a class constant, so the next member reuses the same attrgroup object. The parser wrapper now drops every attrgroup occurrence after the first one, ordered by source position. Two attribute groups written in the source are always distinct objects, so a repeated object can only come from this parser bug.

Verified with the repro from #2436 (attribute printed once, on the constant) and by formatting all 437 test fixtures before and after: only the new fixture output changes. The new fixture also covers a constant attribute leaking onto a method.

@benjaminkincses
benjaminkincses marked this pull request as ready for review November 25, 2026 19:21
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.

Class attribute gets duplicated on another class property

1 participant