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

Evaluate finally blocks at the end of every catch

This commit is contained in:
Brown 2019-11-11 18:38:33 -05:00
parent da03902f76
commit 51557c44ea
2 changed files with 35 additions and 1 deletions

View File

@ -436,6 +436,24 @@ class TryAnalyzer
);
}
}
if ($stmt->finally) {
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
foreach ($issues_to_suppress as $issue_to_suppress) {
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues([$issue_to_suppress]);
}
}
$statements_analyzer->analyze($stmt->finally->stmts, $catch_context);
foreach ($issues_to_suppress as $issue_to_suppress) {
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues([$issue_to_suppress]);
}
}
}
}
foreach ($definitely_newly_assigned_var_ids as $var_id => $_) {

View File

@ -984,7 +984,9 @@ class UnusedVariableTest extends TestCase
} catch (\Exception $e) {
throw new \Exception("Something went wrong");
} finally {
\fclose($stream);
if ($stream) {
\fclose($stream);
}
}',
],
'varUsedInloop' => [
@ -1216,6 +1218,20 @@ class UnusedVariableTest extends TestCase
return "hello";
}',
],
'useTryAndCatchAssignedVariableInsideFinally' => [
'<?php
function foo() : void {
try {
// do something dangerous
$a = 5;
} catch (Exception $e) {
$a = 4;
throw new Exception("bad");
} finally {
echo $a;
}
}'
],
];
}