diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index 3d38c3c56..c6955f5f4 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -451,6 +451,8 @@ class LoopAnalyzer $type, $loop_scope->loop_parent_context->vars_in_scope[$var] ); + + $loop_scope->loop_parent_context->possibly_assigned_var_ids[$var] = true; } } } diff --git a/tests/Loop/ForeachTest.php b/tests/Loop/ForeachTest.php index 87a54d5c7..f7025ad54 100644 --- a/tests/Loop/ForeachTest.php +++ b/tests/Loop/ForeachTest.php @@ -1057,7 +1057,7 @@ class ForeachTest extends \Psalm\Tests\TestCase } }' ], - 'loopCanUpdateOuter' => [ + 'loopCanUpdateOuterWithoutBreak' => [ ' $mappings @@ -1069,6 +1069,42 @@ class ForeachTest extends \Psalm\Tests\TestCase } } + if (is_int($id)) {} + }' + ], + 'loopCanUpdateOuterWithBreak' => [ + ' $mappings + */ + function foo(string $id, array $mappings) : void { + if ($id === "a") { + foreach ($mappings as $value) { + if (rand(0, 1)) { + $id = $value; + break; + } + } + } + + if (is_int($id)) {} + }' + ], + 'loopCanUpdateOuterWithContinue' => [ + ' $mappings + */ + function foo(string $id, array $mappings) : void { + if ($id === "a") { + foreach ($mappings as $value) { + if (rand(0, 1)) { + $id = $value; + continue; + } + } + } + if (is_int($id)) {} }' ],