From 169b2b702340dfb7004dd8184a9af587cb7c04f6 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 30 Sep 2020 00:04:07 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20analysis=20when=20there=E2=80=99s=20a=20b?= =?UTF-8?q?reak=20in=20a=20loop=20after=20a=20reassignment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Statements/Block/LoopAnalyzer.php | 2 + tests/Loop/ForeachTest.php | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) 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)) {} }' ],