diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index a5ed09611..f0a978f76 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -426,15 +426,13 @@ class LoopAnalyzer $does_always_break = $loop_scope->final_actions === [ScopeAnalyzer::ACTION_BREAK]; if ($does_sometimes_break) { - if ($loop_scope->possibly_redefined_loop_parent_vars !== null) { - foreach ($loop_scope->possibly_redefined_loop_parent_vars as $var => $type) { - $loop_scope->loop_parent_context->vars_in_scope[$var] = Type::combineUnionTypes( - $type, - $loop_scope->loop_parent_context->vars_in_scope[$var] - ); + foreach ($loop_scope->possibly_redefined_loop_parent_vars as $var => $type) { + $loop_scope->loop_parent_context->vars_in_scope[$var] = Type::combineUnionTypes( + $type, + $loop_scope->loop_parent_context->vars_in_scope[$var] + ); - $loop_scope->loop_parent_context->possibly_assigned_var_ids[$var] = true; - } + $loop_scope->loop_parent_context->possibly_assigned_var_ids[$var] = true; } } @@ -573,11 +571,9 @@ class LoopAnalyzer if (!in_array(ScopeAnalyzer::ACTION_CONTINUE, $loop_scope->final_actions, true)) { $loop_scope->loop_context->vars_in_scope = $pre_outer_context->vars_in_scope; } else { - if ($loop_scope->redefined_loop_vars !== null) { - foreach ($loop_scope->redefined_loop_vars as $var => $type) { - $loop_scope->loop_context->vars_in_scope[$var] = $type; - $updated_loop_vars[$var] = true; - } + foreach ($loop_scope->redefined_loop_vars as $var => $type) { + $loop_scope->loop_context->vars_in_scope[$var] = $type; + $updated_loop_vars[$var] = true; } if ($loop_scope->possibly_redefined_loop_vars) { diff --git a/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php index c2bc83491..6110745c3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php @@ -38,15 +38,11 @@ class BreakAnalyzer $redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope); - if ($loop_scope->possibly_redefined_loop_parent_vars === null) { - $loop_scope->possibly_redefined_loop_parent_vars = $redefined_vars; - } else { - foreach ($redefined_vars as $var => $type) { - $loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes( - $type, - $loop_scope->possibly_redefined_loop_parent_vars[$var] ?? null - ); - } + foreach ($redefined_vars as $var => $type) { + $loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes( + $type, + $loop_scope->possibly_redefined_loop_parent_vars[$var] ?? null + ); } if ($loop_scope->iteration_count === 0) { diff --git a/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php index 864c62ede..60cb5527e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php @@ -59,18 +59,14 @@ class ContinueAnalyzer $redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope); - if ($loop_scope->redefined_loop_vars === null) { - $loop_scope->redefined_loop_vars = $redefined_vars; - } else { - foreach ($loop_scope->redefined_loop_vars as $redefined_var => $type) { - if (!isset($redefined_vars[$redefined_var])) { - unset($loop_scope->redefined_loop_vars[$redefined_var]); - } else { - $loop_scope->redefined_loop_vars[$redefined_var] = Type::combineUnionTypes( - $redefined_vars[$redefined_var], - $type - ); - } + foreach ($loop_scope->redefined_loop_vars as $redefined_var => $type) { + if (!isset($redefined_vars[$redefined_var])) { + unset($loop_scope->redefined_loop_vars[$redefined_var]); + } else { + $loop_scope->redefined_loop_vars[$redefined_var] = Type::combineUnionTypes( + $redefined_vars[$redefined_var], + $type + ); } } diff --git a/src/Psalm/Internal/Scope/LoopScope.php b/src/Psalm/Internal/Scope/LoopScope.php index 4bbb2089c..90163c1a3 100644 --- a/src/Psalm/Internal/Scope/LoopScope.php +++ b/src/Psalm/Internal/Scope/LoopScope.php @@ -26,7 +26,7 @@ class LoopScope public $loop_parent_context; /** - * @var array|null + * @var array */ public $redefined_loop_vars = []; @@ -36,9 +36,9 @@ class LoopScope public $possibly_redefined_loop_vars = []; /** - * @var array|null + * @var array */ - public $possibly_redefined_loop_parent_vars; + public $possibly_redefined_loop_parent_vars = []; /** * @var array