8000 bug #60632 [TypeInfo] Fix merging collection value types with union t… · symfony/symfony@effb4a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit effb4a7

Browse files
bug #60632 [TypeInfo] Fix merging collection value types with union types (mtarld)
This PR was merged into the 7.3 branch. Discussion ---------- [TypeInfo] Fix merging collection value types with union types | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #60620 | License | MIT Fix merging collection values types. To be properly merged, union types need to be spread, and it was not the case. Commits ------- c0783c3 [TypeInfo] Fix merging collection value types with union types
2 parents 1b70214 + c0783c3 commit effb4a7

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/Symfony/Component/TypeInfo/Tests/TypeResolver/StringTypeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static function resolveDataProvider(): iterable
7979
yield [Type::arrayShape(['foo' => Type::bool()], sealed: false), 'array{foo: bool, ...}'];
8080
yield [Type::arrayShape(['foo' => Type::bool()], extraKeyType: Type::int(), extraValueType: Type::string()), 'array{foo: bool, ...<int, string>}'];
8181
yield [Type::arrayShape(['foo' => Type::bool()], extraValueType: Type::int()), 'array{foo: bool, ...<int>}'];
82+
yield [Type::arrayShape(['foo' => Type::union(Type::bool(), Type::float(), Type::int(), Type::null(), Type::string()), 'bar' => Type::string()]), 'array{foo: scalar|null, bar: string}'];
8283

8384
// object shape
8485
yield [Type::object(), 'object{foo: true, bar: false}'];

src/Symfony/Component/TypeInfo/Type/CollectionType.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,27 @@ public static function mergeCollectionValueTypes(array $types): Type
6565
$boolTypes = [];
6666
$objectTypes = [];
6767

68-
foreach ($types as $t) {
69-
// cannot create an union with a standalone type
70-
if ($t->isIdentifiedBy(TypeIdentifier::MIXED)) {
71-
return Type::mixed();
72-
}
68+
foreach ($types as $type) {
69+
foreach (($type instanceof UnionType ? $type->getTypes() : [$type]) as $t) {
70+
// cannot create an union with a standalone type
71+
if ($t->isIdentifiedBy(TypeIdentifier::MIXED)) {
72+
return Type::mixed();
73+
}
7374

74-
if ($t->isIdentifiedBy(TypeIdentifier::TRUE, TypeIdentifier::FALSE, TypeIdentifier::BOOL)) {
75-
$boolTypes[] = $t;
75+
if ($t->isIdentifiedBy(TypeIdentifier::TRUE, TypeIdentifier::FALSE, TypeIdentifier::BOOL)) {
76+
$boolTypes[] = $t;
7677

77-
continue;
78-
}
78+
continue;
79+
}
7980

80-
if ($t->isIdentifiedBy(TypeIdentifier::OBJECT)) {
81-
$objectTypes[] = $t;
81+
if ($t->isIdentifiedBy(TypeIdentifier::OBJECT)) {
82+
$objectTypes[] = $t;
8283

83-
continue;
84-
}
84+
continue;
85+
}
8586

86-
$normalizedTypes[] = $t;
87+
$normalizedTypes[] = $t;
88+
}
8789
}
8890

8991
$boolTypes = array_unique($boolTypes);

0 commit comments

Comments
 (0)
0