From 55245cf5580849950f9d0ea47a28d187437169bf Mon Sep 17 00:00:00 2001 From: orklah Date: Fri, 23 Jul 2021 21:55:48 +0200 Subject: [PATCH] Arithmetic operation between numeric and int or float gives int|float --- .../BinaryOp/NonDivArithmeticOpAnalyzer.php | 12 +++++++---- tests/BinaryOperationTest.php | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonDivArithmeticOpAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonDivArithmeticOpAnalyzer.php index ae3400d4419..eba3358b485 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonDivArithmeticOpAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonDivArithmeticOpAnalyzer.php @@ -667,11 +667,15 @@ private static function analyzeNonDivOperands( } if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) { - $result_type = Type::getInt(); - } elseif (!$result_type) { - $result_type = Type::getNumeric(); + $new_result_type = Type::getInt(); + } else { + $new_result_type = new Type\Union([new TFloat(), new TInt()]); + } + + if (!$result_type) { + $result_type = $new_result_type; } else { - $result_type = Type::combineUnionTypes(Type::getNumeric(), $result_type); + $result_type = Type::combineUnionTypes($new_result_type, $result_type); } $has_valid_right_operand = true; diff --git a/tests/BinaryOperationTest.php b/tests/BinaryOperationTest.php index 0cb9ce124c0..6bfd4beaf53 100644 --- a/tests/BinaryOperationTest.php +++ b/tests/BinaryOperationTest.php @@ -640,6 +640,26 @@ function foo(int $s1): string { return "foo" . $s1; }', ], + 'numericWithInt' => [ + ' [ + '$a' => 'float|int', + '$b' => 'float|int', + '$c' => 'float|int', + '$d' => 'float|int', + ], + ], 'encapsedStringWithIntIncludingLiterals' => [ '