Skip to content

Commit a5f779c

Browse files
committed
[TypeInfo] Fix create union with nullable type
1 parent 4e4fd9d commit a5f779c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,9 @@ public function testCreateNullable()
205205
new NullableType(new UnionType(new BuiltinType(TypeIdentifier::INT), new BuiltinType(TypeIdentifier::STRING))),
206206
Type::nullable(Type::union(Type::int(), Type::string(), Type::null())),
207207
);
208+
$this->assertEquals(
209+
new NullableType(new UnionType(new BuiltinType(TypeIdentifier::INT), new BuiltinType(TypeIdentifier::STRING))),
210+
Type::union(Type::nullable(Type::int()), Type::string()),
211+
);
208212
}
209213
}

src/Symfony/Component/TypeInfo/TypeFactoryTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ public static function union(Type ...$types): UnionType
266266
$isNullable = fn (Type $type): bool => $type instanceof BuiltinType && TypeIdentifier::NULL === $type->getTypeIdentifier();
267267

268268
foreach ($types as $type) {
269+
if ($type instanceof NullableType) {
270+
$nullableUnion = true;
271+
$unionTypes[] = $type->getWrappedType();
272+
273+
continue;
274+
}
275+
269276
if ($type instanceof UnionType) {
270277
foreach ($type->getTypes() as $unionType) {
271278
if ($isNullable($type)) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy