Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support union type in Type::nullable() #141

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ Each type or union/intersection type can be passed as a string, you can also use
use Nette\PhpGenerator\Type;

$member->setType('array'); // or Type::Array;
$member->setType('array|string'); // or Type::union('array', 'string')
$member->setType('?array'); // or Type::nullable(Type::Array);
$member->setType('array|string'); // or Type::union(Type::Array, Type::String)
$member->setType('array|string|null'); // or Type::nullable(Type::union(Type::Array, Type::String))
$member->setType('Foo&Bar'); // or Type::intersection(Foo::class, Bar::class)
$member->setType(null); // removes type
```
Expand Down
20 changes: 19 additions & 1 deletion src/PhpGenerator/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,25 @@ class Type

public static function nullable(string $type, bool $nullable = true): string
{
return ($nullable ? '?' : '') . ltrim($type, '?');
if (! str_contains($type, '|')) {
return ($nullable ? '?' : '') . ltrim($type, '?');
}

// Union type
$types = explode('|', $type);
if (in_array('null', $types)) {
if ($nullable) {
return $type;
}
$types = array_diff($types, ['null']);
} else {
if (!$nullable) {
return $type;
}
$types[] = 'null';
}

return implode('|', $types);
}


Expand Down
27 changes: 21 additions & 6 deletions tests/PhpGenerator/Type.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@ use Nette\PhpGenerator\Type;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';

// Nullable
Assert::same('?int', Type::nullable(Type::Int));
Assert::same('int', Type::nullable(Type::Int, nullable: false));

Assert::same('A|string', Type::union(A::class, Type::String));
Assert::same('?int', Type::nullable('?int'));
Assert::same('int', Type::nullable('?int', nullable: false));

Assert::same('int|float|string|null', Type::nullable('int|float|string'));
Assert::same('int|float|string', Type::nullable('int|float|string', nullable: false));

Assert::same('null|int|float|string', Type::nullable('null|int|float|string'));
Assert::same('int|float|string', Type::nullable('null|int|float|string', nullable: false));

Assert::same('?A', Type::nullable(A::class));
Assert::same('?A', Type::nullable(A::class));
Assert::same('A', Type::nullable(A::class, nullable: false));
Assert::same('int|float|string|null', Type::nullable('int|float|string|null'));
Assert::same('int|float|string', Type::nullable('int|float|string|null', nullable: false));

Assert::same('int|float|null|string', Type::nullable('int|float|null|string'));
Assert::same('int|float|string', Type::nullable('int|float|null|string', nullable: false));

// Union
Assert::same('A|string', Type::union(A::class, Type::String));

Assert::same('?A', Type::nullable('?A'));
Assert::same('A', Type::nullable('?A', nullable: false));
// Intersection
Assert::same('A&string', Type::intersection(A::class, Type::String));