Skip to content

Commit 3d140c0

Browse files
Fix
1 parent 2d31641 commit 3d140c0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
use PHPStan\Type\TypeUtils;
6666
use PHPStan\Type\TypeWithClassName;
6767
use PHPStan\Type\UnionType;
68+
use PHPStan\Type\VerbosityLevel;
69+
6870
use function array_keys;
6971
use function array_merge;
7072
use function assert;
@@ -1479,7 +1481,7 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
14791481
return $union->toNumber();
14801482
}
14811483

1482-
return $this->integerRangeMath($leftType, $expr, $rightType);
1484+
return $this->integerRangeMath($leftNumberType, $expr, $rightNumberType);
14831485
}
14841486

14851487
$specifiedTypes = $this->callOperatorTypeSpecifyingExtensions($expr, $leftType, $rightType);
@@ -1532,7 +1534,6 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
15321534
/**
15331535
* @param ConstantIntegerType|IntegerRangeType $range
15341536
* @param BinaryOp\Div|BinaryOp\Minus|BinaryOp\Mul|BinaryOp\Plus $node
1535-
* @param IntegerRangeType|ConstantIntegerType|UnionType $operand
15361537
*/
15371538
private function integerRangeMath(Type $range, BinaryOp $node, Type $operand): Type
15381539
{
@@ -1549,8 +1550,9 @@ private function integerRangeMath(Type $range, BinaryOp $node, Type $operand): T
15491550
$unionParts = [];
15501551

15511552
foreach ($operand->getTypes() as $type) {
1552-
if ($type instanceof IntegerRangeType || $type instanceof ConstantIntegerType) {
1553-
$unionParts[] = $this->integerRangeMath($range, $node, $type);
1553+
$numberType = $type->toNumber();
1554+
if ($numberType instanceof IntegerRangeType || $numberType instanceof ConstantIntegerType) {
1555+
$unionParts[] = $this->integerRangeMath($range, $node, $numberType);
15541556
} else {
15551557
$unionParts[] = $type->toNumber();
15561558
}
@@ -1564,6 +1566,11 @@ private function integerRangeMath(Type $range, BinaryOp $node, Type $operand): T
15641566
return $union->toNumber();
15651567
}
15661568

1569+
$operand = $operand->toNumber();
1570+
if (!$operand instanceof IntegerRangeType && !$operand instanceof ConstantIntegerType) {
1571+
return $operand;
1572+
}
1573+
15671574
if ($operand instanceof IntegerRangeType) {
15681575
$operandMin = $operand->getMin();
15691576
$operandMax = $operand->getMax();

tests/PHPStan/Analyser/data/bug-8803.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ public function sayHello(): void
1111
$from = new \DateTimeImmutable('2023-01-30');
1212
for ($offset = 1; $offset <= 14; $offset++) {
1313
$value = $from->format('N') + $offset;
14+
$value2 = $offset + $from->format('N');
1415

1516
assertType("'1'|'2'|'3'|'4'|'5'|'6'|'7'", $from->format('N'));
1617
assertType('int<1, 14>', $offset);
1718
assertType('int<2, 21>', $value);
19+
assertType('int<2, 21>', $value2);
1820
}
1921
}
2022

0 commit comments

Comments
 (0)