Skip to content

Commit 33f70b7

Browse files
bug #59818 [TypeInfo] Fix create union with nullable type (mtarld)
This PR was merged into the 7.2 branch. Discussion ---------- [TypeInfo] Fix create union with nullable type | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Fix the creation of a union type with a nullable type, such as ```php Type::union(Type::nullable(Type::int()), Type::string()); // it will be handled the same as: Type::nullable(Type::int(), Type::string()) Type::union(Type::int(), Type::string(), Type::null()) ``` Commits ------- a5f779c [TypeInfo] Fix create union with nullable type
2 parents 4e4fd9d + a5f779c commit 33f70b7

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