From 5390d1bf4e95b974e65058e765c342e7d9aead54 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 14 Nov 2023 16:03:21 +0100 Subject: [PATCH 1/4] Added failling test --- .../Fixture/function.php.inc | 20 +++++++++++++++++++ .../Fixture/skip_used_function.php.inc | 14 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/function.php.inc create mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_used_function.php.inc diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/function.php.inc b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/function.php.inc new file mode 100644 index 00000000000..7fd57c7d69b --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/function.php.inc @@ -0,0 +1,20 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_used_function.php.inc b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_used_function.php.inc new file mode 100644 index 00000000000..7585f80ba80 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_used_function.php.inc @@ -0,0 +1,14 @@ + From bb7b6e3b01fff929c608354044a9902704c04ca4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 14 Nov 2023 16:07:27 +0100 Subject: [PATCH 2/4] fix --- .../RemoveUnusedVariableAssignRector.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php index b14f4283309..1e535c0cb45 100644 --- a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php @@ -69,16 +69,16 @@ public function run() */ public function getNodeTypes(): array { - return [ClassMethod::class]; + return [ClassMethod::class, Stmt\Function_::class]; } /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?ClassMethod + public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Stmt\Function_ { $classMethodStmts = $node->stmts; - if ($classMethodStmts === null) { + if ($classMethodStmts === null || $classMethodStmts === []) { return null; } @@ -144,15 +144,15 @@ private function hasCallLikeInAssignExpr(Expr $expr, Scope $scope): bool } private function isVariableUsedInFollowingStmts( - ClassMethod $classMethod, - int $assignStmtPosition, - string $variableName + ClassMethod|Stmt\Function_ $functionLike, + int $assignStmtPosition, + string $variableName ): bool { - if ($classMethod->stmts === null) { + if ($functionLike->stmts === null) { return false; } - foreach ($classMethod->stmts as $key => $stmt) { + foreach ($functionLike->stmts as $key => $stmt) { // do not look yet if ($key <= $assignStmtPosition) { continue; @@ -185,9 +185,9 @@ private function containsCompactFuncCall(ClassMethod|Node $node): bool return $compactFuncCall instanceof FuncCall; } - private function containsFileIncludes(ClassMethod $classMethod): bool + private function containsFileIncludes(ClassMethod|Stmt\Function_ $functionLike): bool { - return (bool) $this->betterNodeFinder->findInstancesOf($classMethod, [Include_::class]); + return (bool) $this->betterNodeFinder->findInstancesOf($functionLike, [Include_::class]); } /** From 581eebae583ad17d8d44cda6ed052482d79cf52e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 14 Nov 2023 16:10:33 +0100 Subject: [PATCH 3/4] stan fixes --- .../Rector/Assign/RemoveUnusedVariableAssignRector.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php index 1e535c0cb45..f8709b61d66 100644 --- a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php @@ -73,7 +73,7 @@ public function getNodeTypes(): array } /** - * @param ClassMethod $node + * @param ClassMethod|Stmt\Function_ $node */ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Stmt\Function_ { @@ -172,9 +172,9 @@ private function isVariableUsedInFollowingStmts( return false; } - private function containsCompactFuncCall(ClassMethod|Node $node): bool + private function containsCompactFuncCall(ClassMethod|Stmt\Function_ $functionLike): bool { - $compactFuncCall = $this->betterNodeFinder->findFirst($node, function (Node $node): bool { + $compactFuncCall = $this->betterNodeFinder->findFirst($functionLike, function (Node $node): bool { if (! $node instanceof FuncCall) { return false; } From c3dccf236c98a0a5b640e7838f986455d0710dee Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 14 Nov 2023 16:11:35 +0100 Subject: [PATCH 4/4] cleanup --- .../Rector/Assign/RemoveUnusedVariableAssignRector.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php index f8709b61d66..9267c271cca 100644 --- a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php @@ -77,8 +77,8 @@ public function getNodeTypes(): array */ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Stmt\Function_ { - $classMethodStmts = $node->stmts; - if ($classMethodStmts === null || $classMethodStmts === []) { + $stmts = $node->stmts; + if ($stmts === null || $stmts === []) { return null; } @@ -91,7 +91,7 @@ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|St return null; } - $assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($classMethodStmts); + $assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($stmts); $hasChanged = false; @@ -101,7 +101,7 @@ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|St } /** @var Expression $currentStmt */ - $currentStmt = $classMethodStmts[$stmtPosition]; + $currentStmt = $stmts[$stmtPosition]; /** @var Assign $assign */ $assign = $currentStmt->expr;