Skip to content

Commit

Permalink
[TypeInfo] Fix PHPDoc resolving of union with mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Dec 20, 2024
1 parent 73b998e commit 3b5a174
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Tests/TypeResolver/StringTypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public static function resolveDataProvider(): iterable

// union
yield [Type::union(Type::int(), Type::string()), 'int|string'];
yield [Type::mixed(), 'int|mixed'];
yield [Type::mixed(), 'mixed|int'];

// intersection
yield [Type::intersection(Type::object(\DateTime::class), Type::object(\Stringable::class)), \DateTime::class.'&'.\Stringable::class];
Expand Down
14 changes: 13 additions & 1 deletion TypeResolver/StringTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,19 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
}

if ($node instanceof UnionTypeNode) {
return Type::union(...array_map(fn (TypeNode $t): Type => $this->getTypeFromNode($t, $typeContext), $node->types));
$types = [];

foreach ($node->types as $nodeType) {
$type = $this->getTypeFromNode($nodeType, $typeContext);

if ($type instanceof BuiltinType && TypeIdentifier::MIXED === $type->getTypeIdentifier()) {
return Type::mixed();
}

$types[] = $type;
}

return Type::union(...$types);
}

if ($node instanceof IntersectionTypeNode) {
Expand Down

0 comments on commit 3b5a174

Please sign in to comment.