mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 19:37:55 +01:00
[DeadCode] Add RemoveUnusedVariableAssignRector
This commit is contained in:
parent
db5a881b3b
commit
f7aefc68b8
@ -253,9 +253,6 @@
|
|||||||
"rector-ci": "bin/rector process --config rector-ci.yaml --dry-run --ansi --no-progress-bar",
|
"rector-ci": "bin/rector process --config rector-ci.yaml --dry-run --ansi --no-progress-bar",
|
||||||
"rector": "bin/rector process --config rector-ci.yaml --ansi"
|
"rector": "bin/rector process --config rector-ci.yaml --ansi"
|
||||||
},
|
},
|
||||||
"scripts-descriptions": {
|
|
||||||
"docs": "Regenerate descriptions of all Rectors to docs/AllRectorsOverview.md file"
|
|
||||||
},
|
|
||||||
"config": {
|
"config": {
|
||||||
"sort-packages": true
|
"sort-packages": true
|
||||||
},
|
},
|
||||||
|
@ -33,4 +33,4 @@ services:
|
|||||||
Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector: null
|
Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector: null
|
||||||
Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector: null
|
Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector: null
|
||||||
Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector: null
|
Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector: null
|
||||||
|
Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector: null
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# All 461 Rectors Overview
|
# All 462 Rectors Overview
|
||||||
|
|
||||||
- [Projects](#projects)
|
- [Projects](#projects)
|
||||||
- [General](#general)
|
- [General](#general)
|
||||||
@ -3115,6 +3115,25 @@ Remove unused private properties
|
|||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
### `RemoveUnusedVariableAssignRector`
|
||||||
|
|
||||||
|
- class: [`Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector`](/../master/rules/dead-code/src/Rector/Assign/RemoveUnusedVariableAssignRector.php)
|
||||||
|
- [test fixtures](/../master/rules/dead-code/tests/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture)
|
||||||
|
|
||||||
|
Remove unused assigns to variables
|
||||||
|
|
||||||
|
```diff
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
- $value = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
### `SimplifyIfElseWithSameContentRector`
|
### `SimplifyIfElseWithSameContentRector`
|
||||||
|
|
||||||
- class: [`Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector`](/../master/rules/dead-code/src/Rector/If_/SimplifyIfElseWithSameContentRector.php)
|
- class: [`Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector`](/../master/rules/dead-code/src/Rector/If_/SimplifyIfElseWithSameContentRector.php)
|
||||||
|
@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Rector\Assign;
|
||||||
|
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Expr\Assign;
|
||||||
|
use PhpParser\Node\Expr\Variable;
|
||||||
|
use PhpParser\Node\FunctionLike;
|
||||||
|
use Rector\Core\Rector\AbstractRector;
|
||||||
|
use Rector\Core\RectorDefinition\CodeSample;
|
||||||
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||||
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see \Rector\DeadCode\Tests\Rector\Assign\RemoveUnusedVariableAssignRector\RemoveUnusedVariableAssignRectorTest
|
||||||
|
*/
|
||||||
|
final class RemoveUnusedVariableAssignRector extends AbstractRector
|
||||||
|
{
|
||||||
|
public function getDefinition(): RectorDefinition
|
||||||
|
{
|
||||||
|
return new RectorDefinition('Remove unused assigns to variables', [
|
||||||
|
new CodeSample(
|
||||||
|
<<<'PHP'
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$value = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PHP
|
||||||
|
,
|
||||||
|
<<<'PHP'
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PHP
|
||||||
|
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function getNodeTypes(): array
|
||||||
|
{
|
||||||
|
return [Assign::class];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Assign $node
|
||||||
|
*/
|
||||||
|
public function refactor(Node $node): ?Node
|
||||||
|
{
|
||||||
|
$functionLike = $node->getAttribute(AttributeKey::METHOD_NODE);
|
||||||
|
if (! $functionLike instanceof FunctionLike) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $node->var instanceof Variable) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// variable is used
|
||||||
|
$variableUsages = $this->findVariableUsages($functionLike, $node);
|
||||||
|
if (count($variableUsages) > 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->removeNode($node);
|
||||||
|
|
||||||
|
return $node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Variable[]
|
||||||
|
*/
|
||||||
|
private function findVariableUsages(FunctionLike $functionLike, Assign $assign): array
|
||||||
|
{
|
||||||
|
return $this->betterNodeFinder->find((array) $functionLike->getStmts(), function (Node $node) use ($assign) {
|
||||||
|
if (! $node instanceof Variable) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip assign value
|
||||||
|
return $assign->var !== $node;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
|
||||||
|
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$value = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
|
||||||
|
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
|
||||||
|
|
||||||
|
class SkipUsed
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$value = 5;
|
||||||
|
|
||||||
|
if (rand(0, 10)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveUnusedVariableAssignRector;
|
||||||
|
|
||||||
|
use Iterator;
|
||||||
|
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||||
|
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
|
||||||
|
|
||||||
|
final class RemoveUnusedVariableAssignRectorTest extends AbstractRectorTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider provideData()
|
||||||
|
*/
|
||||||
|
public function test(string $file): void
|
||||||
|
{
|
||||||
|
$this->doTestFile($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideData(): Iterator
|
||||||
|
{
|
||||||
|
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRectorClass(): string
|
||||||
|
{
|
||||||
|
return RemoveUnusedVariableAssignRector::class;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user