-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add RemoveJustVariableAssignRector
- Loading branch information
1 parent
0660b21
commit cd15d1f
Showing
9 changed files
with
595 additions
and
466 deletions.
There are no files selected for viewing
791 changes: 340 additions & 451 deletions
791
build/target-repository/docs/rector_rules_overview.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...mtsAwareInterface/RemoveJustVariableAssignRector/Fixture/skip_referenced_variable.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\Fixture; | ||
|
||
final class SkipReferencedVariable | ||
{ | ||
private $temporaryValue; | ||
|
||
public function run(&$result) | ||
{ | ||
$result = 100; | ||
|
||
$this->temporaryValue = $result; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...Code/Rector/StmtsAwareInterface/RemoveJustVariableAssignRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
private int $temporaryValue; | ||
|
||
public function run() | ||
{ | ||
$result = 100; | ||
|
||
$this->temporaryValue = $result; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
private int $temporaryValue; | ||
|
||
public function run() | ||
{ | ||
$this->temporaryValue = 100; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...StmtsAwareInterface/RemoveJustVariableAssignRector/RemoveJustVariableAssignRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class RemoveJustVariableAssignRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...Code/Rector/StmtsAwareInterface/RemoveJustVariableAssignRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveJustVariableAssignRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustVariableAssignRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Rector\StmtsAwareInterface; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt\Expression; | ||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; | ||
use Rector\Core\NodeAnalyzer\VariableAnalyzer; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\RemoveJustVariableAssignRectorTest | ||
*/ | ||
final class RemoveJustVariableAssignRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private VariableAnalyzer $variableAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove variable just to assign value or return value', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$result = 100; | ||
$this->temporaryValue = $result; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$this->temporaryValue = 100; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [StmtsAwareInterface::class]; | ||
} | ||
|
||
/** | ||
* @param StmtsAwareInterface $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$stmts = (array) $node->stmts; | ||
if ($stmts === []) { | ||
return null; | ||
} | ||
|
||
$originalStmts = $stmts; | ||
|
||
foreach ($stmts as $key => $stmt) { | ||
$nextStmt = $stmts[$key + 1] ?? null; | ||
|
||
if (! $stmt instanceof Expression) { | ||
continue; | ||
} | ||
|
||
if (! $stmt->expr instanceof Assign) { | ||
continue; | ||
} | ||
|
||
$currentAssign = $stmt->expr; | ||
|
||
if (! $nextStmt instanceof Expression) { | ||
continue; | ||
} | ||
|
||
if (! $nextStmt->expr instanceof Assign) { | ||
continue; | ||
} | ||
|
||
$nextAssign = $nextStmt->expr; | ||
|
||
if (! $this->areTwoVariablesCrossAssign($currentAssign, $nextAssign)) { | ||
continue; | ||
} | ||
|
||
// ... | ||
$currentAssign->var = $nextAssign->var; | ||
unset($stmts[$key + 1]); | ||
} | ||
|
||
if ($originalStmts === $stmts) { | ||
return null; | ||
} | ||
|
||
$node->stmts = $stmts; | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* This detects if two variables are cross assigned: | ||
* | ||
* $<some> = 1000; | ||
* $this->value = $<some>; | ||
*/ | ||
private function areTwoVariablesCrossAssign(Assign $currentAssign, Assign $nextAssign): bool | ||
{ | ||
// is just re-assign to variable | ||
if (! $currentAssign->var instanceof Variable) { | ||
return false; | ||
} | ||
|
||
if (! $nextAssign->expr instanceof Variable) { | ||
return false; | ||
} | ||
|
||
if (! $this->nodeComparator->areNodesEqual($currentAssign->var, $nextAssign->expr)) { | ||
return false; | ||
} | ||
|
||
if ($this->variableAnalyzer->isUsedByReference($currentAssign->var)) { | ||
return false; | ||
} | ||
|
||
return ! $this->variableAnalyzer->isUsedByReference($nextAssign->expr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters