Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55fc9bf

Browse files
committed
[TypeInfo] Handle collection types properly
1 parent a2d03c5 commit 55fc9bf

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\TypeInfo\Tests\Fixtures;
4+
5+
final class DummyCollection implements \IteratorAggregate
6+
{
7+
public function getIterator(): \Traversable
8+
{
9+
return [];
10+
}
11+
}

src/Symfony/Component/TypeInfo/Tests/TypeResolver/StringTypeResolverTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\TypeInfo\Exception\UnsupportedException;
1717
use Symfony\Component\TypeInfo\Tests\Fixtures\AbstractDummy;
1818
use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy;
19+
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyCollection;
1920
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
2021
use Symfony\Component\TypeInfo\Type;
2122
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
@@ -150,6 +151,7 @@ public function resolveDataProvider(): iterable
150151
yield [Type::collection(Type::object(\IteratorAggregate::class)), \IteratorAggregate::class];
151152
yield [Type::collection(Type::object(\IteratorAggregate::class), Type::string()), \IteratorAggregate::class.'<string>'];
152153
yield [Type::collection(Type::object(\IteratorAggregate::class), Type::bool(), Type::string()), \IteratorAggregate::class.'<string, bool>'];
154+
yield [Type::collection(Type::object(DummyCollection::class), Type::bool(), Type::string()), DummyCollection::class.'<string, bool>'];
153155
}
154156

155157
public function testCannotResolveNonStringType()

src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
*/
5454
final class StringTypeResolver implements TypeResolverInterface
5555
{
56-
private const COLLECTION_CLASS_NAMES = [\Traversable::class, \Iterator::class, \IteratorAggregate::class, \ArrayAccess::class, \Generator::class];
57-
5856
/**
5957
* @var array<string, bool>
6058
*/
@@ -164,7 +162,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
164162
default => $this->resolveCustomIdentifier($node->name, $typeContext),
165163
};
166164

167-
if ($type instanceof ObjectType && \in_array($type->getClassName(), self::COLLECTION_CLASS_NAMES, true)) {
165+
if ($type instanceof ObjectType && (\is_a($type->getClassName(), \Traversable::class, true) || \is_a($type->getClassName(), \ArrayAccess::class, true))) {
168166
return Type::collection($type);
169167
}
170168

0 commit comments

Comments
 (0)