Skip to content

Commit

Permalink
Better reconciling of ++/-- operators in ints
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Oct 9, 2023
1 parent ec0dc9e commit 9debd81
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,28 @@ private static function analyzeOperands(
$result_type = Type::getInt();
}
}
} 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);
} elseif ($parent instanceof VirtualPlus || $parent instanceof VirtualMinus) {
$sum = $parent instanceof VirtualPlus ? 1 : -1;
if ($context && $context->inside_loop && $left_type_part instanceof TLiteralInt) {
if ($parent instanceof VirtualPlus) {
$new_type = new TIntRange($left_type_part->value + $sum, null);
} else {
$new_type = new TIntRange(null, $left_type_part->value + $sum);
}
} elseif ($left_type_part instanceof TLiteralInt) {
$new_type = new TLiteralInt($left_type_part->value + $sum);
} elseif ($left_type_part instanceof TIntRange) {
$start = $left_type_part->min_bound === null ? null : $left_type_part->min_bound + $sum;
$end = $left_type_part->max_bound === null ? null : $left_type_part->max_bound + $sum;
$new_type = new TIntRange($start, $end);
} else {
$new_type = new TInt();
}

$result_type = Type::combineUnionTypes(
new Union([$new_type], ['from_calculation' => true]),
$result_type,
);
} else {
$result_type = Type::combineUnionTypes(
$always_positive ? Type::getIntRange(1, null) : Type::getInt(true),
Expand Down
58 changes: 58 additions & 0 deletions tests/TypeReconciliation/RedundantConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,64 @@ function foo(int $x) : void {
}
}',
],
'allowIntValueCheckAfterComparisonDueToUnderflow' => [
'code' => '<?php
function foo(int $x) : void {
$x = $x - 1;
if (!is_int($x)) {
echo "Is a float.";
} else {
echo "Is an int.";
}
}
function bar(int $x) : void {
$x = $x - 1;
if (is_float($x)) {
echo "Is a float.";
} else {
echo "Is an int.";
}
}',
],
'allowIntValueCheckAfterComparisonDueToUnderflowDec' => [
'code' => '<?php
function foo(int $x) : void {
$x--;
if (!is_int($x)) {
echo "Is a float.";
} else {
echo "Is an int.";
}
}
function bar(int $x) : void {
$x--;
if (is_float($x)) {
echo "Is a float.";
} else {
echo "Is an int.";
}
}',
],
'allowIntValueCheckAfterComparisonDueToConditionalUnderflow' => [
'code' => '<?php
function foo(int $x) : void {
if (rand(0, 1)) {
$x = $x - 1;
}
if (is_float($x)) {
echo "Is a float.";
} else {
echo "Is an int.";
}
}',
],
'changeStringValue' => [
'code' => '<?php
$concat = "";
Expand Down

0 comments on commit 9debd81

Please sign in to comment.