Skip to content

Commit

Permalink
[DeadCode] Add RemoveJustPropertyFetchForAssignRector (#2423)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 3, 2022
1 parent 0df2351 commit c5f35e4
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 2 deletions.
27 changes: 25 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 516 Rules Overview
# 517 Rules Overview

<br>

Expand All @@ -14,7 +14,7 @@

- [Composer](#composer) (6)

- [DeadCode](#deadcode) (48)
- [DeadCode](#deadcode) (49)

- [DependencyInjection](#dependencyinjection) (2)

Expand Down Expand Up @@ -3230,6 +3230,29 @@ Remove empty method call

<br>

### RemoveJustPropertyFetchForAssignRector

Remove assign of property, just for value assign

- class: [`Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector`](../rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchForAssignRector.php)

```diff
class SomeClass
{
private $items = [];

public function run()
{
- $items = $this->items;
- $items[] = 1000;
- $this->items = $items ;
+ $this->items[] = 1000;
}
}
```

<br>

### RemoveLastReturnRector

Remove very last `return` that has no meaning
Expand Down
2 changes: 2 additions & 0 deletions config/set/dead-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use Rector\DeadCode\Rector\Return_\RemoveDeadConditionAboveReturnRector;
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector;
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
use Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector;
use Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector;
Expand Down Expand Up @@ -97,5 +98,6 @@
RemoveNonExistingVarAnnotationRector::class,
RemoveUnusedPromotedPropertyRector::class,
RemoveLastReturnRector::class,
RemoveJustPropertyFetchForAssignRector::class,
]);
};
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,8 @@ parameters:
-
message: '#Method "refactor(Params|Return)\(\)" returns bool type, so the name should start with is/has/was#'
path: rules/Php80/Rector/Class_/ConstantListClassToEnumRector.php

# known values
-
message: '#Offset \d+ does not exist on array<PhpParser\\Node\\Stmt>\|null#'
path: rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchForAssignRector.php
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 ;
}
}
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;
}
}
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 ;
}
}
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;
}
}

?>
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';
}
}
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 rules/DeadCode/NodeAnalyzer/JustPropertyFetchVariableAssignMatcher.php
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);
}
}

0 comments on commit c5f35e4

Please sign in to comment.