diff --git a/src/TypeResolver.php b/src/TypeResolver.php index e36c5885..0f94a2f2 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -391,7 +391,9 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ { switch (strtolower($type->type->name)) { case 'array': - return $this->createArray($type->genericTypes, $context); + $genericTypes = array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context)); + + return new Array_(...$genericTypes); case 'non-empty-array': $genericTypes = array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context)); @@ -630,33 +632,6 @@ private function resolveTypedObject(string $type, ?Context $context = null): Obj return new Object_($this->fqsenResolver->resolve($type, $context)); } - /** @param TypeNode[] $typeNodes */ - private function createArray(array $typeNodes, Context $context): Array_ - { - $types = array_reverse($this->createTypesByTypeNodes($typeNodes, $context)); - - if (isset($types[1]) === false) { - return new Array_(...$types); - } - - if ($this->validArrayKeyType($types[1]) || $types[1] instanceof ArrayKey) { - 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); - } - } - - throw new RuntimeException('An array can have only integers or strings as keys'); - } - - private function validArrayKeyType(?Type $type): bool - { - return $type instanceof String_ || $type instanceof Integer; - } - private function parse(TokenIterator $tokenIterator): TypeNode { try { diff --git a/tests/unit/CollectionResolverTest.php b/tests/unit/CollectionResolverTest.php index 051d077f..11807dbb 100644 --- a/tests/unit/CollectionResolverTest.php +++ b/tests/unit/CollectionResolverTest.php @@ -194,19 +194,6 @@ public function testResolvingCollectionOfCollection(): void $this->assertEquals(new Object_(new Fqsen('\\DateTime')), $nestedGenericTypes[0]); } - /** - * @covers ::__construct - * @covers ::resolve - * @covers ::createType - */ - public function testBadArrayCollectionKey(): void - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('An array can have only integers or strings as keys'); - $fixture = new TypeResolver(); - $fixture->resolve('array', new Context('')); - } - /** * @covers ::__construct * @covers ::resolve diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index 0248f5e3..ded731e3 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -1073,6 +1073,43 @@ public function genericsProvider(): array new Integer() ), ], + [ + 'array, string>', + new Array_( + new String_(), + new KeyOf(new ConstExpression( + new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), + 'SOME_CONSTANT' + )) + ), + ], + [ + 'array, string>', + new Array_( + new String_(), + new ValueOf(new ConstExpression( + new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), + 'SOME_CONSTANT' + )) + ), + ], + [ + 'array', + new Array_( + new String_(), + new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar')), '*') + ), + ], + [ + 'array', + new Array_( + new String_(), + new Compound([ + new ConstExpression(new Self_(), 'SOME_CONSTANT_FIRST'), + new ConstExpression(new Self_(), 'SOME_CONSTANT_SECOND'), + ]) + ), + ], [ 'array', new Array_(