Skip to content

Commit

Permalink
Support for the nullable list with question mark
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Apr 29, 2024
1 parent 1f359f6 commit 5f050dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Ast/PhpDocTypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions tests/Ast/PhpDocTypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[][] */',
Expand Down

0 comments on commit 5f050dc

Please sign in to comment.