Skip to content

Commit 198acfd

Browse files
Avoid useless benevolent unions
1 parent 1a47a0a commit 198acfd

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
@@ -850,6 +850,8 @@ public function walkSelectExpression($selectExpression): string
850850
// Here we assume that the value may or may not be casted to
851851
// string by the driver.
852852
$casted = false;
853+
$originalType = $type;
854+
853855
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$casted): Type {
854856
if ($type instanceof UnionType || $type instanceof IntersectionType) {
855857
return $traverse($type);
@@ -867,7 +869,7 @@ public function walkSelectExpression($selectExpression): string
867869

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

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,10 @@ public function getTestData(): iterable
797797
$this->constantArray([
798798
[
799799
new ConstantIntegerType(1),
800-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
800+
TypeCombinator::union(
801801
new StringType(),
802802
new IntegerType()
803-
)),
803+
),
804804
],
805805
[
806806
new ConstantIntegerType(2),
@@ -826,10 +826,10 @@ public function getTestData(): iterable
826826
$this->constantArray([
827827
[
828828
new ConstantIntegerType(1),
829-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
829+
TypeCombinator::union(
830830
new StringType(),
831831
new ConstantIntegerType(0)
832-
)),
832+
),
833833
],
834834
]),
835835
'
@@ -846,10 +846,10 @@ public function getTestData(): iterable
846846
$this->constantArray([
847847
[
848848
new ConstantIntegerType(1),
849-
TypeUtils::toBenevolentUnion(TypeCombinator::union(
849+
TypeCombinator::union(
850850
new StringType(),
851851
new ConstantIntegerType(0)
852-
)),
852+
),
853853
],
854854
]),
855855
'

0 commit comments

Comments
 (0)