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,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;

$validator = $validator = createValidator();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe worth adding support for $validator = $another = $validator = createValidator();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Care to PR?


?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;

$validator = createValidator();

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

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;

$first = $second = createValidator();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector;

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

final class RemoveDoubleSelfAssignRectorTest 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\Assign\RemoveDoubleSelfAssignRector;

return RectorConfig::configure()
->withRules([RemoveDoubleSelfAssignRector::class]);
61 changes: 61 additions & 0 deletions rules/DeadCode/Rector/Assign/RemoveDoubleSelfAssignRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\RemoveDoubleSelfAssignRectorTest
*/
final class RemoveDoubleSelfAssignRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove duplicated self assign of the same variable', [
new CodeSample(
'$validator = $validator = Validation::createValidator();',
'$validator = Validation::createValidator();'
),
]);
}

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

/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->var instanceof Variable) {
return null;
}

if (! $node->expr instanceof Assign) {
return null;
}

$innerAssign = $node->expr;
if (! $innerAssign->var instanceof Variable) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($node->var, $innerAssign->var)) {
return null;
}

return $innerAssign;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/DeadCodeLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\Contract\Rector\RectorInterface;
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
Expand Down Expand Up @@ -109,6 +110,7 @@ final class DeadCodeLevel
TernaryToBooleanOrFalseToBooleanAndRector::class,
RemoveUselessTernaryRector::class,
RemoveDoubleAssignRector::class,
RemoveDoubleSelfAssignRector::class,
RemoveUselessAssignFromPropertyPromotionRector::class,
RemoveConcatAutocastRector::class,
SimplifyIfElseWithSameContentRector::class,
Expand Down
Loading