Skip to content

Commit

Permalink
[Rector] Allow transform sets() to withSets() on RectorConfigBuilderR…
Browse files Browse the repository at this point in the history
…ector (#5638)
  • Loading branch information
samsonasik committed Feb 19, 2024
1 parent d5c3fc7 commit f107b4c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SetList::DEAD_CODE]);
};

?>
-----
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return \Rector\Config\RectorConfig::configure()->withSets([SetList::DEAD_CODE]);

?>
Expand Up @@ -91,6 +91,7 @@ public function refactor(Node $node): ?Node
$skips = new Array_();
$autoloadPaths = new Array_();
$bootstrapFiles = new Array_();
$sets = new Array_();

foreach ($stmts as $rectorConfigStmt) {
// complex stmts should be skipped, eg: with if else
Expand Down Expand Up @@ -139,6 +140,9 @@ public function refactor(Node $node): ?Node
} elseif ($name === 'ruleWithConfiguration') {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withConfiguredRule', [$value, $args[1]->value]);
$hasChanged = true;
} elseif ($name === 'sets') {
Assert::isAOf($value, Array_::class);
$sets->items = array_merge($sets->items, $value->items);
} else {
// implementing method by method
return null;
Expand Down Expand Up @@ -170,6 +174,11 @@ public function refactor(Node $node): ?Node
$hasChanged = true;
}

if ($sets->items !== []) {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withSets', [$sets]);
$hasChanged = true;
}

if ($hasChanged) {
$stmt->expr = $newExpr;
}
Expand Down

0 comments on commit f107b4c

Please sign in to comment.