Skip to content

Commit

Permalink
Avoid issue with multiplication by 0
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 4, 2022
1 parent 44da600 commit 968b0ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -1627,10 +1627,10 @@ private function integerRangeMath(Type $range, BinaryOp $node, Type $operand): T
}
}
} elseif ($node instanceof Expr\BinaryOp\Mul) {
$min1 = ($rangeMin ?? -INF) * ($operandMin ?? -INF);
$min2 = ($rangeMin ?? -INF) * ($operandMax ?? INF);
$max1 = ($rangeMax ?? INF) * ($operandMin ?? -INF);
$max2 = ($rangeMax ?? INF) * ($operandMax ?? INF);
$min1 = $rangeMin === 0 || $operandMin === 0 ? 0 : ($rangeMin ?? -INF) * ($operandMin ?? -INF);
$min2 = $rangeMin === 0 || $operandMax === 0 ? 0 : ($rangeMin ?? -INF) * ($operandMax ?? INF);
$max1 = $rangeMax === 0 || $operandMin === 0 ? 0 : ($rangeMax ?? INF) * ($operandMin ?? -INF);
$max2 = $rangeMax === 0 || $operandMax === 0 ? 0 : ($rangeMax ?? INF) * ($operandMax ?? INF);

$min = min($min1, $min2, $max1, $max2);
$max = max($min1, $min2, $max1, $max2);
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/data/integer-range-types.php
Expand Up @@ -342,4 +342,16 @@ public function sayHello($p, $u): void
assertType('float|int<-2, 2>', $p / $u);
}

/**
* @param int<0, max> $positive
* @param int<min, 0> $negative
*/
public function zeroIssues($positive, $negative)
{
assertType('0', 0 * $positive);
assertType('int<0, max>', $positive * $positive);
assertType('0', 0 * $negative);
assertType('int<0, max>', $negative * $negative);
}

}

0 comments on commit 968b0ab

Please sign in to comment.