From 7fad81edacbfbd66d504fcdf46d1ccf3a72d3bab Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 15 Mar 2017 11:38:23 -0400 Subject: [PATCH] Fix issue with unset var in loop --- src/Psalm/Checker/StatementsChecker.php | 6 ++++++ tests/LoopScopeTest.php | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Psalm/Checker/StatementsChecker.php b/src/Psalm/Checker/StatementsChecker.php index 0a11825e2..775d33f9c 100644 --- a/src/Psalm/Checker/StatementsChecker.php +++ b/src/Psalm/Checker/StatementsChecker.php @@ -400,6 +400,12 @@ class StatementsChecker extends SourceChecker implements StatementsSource } } + foreach ($asserted_vars as $var_id) { + if (!isset($loop_context->vars_in_scope[$var_id])) { + $loop_context->vars_in_scope[$var_id] = $pre_loop_context->vars_in_scope[$var_id]; + } + } + // if there are no changes to the types, no need to re-examine if (!$has_changes) { break; diff --git a/tests/LoopScopeTest.php b/tests/LoopScopeTest.php index 9cb38148e..0da492167 100644 --- a/tests/LoopScopeTest.php +++ b/tests/LoopScopeTest.php @@ -550,6 +550,24 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase $file_checker->visitAndAnalyzeMethods(); } + /** + * @return void + */ + public function testUnsetInLoop() + { + $stmts = self::$parser->parse('project_checker, $stmts); + $file_checker->visitAndAnalyzeMethods(); + } + /** * @return void */