From feeaaeb03c9bfba91805c89d2e7d37967850aeff Mon Sep 17 00:00:00 2001 From: orklah Date: Tue, 1 Dec 2020 18:55:27 +0100 Subject: [PATCH] handle return flag for a try/catch/finally (#4746) * handle return flag for a try/catch/finally * add tests for psalter --- .../Analyzer/Statements/Block/TryAnalyzer.php | 8 +++-- .../ReturnTypeManipulationTest.php | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php index 7f8a82f68..e8645af81 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php @@ -444,6 +444,7 @@ class TryAnalyzer $context->loop_scope->final_actions[] = ScopeAnalyzer::ACTION_NONE; } + $finally_has_returned = false; if ($stmt->finally) { if ($try_context->finally_scope) { $finally_context = clone $context; @@ -455,9 +456,7 @@ class TryAnalyzer $statements_analyzer->analyze($stmt->finally->stmts, $finally_context); - if ($finally_context->has_returned) { - $context->has_returned = true; - } + $finally_has_returned = $finally_context->has_returned; /** @var string $var_id */ foreach ($finally_context->assigned_var_ids as $var_id => $_) { @@ -500,6 +499,9 @@ class TryAnalyzer } } + $body_has_returned = !in_array(ScopeAnalyzer::ACTION_NONE, $stmt_control_actions, true); + $context->has_returned = ($body_has_returned && $all_catches_leave) || $finally_has_returned; + return null; } } diff --git a/tests/FileManipulation/ReturnTypeManipulationTest.php b/tests/FileManipulation/ReturnTypeManipulationTest.php index caeb74b16..ee83044ad 100644 --- a/tests/FileManipulation/ReturnTypeManipulationTest.php +++ b/tests/FileManipulation/ReturnTypeManipulationTest.php @@ -607,6 +607,38 @@ class ReturnTypeManipulationTest extends FileManipulationTest false, true, ], + 'tryCatchReturn' => [ + '