1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Infer result of modulo 1 operation statically (#4926)

This commit is contained in:
orklah 2021-01-03 17:56:04 +01:00 committed by GitHub
parent 0397564f6b
commit c47230c690
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -674,9 +674,18 @@ class NonDivArithmeticOpAnalyzer
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = $always_positive
? new Type\Union([new Type\Atomic\TPositiveInt(), new TLiteralInt(0)])
: Type::getInt();
if ($always_positive) {
if ($right_type_part instanceof TLiteralInt && $right_type_part->value === 1) {
$result_type = Type::getInt(true, 0);
} else {
$result_type = new Type\Union([
new Type\Atomic\TPositiveInt(),
new TLiteralInt(0)
]);
}
} else {
$result_type = Type::getInt();
}
} elseif (!$result_type) {
$result_type = $always_positive ? Type::getPositiveInt(true) : Type::getInt(true);
} else {

View File

@ -131,12 +131,14 @@ class BinaryOperationTest extends TestCase
$a = 25 % 2;
$b = 25.4 % 2;
$c = 25 % 2.5;
$d = 25.5 % 2.5;',
$d = 25.5 % 2.5;
$e = 25 % 1;',
'assertions' => [
'$a' => 'int',
'$b' => 'int',
'$c' => 'int',
'$d' => 'int',
'$e' => 'int',
],
],
'numericAddition' => [