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

Fix #1153 - ignore redundant conditions in finally block

This commit is contained in:
Matthew Brown 2019-01-06 12:39:18 -05:00
parent 6972babce0
commit 9c9e57cf6b
2 changed files with 37 additions and 0 deletions

View File

@ -357,7 +357,25 @@ class TryAnalyzer
}
if ($stmt->finally) {
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
$statements_analyzer->analyze($stmt->finally->stmts, $context);
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
}
if ($context->collect_references) {

View File

@ -144,6 +144,25 @@ class TryCatchTest extends TestCase
echo $a;',
],
'noRedundantConditionsInFinally' => [
'<?php
function doThings(): void {}
function message(): string { return "message"; }
$errors = [];
try {
doThings();
} catch (RuntimeException $e) {
$errors["field"] = message();
} catch (LengthException $e) {
$errors[rand(0,1) ? "field" : "field2"] = message();
} finally {
if (!empty($errors)) {
return $errors;
}
}'
],
];
}