1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Don’t forget control actions

This commit is contained in:
Brown 2020-09-21 11:18:30 -04:00 committed by Daniil Gentili
parent 0856354b48
commit 81626eee0f
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -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;
}
);
}
}