Skip to content

Commit 4ac5a29

Browse files
Avoid useless benevolent unions
1 parent 4fec09d commit 4ac5a29

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
@@ -713,10 +713,10 @@ public function getTestData(): iterable
713713
$this->constantArray([
714714
[
715715
new ConstantIntegerType(1),
716-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
716+
TypeCombinator::union(
717717
new StringType(),
718718
new IntegerType()
719-
)),
719+
),
720720
],
721721
[
722722
new ConstantIntegerType(2),
@@ -742,10 +742,10 @@ public function getTestData(): iterable
742742
$this->constantArray([
743743
[
744744
new ConstantIntegerType(1),
745-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
745+
TypeCombinator::union(
746746
new StringType(),
747747
new ConstantIntegerType(0)
748-
)),
748+
),
749749
],
750750
]),
751751
'
@@ -762,10 +762,10 @@ public function getTestData(): iterable
762762
$this->constantArray([
763763
[
764764
new ConstantIntegerType(1),
765-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
765+
TypeCombinator::union(
766766
new StringType(),
767767
new ConstantIntegerType(0)
768-
)),
768+
),
769769
],
770770
]),
771771
'

0 commit comments

Comments
 (0)