-
-
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 RemoveJustPropertyFetchForAssignRector (#2423)
- Loading branch information
1 parent
0df2351
commit c5f35e4
Showing
12 changed files
with
438 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
17 changes: 17 additions & 0 deletions
17
...face/RemoveJustPropertyFetchForAssignRector/Fixture/skip_different_property_fetch.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,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector\Fixture; | ||
|
||
final class SkipDifferentPropertyFetch | ||
{ | ||
private $items = []; | ||
|
||
private $another = []; | ||
|
||
public function run() | ||
{ | ||
$items = $this->another; | ||
$items[] = 1000; | ||
$this->items = $items ; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...eInterface/RemoveJustPropertyFetchForAssignRector/Fixture/skip_different_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\RemoveJustPropertyFetchForAssignRector\Fixture; | ||
|
||
final class SkipDifferentVariable | ||
{ | ||
private $items = []; | ||
|
||
public function run() | ||
{ | ||
$items = $this->items; | ||
$items[] = 1000; | ||
$this->items = $items2; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...tmtsAwareInterface/RemoveJustPropertyFetchForAssignRector/Fixture/skip_four_stmts.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector\Fixture; | ||
|
||
final class SkipFourStmts | ||
{ | ||
private $items = []; | ||
|
||
public function run() | ||
{ | ||
$items = $this->items; | ||
$items[] = 1000; | ||
$items[] = 1000; | ||
$this->items = $items ; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...tor/StmtsAwareInterface/RemoveJustPropertyFetchForAssignRector/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\RemoveJustPropertyFetchForAssignRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
private $items = []; | ||
|
||
public function run() | ||
{ | ||
$items = $this->items; | ||
$items[] = 1000; | ||
$this->items = $items ; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
private $items = []; | ||
|
||
public function run() | ||
{ | ||
$this->items[] = 1000; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ace/RemoveJustPropertyFetchForAssignRector/RemoveJustPropertyFetchForAssignRectorTest.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\RemoveJustPropertyFetchForAssignRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class RemoveJustPropertyFetchForAssignRectorTest 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
...tor/StmtsAwareInterface/RemoveJustPropertyFetchForAssignRector/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\RemoveJustPropertyFetchForAssignRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveJustPropertyFetchForAssignRector::class); | ||
}; |
104 changes: 104 additions & 0 deletions
104
rules/DeadCode/NodeAnalyzer/JustPropertyFetchVariableAssignMatcher.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,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Expression; | ||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; | ||
use Rector\Core\PhpParser\Comparing\NodeComparator; | ||
use Rector\DeadCode\ValueObject\VariableAndPropertyFetchAssign; | ||
|
||
final class JustPropertyFetchVariableAssignMatcher | ||
{ | ||
public function __construct( | ||
private readonly NodeComparator $nodeComparator | ||
) { | ||
} | ||
|
||
public function match(StmtsAwareInterface $stmtsAware): ?VariableAndPropertyFetchAssign | ||
{ | ||
$stmts = (array) $stmtsAware->stmts; | ||
|
||
$stmtCount = count($stmts); | ||
|
||
// must be exactly 3 stmts | ||
if ($stmtCount !== 3) { | ||
return null; | ||
} | ||
|
||
$firstVariableAndPropertyFetchAssign = $this->matchVariableAndPropertyFetchAssign($stmts[0]); | ||
if (! $firstVariableAndPropertyFetchAssign instanceof VariableAndPropertyFetchAssign) { | ||
return null; | ||
} | ||
|
||
$thirdVariableAndPropertyFetchAssign = $this->matchRevertedVariableAndPropertyFetchAssign($stmts[2]); | ||
if (! $thirdVariableAndPropertyFetchAssign instanceof VariableAndPropertyFetchAssign) { | ||
return null; | ||
} | ||
|
||
if (! $this->nodeComparator->areNodesEqual( | ||
$firstVariableAndPropertyFetchAssign->getPropertyFetch(), | ||
$thirdVariableAndPropertyFetchAssign->getPropertyFetch() | ||
)) { | ||
return null; | ||
} | ||
|
||
if (! $this->nodeComparator->areNodesEqual( | ||
$firstVariableAndPropertyFetchAssign->getVariable(), | ||
$thirdVariableAndPropertyFetchAssign->getVariable() | ||
)) { | ||
return null; | ||
} | ||
|
||
return $firstVariableAndPropertyFetchAssign; | ||
} | ||
|
||
private function matchVariableAndPropertyFetchAssign(Stmt $stmt): ?VariableAndPropertyFetchAssign | ||
{ | ||
if (! $stmt instanceof Expression) { | ||
return null; | ||
} | ||
|
||
if (! $stmt->expr instanceof Assign) { | ||
return null; | ||
} | ||
|
||
$assign = $stmt->expr; | ||
if (! $assign->expr instanceof PropertyFetch) { | ||
return null; | ||
} | ||
|
||
if (! $assign->var instanceof Variable) { | ||
return null; | ||
} | ||
|
||
return new VariableAndPropertyFetchAssign($assign->var, $assign->expr); | ||
} | ||
|
||
private function matchRevertedVariableAndPropertyFetchAssign(Stmt $stmt): ?VariableAndPropertyFetchAssign | ||
{ | ||
if (! $stmt instanceof Expression) { | ||
return null; | ||
} | ||
|
||
if (! $stmt->expr instanceof Assign) { | ||
return null; | ||
} | ||
|
||
$assign = $stmt->expr; | ||
if (! $assign->var instanceof PropertyFetch) { | ||
return null; | ||
} | ||
|
||
if (! $assign->expr instanceof Variable) { | ||
return null; | ||
} | ||
|
||
return new VariableAndPropertyFetchAssign($assign->expr, $assign->var); | ||
} | ||
} |
Oops, something went wrong.