diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index 0ed1a446a..83033d4aa 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -155,6 +155,8 @@ class LoopAnalyzer $original_mixed_counts = $analyzer->getMixedCountsForFile($statements_analyzer->getFilePath()); + $pre_condition_vars_in_scope = $loop_scope->loop_context->vars_in_scope; + IssueBuffer::startRecording(); foreach ($pre_conditions as $condition_offset => $pre_condition) { @@ -315,6 +317,15 @@ class LoopAnalyzer $analyzer->setMixedCountsForFile($statements_analyzer->getFilePath(), $original_mixed_counts); IssueBuffer::startRecording(); + foreach ($pre_loop_context->vars_in_scope as $var_id => $_) { + if (!isset($pre_condition_vars_in_scope[$var_id]) + && strpos($var_id, '->') === false + && strpos($var_id, '[') === false + ) { + $inner_context->vars_in_scope[$var_id]->possibly_undefined = true; + } + } + foreach ($pre_conditions as $condition_offset => $pre_condition) { self::applyPreConditionToLoopContext( $statements_analyzer, diff --git a/tests/Loop/WhileTest.php b/tests/Loop/WhileTest.php index 2e54e1b31..92295b91f 100644 --- a/tests/Loop/WhileTest.php +++ b/tests/Loop/WhileTest.php @@ -394,7 +394,20 @@ class WhileTest extends \Psalm\Tests\TestCase $i++; } }' - ] + ], + 'possiblyUndefinedInWhile' => [ + '