Skip to content
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,31 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Closure\RemoveUnusedClosureVariableUseRector\Fixture;

class Fixture
{
public function run($value)
{
return function () use ($value) {
return 'value';
};
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Closure\RemoveUnusedClosureVariableUseRector\Fixture;

class Fixture
{
public function run($value)
{
return function () {
return 'value';
};
}
}

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

namespace Rector\Tests\DeadCode\Rector\Closure\RemoveUnusedClosureVariableUseRector\Fixture;

final class SkipUsedVariable
{
public function run($value)
{
return function () use ($value) {
$result = $value + 100;

return $result;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\Closure\RemoveUnusedClosureVariableUseRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveUnusedClosureVariableUseRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Concat\RemoveUnusedClosureVariableUseRector;

return RectorConfig::configure()
->withRules([RemoveUnusedClosureVariableUseRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\Concat;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should under Closure namespace:

-namespace Rector\DeadCode\Rector\Concat;
+namespace Rector\DeadCode\Rector\Closure;

I will create new PR for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\Closure\RemoveUnusedClosureVariableUseRector\RemoveUnusedClosureVariableUseRectorTest
*/
final class RemoveUnusedClosureVariableUseRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove unused variable in use() of closure', [
new CodeSample(
<<<'CODE_SAMPLE'
$var = 1;

$closure = function() use ($var) {
echo 'Hello World';
};

CODE_SAMPLE

,
<<<'CODE_SAMPLE'
$var = 1;
$closure = function() {
echo 'Hello World';
};

CODE_SAMPLE
),

]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Closure::class];
}

/**
* @param Closure $node
*/
public function refactor(Node $node): ?Node
{
if ($node->uses === []) {
return null;
}

$hasChanged = false;

foreach ($node->uses as $key => $useVariable) {
$useVariableName = $this->getName($useVariable->var);
if (! is_string($useVariableName)) {
continue;
}

$isUseUsed = (bool) $this->betterNodeFinder->findVariableOfName($node->stmts, $useVariableName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should verify compact use as well, better use ExprUsedInNodeAnalyzer for it, I will create new PR for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($isUseUsed) {
continue;
}

unset($node->uses[$key]);
$hasChanged = true;

}

if ($hasChanged) {
// reset keys, to keep as expected
$node->uses = array_values($node->uses);
Comment on lines +86 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed, already covered at NodeAttributeReIndexer, see

if ($node instanceof Closure) {
$node->uses = array_values($node->uses);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $node;
}

return null;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/DeadCodeLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
use Rector\DeadCode\Rector\Concat\RemoveUnusedClosureVariableUseRector;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector;
use Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector;
Expand Down Expand Up @@ -115,6 +116,7 @@ final class DeadCodeLevel
ReduceAlwaysFalseIfOrRector::class,
RemoveUnusedPrivateClassConstantRector::class,
RemoveUnusedPrivatePropertyRector::class,
RemoveUnusedClosureVariableUseRector::class,

RemoveDuplicatedCaseInSwitchRector::class,
RemoveDeadInstanceOfRector::class,
Expand Down