Skip to content

Commit

Permalink
Fix RemoveDoubleAssignRector in case of method call (#6442)
Browse files Browse the repository at this point in the history
* add test for double assign removal

* Fix RemoveDoubleAssignRector in case of method call
  • Loading branch information
TomasVotruba authored Nov 16, 2024
1 parent 9cd6376 commit e7800e9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
26 changes: 9 additions & 17 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -54,19 +49,11 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [
Foreach_::class,
FileWithoutNamespace::class,
ClassMethod::class,
Function_::class,
Closure::class,
If_::class,
Namespace_::class,
];
return [StmtsAwareInterface::class];
}

/**
* @param Foreach_|FileWithoutNamespace|If_|Namespace_|ClassMethod|Function_|Closure $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?Node
{
Expand Down Expand Up @@ -116,6 +103,11 @@ public function refactor(Node $node): ?Node
continue;
}

// next stmts can have side effect as well
if ($nextAssign->expr instanceof MethodCall) {
continue;
}

if (! $stmt->expr->var instanceof Variable && ! $stmt->expr->var instanceof PropertyFetch && ! $stmt->expr->var instanceof StaticPropertyFetch) {
continue;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Issues/KeepDoubleAssignParam/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Tests\Issues\KeepDoubleAssignParam\Fixture;

final class Fixture
{
private $items;

public function __construct($input)
{
if (! \is_array($input)) {
$this->items = [$input];
} else {
$this->items = $input;
}

$this->items = $this->getItems();
}

public function getItems()
{
return sort($this->items);
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\KeepDoubleAssignParam\Fixture;

final class Fixture
{
private $items;

public function __construct($input)
{
$this->items = ! \is_array($input) ? [$input] : $input;

$this->items = $this->getItems();
}

public function getItems()
{
return sort($this->items);
}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/KeepDoubleAssignParam/KeepDoubleAssignParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\KeepDoubleAssignParam;

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

final class KeepDoubleAssignParamTest 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';
}
}
10 changes: 10 additions & 0 deletions tests/Issues/KeepDoubleAssignParam/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\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;

return RectorConfig::configure()
->withRules([RemoveDoubleAssignRector::class, SimplifyIfElseToTernaryRector::class]);

0 comments on commit e7800e9

Please sign in to comment.