diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php index 10d14b45f18..4d87d3749d3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php @@ -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) { @@ -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; diff --git a/tests/BinaryOperationTest.php b/tests/BinaryOperationTest.php index 5ecc5533326..13a995e4f8e 100644 --- a/tests/BinaryOperationTest.php +++ b/tests/BinaryOperationTest.php @@ -592,6 +592,44 @@ function scope(){ '$b' => 'float', ], ], + 'intArithmeticAssignmentAdditionFromFunction' => [ + 'code' => ' [ + '$a===' => 'int', + ], + ], + 'intArithmeticAssignmentAddition' => [ + 'code' => ' [ + '$a===' => '6', + ], + ], + 'intArithmeticAssignmentSubtractionFromFunction' => [ + 'code' => ' [ + '$a===' => 'int', + ], + ], + 'intArithmeticAssignmentSubtraction' => [ + 'code' => ' [ + '$a===' => '3', + ], + ], 'exponent' => [ 'code' => '