mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Fix #4366 - possibly-undefined vars in finally block should not error
This commit is contained in:
parent
7c5feb2968
commit
1a6b684993
@ -125,7 +125,11 @@ class TryAnalyzer
|
||||
foreach ($context->vars_in_scope as $var_id => $type) {
|
||||
if (!isset($try_context->vars_in_scope[$var_id])) {
|
||||
$try_context->vars_in_scope[$var_id] = clone $type;
|
||||
$try_context->vars_in_scope[$var_id]->from_docblock = true;
|
||||
|
||||
if (!$stmt->catches) {
|
||||
$context->vars_in_scope[$var_id]->possibly_undefined = true;
|
||||
$context->vars_in_scope[$var_id]->possibly_undefined_from_try = true;
|
||||
}
|
||||
} else {
|
||||
$try_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
||||
$try_context->vars_in_scope[$var_id],
|
||||
@ -440,11 +444,13 @@ class TryAnalyzer
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($definitely_newly_assigned_var_ids as $var_id => $_) {
|
||||
if (isset($context->vars_in_scope[$var_id])) {
|
||||
$new_type = clone $context->vars_in_scope[$var_id];
|
||||
$new_type->possibly_undefined_from_try = false;
|
||||
$context->vars_in_scope[$var_id] = $new_type;
|
||||
if ($stmt->catches) {
|
||||
foreach ($definitely_newly_assigned_var_ids as $var_id => $_) {
|
||||
if (isset($context->vars_in_scope[$var_id])) {
|
||||
$new_type = clone $context->vars_in_scope[$var_id];
|
||||
$new_type->possibly_undefined_from_try = false;
|
||||
$context->vars_in_scope[$var_id] = $new_type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -391,6 +391,29 @@ class TryCatchTest extends TestCase
|
||||
}
|
||||
}'
|
||||
],
|
||||
'finallyArgMaybeUndefined' => [
|
||||
'<?php
|
||||
class TestMe {
|
||||
private function startTransaction(): void {
|
||||
}
|
||||
|
||||
private function endTransaction(bool $commit): void {
|
||||
echo $commit ? "Committing" : "Rolling back";
|
||||
}
|
||||
|
||||
public function doWork(): void {
|
||||
$this->startTransaction();
|
||||
try {
|
||||
$this->workThatMayOrMayNotThrow();
|
||||
$success = true;
|
||||
} finally {
|
||||
$this->endTransaction($success ?? false);
|
||||
}
|
||||
}
|
||||
|
||||
private function workThatMayOrMayNotThrow(): void {}
|
||||
}'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user