Skip to content
Permalink
Browse files

Fix #1464 and fix #1128 - reduce try/catch UnusedVariable false posit…

…ives
  • Loading branch information...
muglug committed May 20, 2019
1 parent 1e85380 commit 83a2122f8c037965659c1e13e4d3ad40043868b0
Showing with 58 additions and 12 deletions.
  1. +28 −12 src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
  2. +30 −0 tests/UnusedVariableTest.php
@@ -64,6 +64,7 @@ public static function analyze(
$assigned_var_ids = $try_context->assigned_var_ids;
$context->assigned_var_ids = [];
$old_referenced_var_ids = $try_context->referenced_var_ids;
$old_unreferenced_vars = $try_context->unreferenced_vars;
$newly_unreferenced_vars = [];
$reassigned_vars = [];
@@ -80,6 +81,11 @@ public static function analyze(
$newly_assigned_var_ids
);
$possibly_referenced_var_ids = array_diff(
$context->referenced_var_ids,
$old_referenced_var_ids
);
if ($try_context !== $context) {
foreach ($context->vars_in_scope as $var_id => $type) {
if (!isset($try_context->vars_in_scope[$var_id])) {
@@ -96,7 +102,7 @@ public static function analyze(
$try_context->vars_possibly_in_scope = $context->vars_possibly_in_scope;
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids = array_intersect_key(
$try_context->referenced_var_ids,
$context->referenced_var_ids
);
@@ -304,11 +310,16 @@ function ($fq_catch_class) use ($codebase) {
}
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids = array_intersect_key(
$catch_context->referenced_var_ids,
$context->referenced_var_ids
);
$possibly_referenced_var_ids = array_merge(
$catch_context->referenced_var_ids,
$possibly_referenced_var_ids
);
if ($context->collect_references && $catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]) {
foreach ($context->unreferenced_vars as $var_id => $_) {
if (!isset($catch_context->unreferenced_vars[$var_id])) {
@@ -369,6 +380,21 @@ function ($fq_catch_class) use ($codebase) {
$context->loop_scope->final_actions[] = ScopeAnalyzer::ACTION_NONE;
}
$newly_referenced_var_ids = array_diff_key(
$context->referenced_var_ids,
$old_referenced_var_ids
);
if ($context->collect_references) {
foreach ($old_unreferenced_vars as $var_id => $locations) {
if ((isset($context->unreferenced_vars[$var_id]) && $context->unreferenced_vars[$var_id] !== $locations)
|| (!isset($newly_referenced_var_ids[$var_id]) && isset($possibly_referenced_var_ids[$var_id]))
) {
$statements_analyzer->registerVariableUses($locations);
}
}
}
if ($stmt->finally) {
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
@@ -387,16 +413,6 @@ function ($fq_catch_class) use ($codebase) {
}
}
if ($context->collect_references) {
foreach ($old_unreferenced_vars as $var_id => $locations) {
if (isset($context->unreferenced_vars[$var_id])
&& $context->unreferenced_vars[$var_id] !== $locations
) {
$statements_analyzer->registerVariableUses($locations);
}
}
}
foreach ($existing_thrown_exceptions as $possibly_thrown_exception => $codelocations) {
foreach ($codelocations as $hash => $codelocation) {
$context->possibly_thrown_exceptions[$possibly_thrown_exception][$hash] = $codelocation;
@@ -935,6 +935,36 @@ function foo() : void {
echo $a;
}',
],
'useVariableInsideTry' => [
'<?php
$foo = false;
try {
if (rand(0, 1)) {
throw new \Exception("bad");
}
$foo = rand(0, 1);
if ($foo) {}
} catch (Exception $e) {}
if ($foo) {}',
],
'useTryAssignedVariableInsideFinally' => [
'<?php
$var = "";
try {
if (rand(0, 1)) {
throw new \Exception();
}
$var = "hello";
} finally {
if ($var !== "") {
echo $var;
}
}'
],
];
}

0 comments on commit 83a2122

Please sign in to comment.
You can’t perform that action at this time.