Skip to content

Commit f41ce83

Browse files
committed
Fix typehint resolution - stop prefixing unions with namespace
1 parent 40df854 commit f41ce83

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/Parser/NodeVisitor.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ protected function addFunction(FunctionNode $node, ?string $namespace = null)
165165
}
166166
}
167167

168+
/**
169+
* @param \PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|\PhpParser\Node\Name|NullableType|UnionType|IntersectionType|null $type Type declaration
170+
*/
171+
protected function typeToArray($type): array
172+
{
173+
$typeArray = [];
174+
if ($type !== null && ! ($type instanceof NullableType || $type instanceof UnionType || $type instanceof IntersectionType)) {
175+
$typeAsStr = $type->__toString();
176+
if ($type instanceof FullyQualified && 0 !== strpos($typeAsStr, '\\')) {
177+
$typeAsStr = '\\' . $typeAsStr;
178+
}
179+
$typeArray[] = [$typeAsStr, false];
180+
} elseif ($type instanceof NullableType) {
181+
$typeArray = array_merge($typeArray, $this->typeToArray($type->type));
182+
$typeArray[] = ['null', false];
183+
} elseif ($type instanceof UnionType || $type instanceof IntersectionType) {
184+
foreach ($type->types as $subType) {
185+
$typeArray = array_merge($typeArray, $this->typeToArray($subType));
186+
}
187+
}
188+
189+
return $typeArray;
190+
}
191+
168192
/**
169193
* @param \PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|\PhpParser\Node\Name|NullableType|UnionType|IntersectionType|null $type Type declaration
170194
*/
@@ -439,20 +463,14 @@ protected function manageHint($type, Reflection $object): void
439463

440464
$typeArr = [];
441465
foreach ($type->types as $type) {
442-
$typeStr = $this->typeToString($type);
443-
$typeArr[] = [$typeStr, false];
466+
$typeArr = array_merge($typeArr, $this->typeToArray($type));
444467
}
445468

446469
$object->setHint($this->resolveHint($typeArr));
447470
} else {
448-
$typeStr = $this->typeToString($type);
449-
450-
if (null !== $typeStr) {
451-
$typeArr = [[$typeStr, false]];
471+
$typeArr = $this->typeToArray($type);
452472

453-
if ($type instanceof NullableType) {
454-
$typeArr[] = ['null', false];
455-
}
473+
if (!empty($typeArr)) {
456474
$object->setHint($this->resolveHint($typeArr));
457475
}
458476
}

0 commit comments

Comments
 (0)