Skip to content

Commit 771aaa0

Browse files
Avoid useless benevolent unions
1 parent d7791bc commit 771aaa0

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Type/Doctrine/Query/QueryResultTypeWalker.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,8 @@ public function walkSelectExpression($selectExpression)
847847
// Here we assume that the value may or may not be casted to
848848
// string by the driver.
849849
$casted = false;
850+
$originalType = $type;
851+
850852
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$casted): Type {
851853
if ($type instanceof UnionType || $type instanceof IntersectionType) {
852854
return $traverse($type);
@@ -864,7 +866,7 @@ public function walkSelectExpression($selectExpression)
864866

865867
// Since we made supposition about possibly casted values,
866868
// we can only provide a benevolent union.
867-
if ($casted && $type instanceof UnionType) {
869+
if ($casted && $type instanceof UnionType && !$originalType->equals($type)) {
868870
$type = TypeUtils::toBenevolentUnion($type);
869871
}
870872
}

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,10 @@ public function getTestData(): iterable
714714
$this->constantArray([
715715
[
716716
new ConstantIntegerType(1),
717-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
717+
TypeCombinator::union(
718718
new StringType(),
719719
new IntegerType()
720-
)),
720+
),
721721
],
722722
[
723723
new ConstantIntegerType(2),
@@ -743,10 +743,10 @@ public function getTestData(): iterable
743743
$this->constantArray([
744744
[
745745
new ConstantIntegerType(1),
746-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
746+
TypeCombinator::union(
747747
new StringType(),
748748
new ConstantIntegerType(0)
749-
)),
749+
),
750750
],
751751
]),
752752
'
@@ -763,10 +763,10 @@ public function getTestData(): iterable
763763
$this->constantArray([
764764
[
765765
new ConstantIntegerType(1),
766-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
766+
TypeCombinator::union(
767767
new StringType(),
768768
new ConstantIntegerType(0)
769-
)),
769+
),
770770
],
771771
]),
772772
'

0 commit comments

Comments
 (0)