Skip to content

Commit

Permalink
Merge pull request #11269 from KevinVanSonsbeek/bugfix/11241-arithmet…
Browse files Browse the repository at this point in the history
…ic-assignment-confusion

Fix type assertion for Arithemtic addition and subtraction
  • Loading branch information
orklah authored Feb 10, 2025
2 parents 3f17a6b + 9b6734d commit 27f85a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ private static function analyzeOperands(
}
}
} elseif ($parent instanceof VirtualPlus || $parent instanceof VirtualMinus) {
// This seems arbitrary defined as 1/-1, to facilitate loops
// and may need a better handling if there's issues
$sum = $parent instanceof VirtualPlus ? 1 : -1;
if ($context && $context->inside_loop && $left_type_part instanceof TLiteralInt) {
if ($parent instanceof VirtualPlus) {
Expand All @@ -831,7 +833,11 @@ private static function analyzeOperands(
$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);
if ($context && $context->inside_assignment) {
$new_type = new TInt();
} else {
$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;
Expand Down
38 changes: 38 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,44 @@ function scope(){
'$b' => 'float',
],
],
'intArithmeticAssignmentAdditionFromFunction' => [
'code' => '<?php
function intFunc(): int {
return 5;
}
$a = 1;
$a += intFunc();',
'assertions' => [
'$a===' => 'int',
],
],
'intArithmeticAssignmentAddition' => [
'code' => '<?php
$a = 1;
$a += 5;',
'assertions' => [
'$a===' => '6',
],
],
'intArithmeticAssignmentSubtractionFromFunction' => [
'code' => '<?php
function intFunc(): int {
return 5;
}
$a = 8;
$a -= intFunc();',
'assertions' => [
'$a===' => 'int',
],
],
'intArithmeticAssignmentSubtraction' => [
'code' => '<?php
$a = 8;
$a -= 5;',
'assertions' => [
'$a===' => '3',
],
],
'exponent' => [
'code' => '<?php
$b = 4 ** 5;',
Expand Down

0 comments on commit 27f85a0

Please sign in to comment.