From 671c2895a3ac6fabbf7a62d13eb1961056afc838 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Sun, 7 Dec 2025 02:36:57 +0100 Subject: [PATCH] Fix literal array key types false positive error Fix https://github.com/phpDocumentor/TypeResolver/issues/264 --- src/TypeResolver.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/TypeResolver.php b/src/TypeResolver.php index de20a1dd..63524aa7 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -643,10 +643,16 @@ private function createArray(array $typeNodes, Context $context): Array_ return new Array_(...$types); } - if ($types[1] instanceof Compound && $types[1]->getIterator()->count() === 2) { - if ($this->validArrayKeyType($types[1]->get(0)) && $this->validArrayKeyType($types[1]->get(1))) { - return new Array_(...$types); - } + if ($types[1] instanceof Compound) { + for ($i = 0; $i < $types[1]->getIterator()->count(); $i++) { + if ($this->validArrayKeyType($types[1]->get($i))) { + continue; + } + + throw new RuntimeException('An array can have only integers or strings as keys'); + } + + return new Array_(...$types); } throw new RuntimeException('An array can have only integers or strings as keys'); @@ -654,7 +660,25 @@ private function createArray(array $typeNodes, Context $context): Array_ private function validArrayKeyType(?Type $type): bool { - return $type instanceof String_ || $type instanceof Integer; + return $type instanceof String_ || + $type instanceof CallableString || + $type instanceof HtmlEscapedString || + $type instanceof IntegerRange || + $type instanceof IntegerValue || + $type instanceof IntMask || + $type instanceof IntMaskOf || + $type instanceof KeyOf || + $type instanceof LiteralString || + $type instanceof LowercaseString || + $type instanceof NegativeInteger || + $type instanceof NonEmptyLowercaseString || + $type instanceof NonEmptyString || + $type instanceof PositiveInteger || + $type instanceof StringValue || + $type instanceof ClassString || + $type instanceof InterfaceString || + $type instanceof TraitString || + $type instanceof Integer; } private function parse(TokenIterator $tokenIterator): TypeNode