diff --git a/src/Type/ExplicitType.php b/src/Type/ExplicitType.php index a067fee..397282f 100644 --- a/src/Type/ExplicitType.php +++ b/src/Type/ExplicitType.php @@ -3,6 +3,7 @@ namespace Radebatz\TypeInfoExtras\Type; use Symfony\Component\TypeInfo\Type; +use Symfony\Component\TypeInfo\Type\BuiltinType; use Symfony\Component\TypeInfo\TypeIdentifier; /** @@ -31,6 +32,14 @@ public function getTypeIdentifier(): TypeIdentifier return $this->typeIdentifier; } + /** + * Get the underlying builtin type. + */ + public function getBuiltinType(): BuiltinType + { + return Type::builtin($this->typeIdentifier); + } + public function getExplicitType(): string { return $this->explicitType; diff --git a/src/TypeResolver/ResolverExtrasTrait.php b/src/TypeResolver/ResolverExtrasTrait.php index 9354e8b..3467774 100644 --- a/src/TypeResolver/ResolverExtrasTrait.php +++ b/src/TypeResolver/ResolverExtrasTrait.php @@ -52,4 +52,11 @@ protected function tryAsClassLike(BaseType $type, array $variableTypes): ?ClassL return null; } + + public function reduceToBuiltinType(?BaseType $type) + { + return $type instanceof ExplicitType + ? $type->getBuiltinType() + : $type; + } } diff --git a/src/TypeResolver/StringTypeResolver.php b/src/TypeResolver/StringTypeResolver.php index 064a628..d159f64 100644 --- a/src/TypeResolver/StringTypeResolver.php +++ b/src/TypeResolver/StringTypeResolver.php @@ -286,7 +286,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Bas throw new \DomainException(\sprintf('"%s" type cannot have a key type defined.', $node->type)); } - return Type::collection($type, $variableTypes[1], $variableTypes[0], $asList); + return Type::collection($type, $variableTypes[1], $this->reduceToBuiltinType($variableTypes[0]), $asList); } } diff --git a/tests/TypeResolver/StringTypeResolverTest.php b/tests/TypeResolver/StringTypeResolverTest.php index 1e17b04..f594546 100644 --- a/tests/TypeResolver/StringTypeResolverTest.php +++ b/tests/TypeResolver/StringTypeResolverTest.php @@ -60,6 +60,7 @@ public static function extrasResolveDataProvider(): iterable yield [Type::union(Type::intRange(to: -1), Type::intRange(from: 1)), 'non-zero-int']; yield [Type::intRange(0, 100), 'int<0, 100>']; yield [Type::intRange(), 'int']; + yield [Type::array(Type::bool(), Type::int()), 'array']; yield [Type::explicit(TypeIdentifier::STRING, 'class-string'), 'class-string']; yield [Type::classLike('class-string', Type::object(Dummy::class)), sprintf('class-string<%s>', Dummy::class)];