From 41153473fdfe2523d40acef3e3eac8c45a296a3a Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Fri, 13 Jan 2017 13:09:52 -0500 Subject: [PATCH] Fix #38 - emit issue for non-array on right too --- .../Checker/Statements/ExpressionChecker.php | 10 ++++++ tests/BinaryOperationTest.php | 31 ++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Psalm/Checker/Statements/ExpressionChecker.php b/src/Psalm/Checker/Statements/ExpressionChecker.php index a6ccc85b362..5dbd766f464 100644 --- a/src/Psalm/Checker/Statements/ExpressionChecker.php +++ b/src/Psalm/Checker/Statements/ExpressionChecker.php @@ -926,6 +926,16 @@ public static function analyzeNonDivArithmenticOp( )) { // fall through } + } elseif (!$right_type_part->isArray()) { + if (IssueBuffer::accepts( + new InvalidOperand( + 'Cannot add an array to a non-array', + new CodeLocation($statements_checker->getSource(), $right) + ), + $statements_checker->getSuppressedIssues() + )) { + // fall through + } } $result_type = Type::getArray(); diff --git a/tests/BinaryOperationTest.php b/tests/BinaryOperationTest.php index e92427bc7fe..29358a26640 100644 --- a/tests/BinaryOperationTest.php +++ b/tests/BinaryOperationTest.php @@ -30,7 +30,7 @@ public function setUp() public function testRegularAddition() { $stmts = self::$parser->parse('project_checker, $stmts); @@ -56,7 +56,7 @@ public function testBadAddition() public function testDifferingNumericTypesAdditionInWeakMode() { $stmts = self::$parser->parse('project_checker, $stmts); @@ -73,7 +73,7 @@ public function testDifferingNumericTypesAdditionInStrictMode() Config::getInstance()->strict_binary_operands = true; $stmts = self::$parser->parse('project_checker, $stmts); @@ -87,7 +87,7 @@ public function testNumericAddition() $a = "5"; if (is_numeric($a)) { - echo $a + 4; + $b = $a + 4; } '); @@ -99,7 +99,7 @@ public function testNumericAddition() public function testConcatenation() { $stmts = self::$parser->parse('project_checker, $stmts); @@ -110,7 +110,7 @@ public function testConcatenation() public function testConcatenationWithNumberInWeakMode() { $stmts = self::$parser->parse('project_checker, $stmts); @@ -127,7 +127,24 @@ public function testConcatenationWithNumberInStrictMode() Config::getInstance()->strict_binary_operands = true; $stmts = self::$parser->parse('project_checker, $stmts); + $context = new Context('somefile.php'); + $file_checker->visitAndAnalyzeMethods($context); + } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage InvalidOperand + */ + public function testAddArrayToNumber() + { + Config::getInstance()->strict_binary_operands = true; + + $stmts = self::$parser->parse('project_checker, $stmts);