Skip to content

Commit

Permalink
Fix RemoveDoubleAssignRector in case of method call
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 16, 2024
1 parent 5b990b9 commit 3b4eb07
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 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
2 changes: 1 addition & 1 deletion tests/Issues/KeepDoubleAssignParam/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Fixture

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

$this->items = $this->getItems();
}
Expand Down
8 changes: 3 additions & 5 deletions tests/Issues/KeepDoubleAssignParam/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;

return RectorConfig::configure()
->withRules([
\Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector::class,
\Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector::class,
\Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector::class,
]);
->withRules([RemoveDoubleAssignRector::class, SimplifyIfElseToTernaryRector::class]);

0 comments on commit 3b4eb07

Please sign in to comment.