diff --git a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php index 282a7e00e7c..def30b18b01 100644 --- a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php @@ -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; @@ -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 { @@ -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; } diff --git a/tests/Issues/KeepDoubleAssignParam/Fixture/fixture.php.inc b/tests/Issues/KeepDoubleAssignParam/Fixture/fixture.php.inc index e782968637a..1e994546ab5 100644 --- a/tests/Issues/KeepDoubleAssignParam/Fixture/fixture.php.inc +++ b/tests/Issues/KeepDoubleAssignParam/Fixture/fixture.php.inc @@ -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(); } diff --git a/tests/Issues/KeepDoubleAssignParam/config/configured_rule.php b/tests/Issues/KeepDoubleAssignParam/config/configured_rule.php index 1ae93deb1a6..0c0f164e715 100644 --- a/tests/Issues/KeepDoubleAssignParam/config/configured_rule.php +++ b/tests/Issues/KeepDoubleAssignParam/config/configured_rule.php @@ -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]);