Skip to content

Commit 133ce8a

Browse files
committed
bug symfony#59271 [TypeInfo] Fix PHPDoc resolving of union with mixed (mtarld)
This PR was merged into the 7.2 branch. Discussion ---------- [TypeInfo] Fix PHPDoc resolving of union with mixed | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#59258, Fix symfony#59223 | License | MIT Resolve "invalid" union PHPDoc with `mixed` standalone type as `mixed` type. Commits ------- cc8670d [TypeInfo] Fix PHPDoc resolving of union with mixed
2 parents 909cb59 + cc8670d commit 133ce8a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public static function resolveDataProvider(): iterable
155155

156156
// union
157157
yield [Type::union(Type::int(), Type::string()), 'int|string'];
158+
yield [Type::mixed(), 'int|mixed'];
159+
yield [Type::mixed(), 'mixed|int'];
158160

159161
// intersection
160162
yield [Type::intersection(Type::object(\DateTime::class), Type::object(\Stringable::class)), \DateTime::class.'&'.\Stringable::class];

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,19 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
223223
}
224224

225225
if ($node instanceof UnionTypeNode) {
226-
return Type::union(...array_map(fn (TypeNode $t): Type => $this->getTypeFromNode($t, $typeContext), $node->types));
226+
$types = [];
227+
228+
foreach ($node->types as $nodeType) {
229+
$type = $this->getTypeFromNode($nodeType, $typeContext);
230+
231+
if ($type instanceof BuiltinType && TypeIdentifier::MIXED === $type->getTypeIdentifier()) {
232+
return Type::mixed();
233+
}
234+
235+
$types[] = $type;
236+
}
237+
238+
return Type::union(...$types);
227239
}
228240

229241
if ($node instanceof IntersectionTypeNode) {

0 commit comments

Comments
 (0)