Skip to content

Commit

Permalink
Fix #38 - emit issue for non-array on right too
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 15, 2017
1 parent 0c105d1 commit 4115347
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Checker/Statements/ExpressionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 24 additions & 7 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setUp()
public function testRegularAddition()
{
$stmts = self::$parser->parse('<?php
echo 5 + 4;
$a = 5 + 4;
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
Expand All @@ -56,7 +56,7 @@ public function testBadAddition()
public function testDifferingNumericTypesAdditionInWeakMode()
{
$stmts = self::$parser->parse('<?php
echo 5 + 4.1;
$a = 5 + 4.1;
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
Expand All @@ -73,7 +73,7 @@ public function testDifferingNumericTypesAdditionInStrictMode()
Config::getInstance()->strict_binary_operands = true;

$stmts = self::$parser->parse('<?php
echo 5 + 4.1;
$a = 5 + 4.1;
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
Expand All @@ -87,7 +87,7 @@ public function testNumericAddition()
$a = "5";
if (is_numeric($a)) {
echo $a + 4;
$b = $a + 4;
}
');

Expand All @@ -99,7 +99,7 @@ public function testNumericAddition()
public function testConcatenation()
{
$stmts = self::$parser->parse('<?php
echo "Hey " + "Jude,";
$a = "Hey " . "Jude,";
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
Expand All @@ -110,7 +110,7 @@ public function testConcatenation()
public function testConcatenationWithNumberInWeakMode()
{
$stmts = self::$parser->parse('<?php
echo "hi" . 5;
$a = "hi" . 5;
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
Expand All @@ -127,7 +127,24 @@ public function testConcatenationWithNumberInStrictMode()
Config::getInstance()->strict_binary_operands = true;

$stmts = self::$parser->parse('<?php
echo "hi" . 5;
$a = "hi" . 5;
');

$file_checker = new FileChecker('somefile.php', $this->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('<?php
$a = [1] + 1;
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
Expand Down

0 comments on commit 4115347

Please sign in to comment.