Skip to content

Commit b860565

Browse files
janedbalondrejmirtes
authored andcommitted
More precise type inference with unary plus and minus
1 parent 0f5e82f commit b860565

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Diff for: src/Type/Doctrine/Query/QueryResultTypeWalker.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,19 @@ public function walkArithmeticFactor($factor): string
12611261
$primary = $factor->arithmeticPrimary;
12621262

12631263
$type = $this->unmarshalType($this->walkArithmeticPrimary($primary));
1264-
$type = TypeUtils::generalizeType($type, GeneralizePrecision::lessSpecific());
1264+
1265+
if ($type instanceof ConstantIntegerType && $factor->sign === false) {
1266+
$type = new ConstantIntegerType($type->getValue() * -1);
1267+
1268+
} elseif ($type instanceof IntegerRangeType && $factor->sign === false) {
1269+
$type = IntegerRangeType::fromInterval(
1270+
$type->getMax() === null ? null : $type->getMax() * -1,
1271+
$type->getMin() === null ? null : $type->getMin() * -1
1272+
);
1273+
1274+
} elseif ($type instanceof ConstantFloatType && $factor->sign === false) {
1275+
$type = new ConstantFloatType($type->getValue() * -1);
1276+
}
12651277

12661278
return $this->marshalType($type);
12671279
}

Diff for: tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ public function getTestData(): iterable
11891189
yield 'arithmetic' => [
11901190
$this->constantArray([
11911191
[new ConstantStringType('intColumn'), new IntegerType()],
1192-
[new ConstantIntegerType(1), $this->intStringified()],
1192+
[new ConstantIntegerType(1), TypeCombinator::union(new ConstantIntegerType(1), new ConstantStringType('1'))],
11931193
[new ConstantIntegerType(2), $this->intStringified()],
11941194
[new ConstantIntegerType(3), TypeCombinator::addNull($this->intStringified())],
11951195
[new ConstantIntegerType(4), $this->intStringified()],

0 commit comments

Comments
 (0)