Skip to content

Commit

Permalink
Convert union types to dynamic for Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Apr 20, 2023
1 parent 77c9236 commit 9515181
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"test:update-snapshots": "vendor/bin/phpunit tests -d --update-snapshots",
"cs:src": "vendor/bin/php-cs-fixer fix src",
"cs:tests": "vendor/bin/php-cs-fixer fix tests",
"cs": ["@cs:src", "@cs:tests"],
"phpstan": "vendor/bin/phpstan analyse src tests bin",
"php-parser-dump": "vendor/bin/php-parse tests/fixtures/NestedDto.php"
},
Expand Down
4 changes: 3 additions & 1 deletion src/Language/Dart/DartGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ private function getDartTypeFromPhp(PhpTypeInterface $type, DtoType $dto, DtoLis
{
if ($type instanceof PhpUnionType) {
Assert::greaterThan($type->getTypes(), 2, "Dart does not support union types");
Assert::true($type->isNullable(), "Dart only support nullable union types");
if (!$type->isNullable()) {
return $this->getDartTypeFromPhp(PhpBaseType::mixed(), $dto, $dtoList);
}
$notNullType = $type->getFirstNotNullType();
return sprintf('%s?', $this->getDartTypeFromPhp($notNullType, $dto, $dtoList));
}
Expand Down
1 change: 1 addition & 0 deletions tests/DartGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class User
/** @var User[] */
public array $friends;
public ColorEnum $themeColor;
public string|int $stringOrInteger;
}
CODE;

Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/DartGeneratorTest__testDart__1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class User {
final User? bestFriend;
final List<User> friends;
final ColorEnum themeColor;
final dynamic stringOrInteger;

User({
required this.id,
this.bestFriend,
required this.friends,
required this.themeColor,
required this.stringOrInteger,
});
}

0 comments on commit 9515181

Please sign in to comment.