diff --git a/src/phpDocumentor/Reflection/Php/Factory/ClassConstantIterator.php b/src/phpDocumentor/Reflection/Php/Factory/ClassConstantIterator.php index 4273a838..77051d7b 100644 --- a/src/phpDocumentor/Reflection/Php/Factory/ClassConstantIterator.php +++ b/src/phpDocumentor/Reflection/Php/Factory/ClassConstantIterator.php @@ -22,6 +22,8 @@ /** * This class acts like a combination of a ClassConst and Const_ * to be able to create constant descriptors using a normal strategy. + * + * @implements Iterator */ final class ClassConstantIterator implements Iterator { diff --git a/src/phpDocumentor/Reflection/Php/Factory/GlobalConstantIterator.php b/src/phpDocumentor/Reflection/Php/Factory/GlobalConstantIterator.php index 33910729..4f0d59a9 100644 --- a/src/phpDocumentor/Reflection/Php/Factory/GlobalConstantIterator.php +++ b/src/phpDocumentor/Reflection/Php/Factory/GlobalConstantIterator.php @@ -19,6 +19,9 @@ use PhpParser\Node\Expr; use PhpParser\Node\Stmt\Const_; +/** + * @implements Iterator + */ final class GlobalConstantIterator implements Iterator { private Const_ $constant; diff --git a/src/phpDocumentor/Reflection/Php/Factory/PropertyIterator.php b/src/phpDocumentor/Reflection/Php/Factory/PropertyIterator.php index 83721c3f..6447c29d 100644 --- a/src/phpDocumentor/Reflection/Php/Factory/PropertyIterator.php +++ b/src/phpDocumentor/Reflection/Php/Factory/PropertyIterator.php @@ -25,6 +25,8 @@ /** * This class acts like a combination of a PropertyNode and PropertyProperty to * be able to create property descriptors using a normal strategy. + * + * @implements Iterator */ final class PropertyIterator implements Iterator { diff --git a/src/phpDocumentor/Reflection/Php/Factory/Type.php b/src/phpDocumentor/Reflection/Php/Factory/Type.php index ad128e7e..3cedf2e1 100644 --- a/src/phpDocumentor/Reflection/Php/Factory/Type.php +++ b/src/phpDocumentor/Reflection/Php/Factory/Type.php @@ -23,9 +23,12 @@ use PhpParser\Node\Name; use PhpParser\Node\NullableType; use PhpParser\Node\UnionType; +use PhpParser\NodeAbstract; +use function array_map; use function get_class; use function implode; +use function is_string; use function sprintf; final class Type @@ -39,23 +42,49 @@ public function fromPhpParser($type, ?Context $context = null): ?TypeElement return null; } - $typeResolver = new TypeResolver(); + return (new TypeResolver()) + ->resolve($this->convertPhpParserTypeToString($type), $context); + } + + /** + * @param NodeAbstract|string $type + */ + private function convertPhpParserTypeToString($type): string + { + if (is_string($type)) { + return $type; + } + + if ($type instanceof Identifier) { + return $type->toString(); + } + + if ($type instanceof Name) { + return $type->toString(); + } + if ($type instanceof NullableType) { - return $typeResolver->resolve('?' . $type->type, $context); + return '?' . $this->convertPhpParserTypeToString($type->type); } if ($type instanceof UnionType) { - return $typeResolver->resolve(implode('|', $type->types), $context); + $typesAsStrings = array_map( + fn ($typeObject): string => $this->convertPhpParserTypeToString($typeObject), + $type->types + ); + + return implode('|', $typesAsStrings); } if ($type instanceof IntersectionType) { - return $typeResolver->resolve(implode('&', $type->types), $context); - } + $typesAsStrings = array_map( + fn ($typeObject): string => $this->convertPhpParserTypeToString($typeObject), + $type->types + ); - if ($type instanceof ComplexType) { - throw new InvalidArgumentException(sprintf('Unsupported complex type %s', get_class($type))); + return implode('&', $typesAsStrings); } - return $typeResolver->resolve($type->toString(), $context); + throw new InvalidArgumentException(sprintf('Unsupported complex type %s', get_class($type))); } } diff --git a/tests/unit/phpDocumentor/Reflection/Php/Factory/TypeTest.php b/tests/unit/phpDocumentor/Reflection/Php/Factory/TypeTest.php index 3699f17a..5fecfb82 100644 --- a/tests/unit/phpDocumentor/Reflection/Php/Factory/TypeTest.php +++ b/tests/unit/phpDocumentor/Reflection/Php/Factory/TypeTest.php @@ -78,7 +78,7 @@ public function testReturnsNullableTypeWhenPassedAPhpParserNullable(): void public function testReturnsUnion(): void { $factory = new Type(); - $given = new UnionType(['integer', 'string']); + $given = new UnionType([new Identifier('integer'), new Identifier('string')]); $expected = new Compound([new Integer(), new String_()]); $result = $factory->fromPhpParser($given);