Skip to content

Commit 95e8168

Browse files
committed
feature #54661 [TypeInfo] Handle custom collection objects properly (mtarld)
This PR was merged into the 7.1 branch. Discussion ---------- [TypeInfo] Handle custom collection objects properly | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | | License | MIT Understand custom collection objects. Commits ------- b2a7627 [TypeInfo] Handle collection types properly
2 parents 3d81565 + b2a7627 commit 95e8168

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed
Lines changed: 11 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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;
@@ -167,6 +168,7 @@ public function resolveDataProvider(): iterable
167168
yield [Type::collection(Type::object(\IteratorAggregate::class)), \IteratorAggregate::class];
168169
yield [Type::collection(Type::object(\IteratorAggregate::class), Type::string()), \IteratorAggregate::class.'<string>'];
169170
yield [Type::collection(Type::object(\IteratorAggregate::class), Type::bool(), Type::string()), \IteratorAggregate::class.'<string, bool>'];
171+
yield [Type::collection(Type::object(DummyCollection::class), Type::bool(), Type::string()), DummyCollection::class.'<string, bool>'];
170172
}
171173

172174
public function testCannotResolveNonStringType()

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

Lines changed: 2 additions & 4 deletions
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
*/
@@ -166,7 +164,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
166164
default => $this->resolveCustomIdentifier($node->name, $typeContext),
167165
};
168166

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

@@ -203,7 +201,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
203201
}
204202
}
205203

206-
if ($type instanceof ObjectType && \in_array($type->getClassName(), self::COLLECTION_CLASS_NAMES, true)) {
204+
if ($type instanceof ObjectType && (is_a($type->getClassName(), \Traversable::class, true) || is_a($type->getClassName(), \ArrayAccess::class, true))) {
207205
return match (\count($variableTypes)) {
208206
1 => Type::collection($type, $variableTypes[0]),
209207
2 => Type::collection($type, $variableTypes[1], $variableTypes[0]),

0 commit comments

Comments
 (0)