Skip to content

Commit

Permalink
Correct decrement min/max ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Oct 9, 2023
1 parent f307b8c commit ec0dc9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Psalm\Issue\PossiblyNullOperand;
use Psalm\Issue\StringIncrement;
use Psalm\IssueBuffer;
use Psalm\Node\Expr\BinaryOp\VirtualMinus;
use Psalm\Node\Expr\BinaryOp\VirtualPlus;
use Psalm\StatementsSource;
use Psalm\Type;
use Psalm\Type\Atomic;
Expand Down Expand Up @@ -826,10 +828,15 @@ private static function analyzeOperands(
$result_type = Type::getInt();
}
}
} else {
} elseif ($parent instanceof VirtualPlus) {
$start = $left_type_part instanceof TLiteralInt ? $left_type_part->value : 1;
$result_type = Type::combineUnionTypes(Type::getIntRange($start, null), $result_type);
} elseif ($parent instanceof VirtualMinus) {
$start = $left_type_part instanceof TLiteralInt ? $left_type_part->value : 1;
$result_type = Type::combineUnionTypes(Type::getIntRange(null, $start), $result_type);
} else {
$result_type = Type::combineUnionTypes(
$always_positive ? Type::getIntRange($start, null) : Type::getInt(true),
$always_positive ? Type::getIntRange(1, null) : Type::getInt(true),
$result_type,
);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,23 @@ function scope(array $a): int|float {
'$j' => 'int<100, 110>',
],
],
'decrementInLoop' => [
'code' => '<?php
for ($i = 10; $i > 0; $i--) {
if (rand(0,1)) {
break;
}
}
for ($j = 110; $j > 100; $j--) {
if (rand(0,1)) {
break;
}
}',
'assertions' => [
'$i' => 'int<0, 10>',
'$j' => 'int<100, 110>',
],
],
'coalesceFilterOutNullEvenWithTernary' => [
'code' => '<?php
Expand Down

0 comments on commit ec0dc9e

Please sign in to comment.