diff --git a/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyCollection.php b/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyCollection.php new file mode 100644 index 0000000000000..a12c76b3c6f73 --- /dev/null +++ b/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyCollection.php @@ -0,0 +1,11 @@ +']; yield [Type::collection(Type::object(\IteratorAggregate::class), Type::bool(), Type::string()), \IteratorAggregate::class.'']; + yield [Type::collection(Type::object(DummyCollection::class), Type::bool(), Type::string()), DummyCollection::class.'']; } public function testCannotResolveNonStringType() diff --git a/src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php b/src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php index 40b4dd231286f..e1122925c7d98 100644 --- a/src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php +++ b/src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php @@ -53,8 +53,6 @@ */ final class StringTypeResolver implements TypeResolverInterface { - private const COLLECTION_CLASS_NAMES = [\Traversable::class, \Iterator::class, \IteratorAggregate::class, \ArrayAccess::class, \Generator::class]; - /** * @var array */ @@ -164,7 +162,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ default => $this->resolveCustomIdentifier($node->name, $typeContext), }; - if ($type instanceof ObjectType && \in_array($type->getClassName(), self::COLLECTION_CLASS_NAMES, true)) { + if ($type instanceof ObjectType && (is_a($type->getClassName(), \Traversable::class, true) || is_a($type->getClassName(), \ArrayAccess::class, true))) { return Type::collection($type); } @@ -201,7 +199,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ } } - if ($type instanceof ObjectType && \in_array($type->getClassName(), self::COLLECTION_CLASS_NAMES, true)) { + if ($type instanceof ObjectType && (is_a($type->getClassName(), \Traversable::class, true) || is_a($type->getClassName(), \ArrayAccess::class, true))) { return match (\count($variableTypes)) { 1 => Type::collection($type, $variableTypes[0]), 2 => Type::collection($type, $variableTypes[1], $variableTypes[0]),