Skip to content
Permalink
Browse files

Improve unvariable removal

  • Loading branch information
muglug committed Nov 13, 2019
1 parent c9e7dcf commit fe5e6758814b3d2350af4fe6322274195a6095cd
@@ -1005,10 +1005,12 @@ private function findAssignStmt(array $stmts, string $var_id, CodeLocation $orig
$assign_exp_found = false;
$i = 0;
while ($i<count($stmts) && !$assign_exp_found) {
while ($i < count($stmts) && !$assign_exp_found) {
$stmt = $stmts[$i];
if ($stmt instanceof PhpParser\Node\Stmt\Expression) {
$search_result = $this->findAssignExp($stmt->expr, $var_id, $original_location->raw_file_start);
$target_exp = $search_result[0];
$levels_taken = $search_result[1];
@@ -1017,8 +1019,43 @@ private function findAssignStmt(array $stmts, string $var_id, CodeLocation $orig
$assign_exp = $target_exp;
$assign_stmt = $levels_taken === 1 ? $stmt : null;
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
if ($search_result[0] && $search_result[1]) {
return $search_result;
}
foreach ($stmt->catches as $catch_stmt) {
$search_result = $this->findAssignStmt($catch_stmt->stmts, $var_id, $original_location);
if ($search_result[0] && $search_result[1]) {
return $search_result;
}
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_
|| $stmt instanceof PhpParser\Node\Stmt\While_
) {
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
if ($search_result[0] && $search_result[1]) {
return $search_result;
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
if ($search_result[0] && $search_result[1]) {
return $search_result;
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
if ($search_result[0] && $search_result[1]) {
return $search_result;
}
}
$i+=1;
$i++;
}
return [$assign_stmt, $assign_exp];
@@ -29,7 +29,27 @@ public function foo() : void {
['UnusedVariable'],
true,
],
'removeUnusedVariableFromTry' => [
'<?php
class A {
public function foo() : void {
try {
$c = false;
$d = null;
} catch (Exception $e) {}
}
}',
'<?php
class A {
public function foo() : void {
try {
} catch (Exception $e) {}
}
}',
'7.1',
['UnusedVariable'],
true,
],
'removeUnusedVariableAndFunctionCall' => [
'<?php
class A {

0 comments on commit fe5e675

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