diff --git a/src/Ast/PhpDocTypeParser.php b/src/Ast/PhpDocTypeParser.php index d289b4a..668a268 100644 --- a/src/Ast/PhpDocTypeParser.php +++ b/src/Ast/PhpDocTypeParser.php @@ -13,6 +13,7 @@ use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; +use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; @@ -22,6 +23,7 @@ use PHPStan\PhpDocParser\Parser\TypeParser; use Riverwaysoft\PhpConverter\Dto\DtoClassProperty; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpListType; +use Riverwaysoft\PhpConverter\Dto\PhpType\PhpOptionalType; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpTypeFactory; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpTypeInterface; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpUnionType; @@ -144,6 +146,9 @@ private function convertToDto(TypeNode $node): PhpTypeInterface|null if ($node instanceof UnionTypeNode) { return new PhpUnionType(array_map(fn (TypeNode $child) => $this->convertToDto($child), $node->types)); } + if ($node instanceof NullableTypeNode) { + return new PhpOptionalType($this->convertToDto($node->type)); + } if ($node instanceof GenericTypeNode) { return PhpTypeFactory::create($node->type->name, array_map( fn (TypeNode $child) => $this->convertToDto($child), diff --git a/tests/Ast/PhpDocTypeParserTest.php b/tests/Ast/PhpDocTypeParserTest.php index 0a60514..0fd7bef 100644 --- a/tests/Ast/PhpDocTypeParserTest.php +++ b/tests/Ast/PhpDocTypeParserTest.php @@ -10,6 +10,7 @@ use Riverwaysoft\PhpConverter\Dto\DtoClassProperty; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpBaseType; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpListType; +use Riverwaysoft\PhpConverter\Dto\PhpType\PhpOptionalType; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpTypeInterface; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpUnionType; use Riverwaysoft\PhpConverter\Dto\PhpType\PhpUnknownType; @@ -112,6 +113,15 @@ public static function getDataVarAndReturn(): array PhpBaseType::null(), ]), ], + [ + 'nullable optional string array', + '/** @var ?string[] */', + new PhpOptionalType( + new PhpListType( + PhpBaseType::string(), + ), + ), + ], [ '2d array', '/** @var int[][] */',