Skip to content

Commit 157d521

Browse files
Avoid useless benevolent unions
1 parent 1471e14 commit 157d521

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Type/Doctrine/Query/QueryResultTypeWalker.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ public function walkSelectExpression($selectExpression)
848848
// Here we assume that the value may or may not be casted to
849849
// string by the driver.
850850
$casted = false;
851+
$originalType = $type;
852+
851853
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$casted): Type {
852854
if ($type instanceof UnionType || $type instanceof IntersectionType) {
853855
return $traverse($type);
@@ -865,7 +867,7 @@ public function walkSelectExpression($selectExpression)
865867

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

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

+6-6
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)