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

Improve unvariable removal

This commit is contained in:
Brown 2019-11-13 15:05:21 -05:00
parent c9e7dcfa92
commit fe5e675881
2 changed files with 60 additions and 3 deletions

View File

@ -1005,10 +1005,12 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
$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 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
$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];

View File

@ -29,7 +29,27 @@ class UnusedVariableManipulationTest extends FileManipulationTest
['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 {