Skip to content
Permalink
Browse files

Improve dead code detection a little more

  • Loading branch information...
muglug committed May 20, 2019
1 parent 7708e1c commit 8c653b0312c137e6666ec04fee254947cd6290a4
@@ -70,7 +70,7 @@ public static function analyze(
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);
}
@@ -130,21 +130,19 @@ public static function analyze(
$context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope;
}
$context->referenced_var_ids =
$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
);
}
$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
);
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) {
@@ -173,6 +173,7 @@ public static function analyze(
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 @@ public static function analyze(
$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
);
@@ -189,7 +189,10 @@ public static function analyze(
* @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();
@@ -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) {}
@@ -1588,6 +1588,46 @@ function bar() : void {
}',
'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',
],
];
}
}

0 comments on commit 8c653b0

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