Skip to content

Commit

Permalink
modulo '1' is always zero
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Feb 1, 2022
1 parent 28179f2 commit f9277dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Analyser/MutatingScope.php
Expand Up @@ -1051,12 +1051,24 @@ private function resolveType(Expr $node): Type
} else {
$right = $node->right;
}
$rightType = $this->getType($right);

$stringType = $rightType->toString();
if (
$node instanceof Node\Expr\BinaryOp\Mod
|| $node instanceof Node\Expr\AssignOp\Mod
) {
if ($stringType instanceof ConstantScalarType && $stringType->getValue() === '1') {
return new ConstantIntegerType(0);
}
}

$rightScalarTypes = TypeUtils::getConstantScalars($rightType->toNumber());
foreach ($rightScalarTypes as $scalarType) {

$rightTypes = TypeUtils::getConstantScalars($this->getType($right)->toNumber());
foreach ($rightTypes as $rightType) {
if (
$rightType->getValue() === 0
|| $rightType->getValue() === 0.0
$scalarType->getValue() === 0
|| $scalarType->getValue() === 0.0
) {
return new ErrorType();
}
Expand All @@ -1078,15 +1090,15 @@ private function resolveType(Expr $node): Type
}

$leftTypes = TypeUtils::getConstantScalars($this->getType($left));
$rightTypes = TypeUtils::getConstantScalars($this->getType($right));
$rightScalarTypes = TypeUtils::getConstantScalars($this->getType($right));

$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
$rightTypesCount = count($rightScalarTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
$resultTypes = [];
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
foreach ($leftTypes as $leftType) {
foreach ($rightTypes as $rightType) {
foreach ($rightScalarTypes as $rightType) {
$resultType = $this->calculateFromScalars($node, $leftType, $rightType);
if ($generalize) {
$resultType = TypeUtils::generalizeType($resultType, GeneralizePrecision::lessSpecific());
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/modulo-operator.php
Expand Up @@ -45,4 +45,26 @@ function doBar(int $i, int $j, $p, $range, $zeroOrMore, $intConst, $unionRange,

assertType('int', $j % $i);
}

function moduleOne(int $i, float $f) {
assertType('0', true % '1');
assertType('0', false % '1');
assertType('0', null % '1');
assertType('0', -1 % '1');
assertType('0', 0 % '1');
assertType('0', 1 % '1');
assertType('0', '1' % '1');
assertType('0', 1.24 % '1');

assertType('0', $i % '1');
assertType('0', $f % '1');

assertType('0', $i % true);
assertType('0', $f % true);

$i %= '1';
$f %= '1';
assertType('0', $i);
assertType('0', $f);
}
}

0 comments on commit f9277dd

Please sign in to comment.