Skip to content

Commit

Permalink
Update vars_possibly_in_scope in TryAnalyzer.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrolGenhald committed Feb 20, 2022
1 parent 9a3b46b commit c32ca03
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
Expand Up @@ -561,6 +561,7 @@ private static function applyCatchContextsForFinallyContext(
$context->vars_in_scope[$var_id] = Type::combineUnionTypeArray($types, $codebase);
$context->vars_in_scope[$var_id]->possibly_undefined = $possibly_undefined;
$context->vars_in_scope[$var_id]->possibly_undefined_from_try = false;
$context->vars_possibly_in_scope[$var_id] = true;
}
}

Expand Down Expand Up @@ -630,6 +631,7 @@ private static function applyCatchContextsForOuterContext(
$context->vars_in_scope[$var_id] = Type::combineUnionTypeArray($types, $codebase);
$context->vars_in_scope[$var_id]->possibly_undefined = $possibly_undefined;
$context->vars_in_scope[$var_id]->possibly_undefined_from_try = false;
$context->vars_possibly_in_scope[$var_id] = true;
}

if (count($leaving_catches) < $catch_block_count) {
Expand All @@ -645,6 +647,7 @@ private static function applyCatchContextsForOuterContext(
// Set all variables to their types at the end of the `try` block, since
// it would have to complete successfully for execution to continue.
$context->vars_in_scope = $end_of_try_context->vars_in_scope;
$context->vars_possibly_in_scope = $end_of_try_context->vars_possibly_in_scope;
$context->references_in_scope = $end_of_try_context->references_in_scope;
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/TryCatchTest.php
Expand Up @@ -1082,6 +1082,22 @@ function maybeThrow(): void {}
'$foo===' => '1|2',
],
],
'tryInsideIfPossiblyUndefined' => [
'code' => '<?php
function foo(): void
{
if (random_int(0, 1)) {
try {
} catch (Exception $e) {
}
}
if (isset($e)) {
throw $e;
}
}
',
],
];
}

Expand Down

0 comments on commit c32ca03

Please sign in to comment.