From a87a1f0dc9b95584bad6b0109b857dea3900bea6 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Wed, 6 Dec 2017 00:35:41 -0500 Subject: [PATCH] Make sure undeclared array offset vars have type mixed Fixes #360 --- .../Checker/Statements/ExpressionChecker.php | 37 ++++++++++++------- tests/RedundantConditionTest.php | 12 ++++++ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/Psalm/Checker/Statements/ExpressionChecker.php b/src/Psalm/Checker/Statements/ExpressionChecker.php index 0bc82bdd8..354249403 100644 --- a/src/Psalm/Checker/Statements/ExpressionChecker.php +++ b/src/Psalm/Checker/Statements/ExpressionChecker.php @@ -611,14 +611,19 @@ class ExpressionChecker ); } } elseif ($context->check_variables) { - IssueBuffer::add( + if (IssueBuffer::accepts( new UndefinedVariable( 'Cannot find referenced variable ' . $var_name, new CodeLocation($statements_checker->getSource(), $stmt) - ) - ); + ), + $statements_checker->getSuppressedIssues() + )) { + return false; + } - return false; + $stmt->inferredType = Type::getMixed(); + + return null; } } @@ -1884,14 +1889,17 @@ class ExpressionChecker if (!isset($context->vars_possibly_in_scope[$use_var_id])) { if ($context->check_variables) { - IssueBuffer::add( + if (IssueBuffer::accepts( new UndefinedVariable( 'Cannot find referenced variable ' . $use_var_id, new CodeLocation($statements_checker->getSource(), $use) - ) - ); + ), + $statements_checker->getSuppressedIssues() + )) { + return false; + } - return false; + return null; } } @@ -1909,18 +1917,21 @@ class ExpressionChecker return false; } - return null; + continue; } if ($context->check_variables) { - IssueBuffer::add( + if (IssueBuffer::accepts( new UndefinedVariable( 'Cannot find referenced variable ' . $use_var_id, new CodeLocation($statements_checker->getSource(), $use) - ) - ); + ), + $statements_checker->getSuppressedIssues() + )) { + return false; + } - return false; + continue; } } } diff --git a/tests/RedundantConditionTest.php b/tests/RedundantConditionTest.php index 5a2086deb..3df1bc6e5 100644 --- a/tests/RedundantConditionTest.php +++ b/tests/RedundantConditionTest.php @@ -144,6 +144,18 @@ class RedundantConditionTest extends TestCase if ($a->foo() || $a->bar()) {}', ], + 'noEmptyUndefinedArrayVar' => [ + ' [], + 'error_levels' => ['MixedAssignment', 'MixedArrayAccess'], + ], ]; }