Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode]: Support functions in RemoveUnusedVariableAssignRector #5249

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

function unusedInFunction()
{
$value = 5;
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

function unusedInFunction()
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

function usedInFunction()
{
$value = 5;

if (rand(0,1)) {
$value += 10;
}
}

?>
32 changes: 16 additions & 16 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ public function run()
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [ClassMethod::class, Stmt\Function_::class];
}

/**
* @param ClassMethod $node
* @param ClassMethod|Stmt\Function_ $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) {
$stmts = $node->stmts;
if ($stmts === null || $stmts === []) {
return null;
}

Expand All @@ -91,7 +91,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?ClassMethod
return null;
}

$assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($classMethodStmts);
$assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($stmts);

$hasChanged = false;

Expand All @@ -101,7 +101,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?ClassMethod
}

/** @var Expression<Assign> $currentStmt */
$currentStmt = $classMethodStmts[$stmtPosition];
$currentStmt = $stmts[$stmtPosition];

/** @var Assign $assign */
$assign = $currentStmt->expr;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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]);
}

/**
Expand Down