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

Improve dead code detection a little more

This commit is contained in:
Brown 2019-05-20 18:25:11 -04:00
parent 7708e1c489
commit 8c653b0312
6 changed files with 60 additions and 18 deletions

View File

@ -70,7 +70,7 @@ class DoAnalyzer
continue;
}
if ($do_context->hasVariable($var)) {
if (isset($do_context->vars_in_scope[$var])) {
if ($do_context->vars_in_scope[$var]->getId() !== $type->getId()) {
$do_context->vars_in_scope[$var] = Type::combineUnionTypes($do_context->vars_in_scope[$var], $type);
}

View File

@ -130,21 +130,19 @@ class ForAnalyzer
$context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope;
}
$context->referenced_var_ids =
$for_context->referenced_var_ids + $context->referenced_var_ids;
$context->referenced_var_ids = array_intersect_key(
$for_context->referenced_var_ids,
$context->referenced_var_ids
);
if ($context->collect_references) {
$context->unreferenced_vars = array_intersect_key(
$for_context->unreferenced_vars,
$context->unreferenced_vars
);
}
if ($context->collect_references) {
$context->unreferenced_vars = array_intersect_key(
$for_context->unreferenced_vars,
$context->unreferenced_vars
);
foreach ($for_context->unreferenced_vars as $var_id => $locations) {
if (isset($context->unreferenced_vars[$var_id])) {
$context->unreferenced_vars[$var_id] += $locations;
} else {
$context->unreferenced_vars[$var_id] = $locations;
}
}
}
if ($context->collect_exceptions) {

View File

@ -173,6 +173,7 @@ class ForeachAnalyzer
if ($context->collect_references && !isset($foreach_context->byref_constraints[$key_var_id])) {
$foreach_context->unreferenced_vars[$key_var_id] = [$location->getHash() => $location];
unset($foreach_context->referenced_var_ids[$key_var_id]);
}
if (!$statements_analyzer->hasVariable($key_var_id)) {
@ -267,7 +268,7 @@ class ForeachAnalyzer
$context->vars_possibly_in_scope
);
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids = array_intersect_key(
$foreach_context->referenced_var_ids,
$context->referenced_var_ids
);

View File

@ -189,7 +189,10 @@ class LoopAnalyzer
* @var array<string, bool>
*/
$new_referenced_var_ids = $inner_context->referenced_var_ids;
$inner_context->referenced_var_ids = $old_referenced_var_ids + $inner_context->referenced_var_ids;
$inner_context->referenced_var_ids = array_intersect_key(
$old_referenced_var_ids,
$inner_context->referenced_var_ids
);
$recorded_issues = IssueBuffer::clearRecordingLevel();
IssueBuffer::stopRecording();

View File

@ -22,7 +22,7 @@ function call(callable $gen) : Promise {}
*/
interface Promise {
/**
* @param callable(\Throwable|null $exception, TReturn|null $result):void
* @param callable(?\Throwable, ?TReturn):void $onResolved
* @return void
*/
function onResolve(callable $onResolved);
@ -39,7 +39,7 @@ class Success implements Promise {
*/
public function __construct($value = null) {}
/**
* @param callable(\Throwable|null $exception, TReturn|null $result):void
* @param callable(?Throwable, ?TReturn):void $onResolved
* @return void
*/
function onResolve(callable $onResolved) {}

View File

@ -1588,6 +1588,46 @@ class UnusedVariableTest extends TestCase
}',
'error_message' => 'UnusedVariable',
],
'detectUnusedVariableReassignedInIfFollowedByTryInsideForLoop' => [
'<?php
$user_id = 0;
$user = null;
if (rand(0, 1)) {
$user_id = rand(0, 1);
$user = $user_id;
}
if ($user) {
$a = 0;
for ($i = 1; $i <= 10; $i++) {
$a += $i;
try {} catch (\Exception $e) {}
}
echo $i;
}',
'error_message' => 'UnusedVariable',
],
'detectUnusedVariableReassignedInIfFollowedByTryInsideForeachLoop' => [
'<?php
$user_id = 0;
$user = null;
if (rand(0, 1)) {
$user_id = rand(0, 1);
$user = $user_id;
}
if ($user) {
$a = 0;
foreach ([1, 2, 3] as $i) {
$a += $i;
try {} catch (\Exception $e) {}
}
echo $i;
}',
'error_message' => 'UnusedVariable',
],
];
}
}