Skip to content

Commit

Permalink
[Transform] Allow transform autoloadPaths() to withAutoloadPaths() on…
Browse files Browse the repository at this point in the history
… RectorConfigBuilderRector (#5630)
  • Loading branch information
samsonasik committed Feb 18, 2024
1 parent a533e70 commit b621800
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->autoloadPaths([
__DIR__ . '/../autoload.php',
]);
};

?>
-----
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return \Rector\Config\RectorConfig::configure()->withAutoloadPaths([
__DIR__ . '/../autoload.php',
]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public function refactor(Node $node): ?Node
$rules = new Array_();
$paths = new Array_();
$skips = new Array_();
$autoloadPaths = new Array_();

foreach ($stmts as $rectorConfigStmt) {
// complex stmts should be skipped, eg: with if else
if (! $rectorConfigStmt instanceof Expression) {
Expand All @@ -115,7 +117,7 @@ public function refactor(Node $node): ?Node

if ($name === 'rule') {
Assert::isAOf($rules, Array_::class);
$rules->items[] = new ArrayItem($rectorConfigStmt->expr->getArgs()[0]->value);
$rules->items[] = new ArrayItem($value);
} elseif (($name === 'rules')) {
if ($value instanceof Array_) {
Assert::isAOf($rules, Array_::class);
Expand All @@ -127,6 +129,9 @@ public function refactor(Node $node): ?Node
$paths = $value;
} elseif ($name === 'skip') {
$skips = $value;
} elseif ($name === 'autoloadPaths') {
Assert::isAOf($value, Array_::class);
$autoloadPaths = $value;
} else {
// implementing method by method
return null;
Expand All @@ -148,6 +153,11 @@ public function refactor(Node $node): ?Node
$hasChanged = true;
}

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

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

0 comments on commit b621800

Please sign in to comment.