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, ...} */',