1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

handle return flag for a try/catch/finally (#4746)

* handle return flag for a try/catch/finally

* add tests for psalter
This commit is contained in:
orklah 2020-12-01 18:55:27 +01:00 committed by Daniil Gentili
parent 0814f5d6ee
commit feeaaeb03c
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 37 additions and 3 deletions

View File

@ -444,6 +444,7 @@ class TryAnalyzer
$context->loop_scope->final_actions[] = ScopeAnalyzer::ACTION_NONE; $context->loop_scope->final_actions[] = ScopeAnalyzer::ACTION_NONE;
} }
$finally_has_returned = false;
if ($stmt->finally) { if ($stmt->finally) {
if ($try_context->finally_scope) { if ($try_context->finally_scope) {
$finally_context = clone $context; $finally_context = clone $context;
@ -455,9 +456,7 @@ class TryAnalyzer
$statements_analyzer->analyze($stmt->finally->stmts, $finally_context); $statements_analyzer->analyze($stmt->finally->stmts, $finally_context);
if ($finally_context->has_returned) { $finally_has_returned = $finally_context->has_returned;
$context->has_returned = true;
}
/** @var string $var_id */ /** @var string $var_id */
foreach ($finally_context->assigned_var_ids as $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; return null;
} }
} }

View File

@ -607,6 +607,38 @@ class ReturnTypeManipulationTest extends FileManipulationTest
false, false,
true, true,
], ],
'tryCatchReturn' => [
'<?php
function scope(){
try{
return func();
}
catch(Exception $e){
return null;
}
}
function func(): stdClass{
return new stdClass();
}',
'<?php
function scope(): ?stdClass{
try{
return func();
}
catch(Exception $e){
return null;
}
}
function func(): stdClass{
return new stdClass();
}',
'7.3',
['MissingReturnType'],
false,
true,
],
]; ];
} }
} }