mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Infer result of modulo 1 operation statically (#4926)
This commit is contained in:
parent
0397564f6b
commit
c47230c690
@ -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 {
|
||||
|
@ -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' => [
|
||||
|
Loading…
x
Reference in New Issue
Block a user