From e8a6807e1c146f424461f915b202776d662f7e16 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Tue, 1 Dec 2026 11:08:44 +0000 Subject: [PATCH] Reprint the key type of an array shape's unsealed type before its value type The format-preserving printer reprints the subnodes of a node in the order of its properties, ArrayShapeUnsealedTypeNode declared the value type first. An unchanged ... was printed as .... Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301 --- src/Ast/Type/ArrayShapeUnsealedTypeNode.php | 5 +- tests/PHPStan/Printer/PrinterTest.php | 52 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/Ast/Type/ArrayShapeUnsealedTypeNode.php b/src/Ast/Type/ArrayShapeUnsealedTypeNode.php index b6c36bb7..aa55f897 100644 --- a/src/Ast/Type/ArrayShapeUnsealedTypeNode.php +++ b/src/Ast/Type/ArrayShapeUnsealedTypeNode.php @@ -11,10 +11,11 @@ class ArrayShapeUnsealedTypeNode implements Node use NodeAttributes; - public TypeNode $valueType; - + // declared in the order of the source, the format-preserving printer reprints the subnodes in this order public ?TypeNode $keyType = null; + public TypeNode $valueType; + public function __construct(TypeNode $valueType, ?TypeNode $keyType) { $this->valueType = $valueType; diff --git a/tests/PHPStan/Printer/PrinterTest.php b/tests/PHPStan/Printer/PrinterTest.php index 242965d2..d5f66302 100644 --- a/tests/PHPStan/Printer/PrinterTest.php +++ b/tests/PHPStan/Printer/PrinterTest.php @@ -2467,6 +2467,58 @@ public function enterNode(Node $node) }, ]; + yield [ + '/** @return array{foo: int, ...} */', + '/** @return array{foo: int, ...} */', + new class extends AbstractNodeVisitor { + + }, + ]; + + yield [ + '/** @return array{...} */', + '/** @return array{...} */', + new class extends AbstractNodeVisitor { + + }, + ]; + + yield [ + '/** @return array{foo: int, ...} */', + '/** @return array{foo: int, ...} */', + new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof ArrayShapeNode) { + assert($node->unsealedType !== null); + $node->unsealedType->valueType = new IdentifierTypeNode('bool'); + } + + return $node; + } + + }, + ]; + + yield [ + '/** @return array{foo: int, ...} */', + '/** @return array{foo: int, ...} */', + new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof ArrayShapeNode) { + assert($node->unsealedType !== null); + $node->unsealedType->keyType = new IdentifierTypeNode('int'); + } + + return $node; + } + + }, + ]; + yield [ '/** @return array{foo: int, ...} */', '/** @return array{foo: int, ...} */',