Skip to content

Printer: keep the format of an unsealed array shape type with a key - #354

Open
odolbeau wants to merge 1 commit into
phpstan:2.5.xfrom
odolbeau:fix/unsealed-type-format-preserving
Open

odolbeau wants to merge 1 commit into
phpstan:2.5.xfrom
odolbeau:fix/unsealed-type-format-preserving

Conversation

@odolbeau

Copy link
Copy Markdown

With format-preserving printing, changing any type inside an array shape that has a keyed unsealed type corrupts the PHPDoc.

Reproduction: parse the following, rename Foo to Bar, then call printFormatPreserving().

/** @return array{foo: Foo, ...<string, mixed>} */

Expected:

/** @return array{foo: Bar, ...<string, mixed>} */

Actual:

/** @return array{foo: Bar, ...<string, mixedstring, mixed>} */

The result can no longer be parsed. I found this through Rector, when importing class names.

Cause: printNodeFormatPreserving() walks sub-nodes in property declaration order and expects it to match source order. ArrayShapeUnsealedTypeNode declared $valueType before $keyType, while the key comes first in the source.

Fix: declare $keyType first. The constructor signature is unchanged. As a side effect, NodeTraverser now visits the key before the value, which also matches source order.

Tests: two format-preserving cases, one changing a sibling item and one changing the key type.

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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