Skip to content

Commit ea736cc

Browse files
committed
Fix div by zero errors
1 parent 4ec86b7 commit ea736cc

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,8 @@ private function resolveType(Expr $node): Type
13351335
$min = $leftMin !== null && $rightMin !== null ? $leftMin * $rightMin : null;
13361336
$max = $leftMax !== null && $rightMax !== null ? $leftMax * $rightMax : null;
13371337
} else {
1338-
$min = $leftMin !== null && $rightMin !== null ? (int) ($leftMin / $rightMin) : null;
1339-
$max = $leftMax !== null && $rightMax !== null ? (int) ($leftMax / $rightMax) : null;
1338+
$min = $leftMin !== null && $rightMin !== null && $rightMin !== 0 ? (int) ($leftMin / $rightMin) : null;
1339+
$max = $leftMax !== null && $rightMax !== null && $rightMax !== 0 ? (int) ($leftMax / $rightMax) : null;
13401340

13411341
if ($min !== null && $max !== null && $min > $max) {
13421342
[$min, $max] = [$max, $min];

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ public function dataFileAsserts(): iterable
485485
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5529.php');
486486

487487
yield from $this->gatherAssertTypes(__DIR__ . '/data/sizeof.php');
488+
489+
yield from $this->gatherAssertTypes(__DIR__ . '/data/div-by-zero.php');
488490
}
489491

490492
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DivByZero;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param int<0, max> $range1
12+
* @param int<min, 0> $range2
13+
*/
14+
public function doFoo(int $range1, int $range2): void
15+
{
16+
assertType('(float|int)', 5 / $range1);
17+
assertType('(float|int)', 5 / $range2);
18+
assertType('*ERROR*', 5 / 0);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)