From 81626eee0fbe3c339921f8821be7c66407ae1927 Mon Sep 17 00:00:00 2001 From: Brown Date: Mon, 21 Sep 2020 11:18:30 -0400 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20forget=20control=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Psalm/Internal/Analyzer/ScopeAnalyzer.php | 60 ++++++++++++++----- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php index 0d496b030..c81f3214b 100644 --- a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php @@ -9,6 +9,7 @@ use function strtolower; use function array_merge; use function array_intersect; use function array_unique; +use function array_filter; /** * @internal @@ -96,7 +97,7 @@ class ScopeAnalyzer ($stmt instanceof PhpParser\Node\Stmt\Expression && $stmt->expr instanceof PhpParser\Node\Expr\Exit_) ) { if (!$return_is_exit && $stmt instanceof PhpParser\Node\Stmt\Return_) { - return [self::ACTION_RETURN]; + return array_merge($control_actions, [self::ACTION_RETURN]); } return [self::ACTION_END]; @@ -158,10 +159,10 @@ class ScopeAnalyzer && end($break_types) === 'switch' && (!$stmt->num || !$stmt->num instanceof PhpParser\Node\Scalar\LNumber || $stmt->num->value < 2) ) { - return [self::ACTION_LEAVE_SWITCH]; + return array_merge($control_actions, [self::ACTION_LEAVE_SWITCH]); } - return [self::ACTION_CONTINUE]; + return array_unique(array_merge($control_actions, [self::ACTION_CONTINUE])); } if ($stmt instanceof PhpParser\Node\Stmt\Break_) { @@ -172,7 +173,7 @@ class ScopeAnalyzer return [self::ACTION_LEAVE_SWITCH]; } - return [self::ACTION_BREAK]; + return array_unique(array_merge($control_actions, [self::ACTION_BREAK])); } if ($stmt instanceof PhpParser\Node\Stmt\If_) { @@ -214,11 +215,16 @@ class ScopeAnalyzer return $if_statement_actions; } - $control_actions = array_merge( - $control_actions, - $if_statement_actions, - $else_statement_actions, - $all_elseif_actions + $control_actions = array_filter( + array_merge( + $control_actions, + $if_statement_actions, + $else_statement_actions, + $all_elseif_actions + ), + function ($action) { + return $action !== self::ACTION_NONE; + } ); } @@ -262,7 +268,7 @@ class ScopeAnalyzer } if ($has_default_terminator || isset($stmt->allMatched)) { - return [self::ACTION_END]; + return array_merge($control_actions, [self::ACTION_END]); } } @@ -278,7 +284,12 @@ class ScopeAnalyzer array_merge($break_types, ['loop']) ); - $control_actions = array_merge($control_actions, $do_actions); + $control_actions = array_filter( + array_merge($control_actions, $do_actions), + function ($action) { + return $action !== self::ACTION_NONE; + } + ); } if ($stmt instanceof PhpParser\Node\Stmt\TryCatch) { @@ -322,16 +333,37 @@ class ScopeAnalyzer ); if (!in_array(self::ACTION_NONE, $finally_statement_actions, true)) { - return $finally_statement_actions; + return array_merge( + array_filter( + $control_actions, + function ($action) { + return $action !== self::ACTION_NONE; + } + ), + $finally_statement_actions + ); } } if (!$stmt->catches && !in_array(self::ACTION_NONE, $try_statement_actions, true)) { - return $try_statement_actions; + return array_merge( + array_filter( + $control_actions, + function ($action) { + return $action !== self::ACTION_NONE; + } + ), + $try_statement_actions + ); } } - $control_actions = \array_merge($control_actions, $try_statement_actions); + $control_actions = array_filter( + \array_merge($control_actions, $try_statement_actions), + function ($action) { + return $action !== self::ACTION_NONE; + } + ); } }