diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index f968f2569..960627fb2 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -279,6 +279,8 @@ class LoopAnalyzer // if there's a change, invalidate related clauses $pre_loop_context->removeVarFromConflictingClauses($var_id); + + $loop_scope->loop_parent_context->possibly_assigned_var_ids[$var_id] = true; } if (isset($loop_scope->loop_context->vars_in_scope[$var_id]) diff --git a/tests/Loop/ForeachTest.php b/tests/Loop/ForeachTest.php index 240dd141a..87a54d5c7 100644 --- a/tests/Loop/ForeachTest.php +++ b/tests/Loop/ForeachTest.php @@ -1057,6 +1057,21 @@ class ForeachTest extends \Psalm\Tests\TestCase } }' ], + 'loopCanUpdateOuter' => [ + ' $mappings + */ + function foo(string $id, array $mappings) : void { + if ($id === "a") { + foreach ($mappings as $value) { + $id = $value; + } + } + + if (is_int($id)) {} + }' + ], ]; }