Skip to content

Commit

Permalink
[Transform] Transform paths() to withPaths() on RectorConfigBuilderRe…
Browse files Browse the repository at this point in the history
…ctor (#5574)

* [Transform] Transform paths() to withPaths() on RectorConfigBuilderRector

* [Transform] Transform paths() to withPaths() on RectorConfigBuilderRector
  • Loading branch information
samsonasik committed Feb 7, 2024
1 parent bd37496 commit 5a5ea60
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
]);
$rectorConfig->rule(ReturnUnionTypeRector::class);
};

?>
-----
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;

return \Rector\Config\RectorConfig::configure()->withPaths([
__DIR__ . '/src',
])->withRules([ReturnUnionTypeRector::class]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function refactor(Node $node): ?Node
$newExpr = new StaticCall(new FullyQualified('Rector\Config\RectorConfig'), 'configure');

$rules = new Array_();
$paths = new Array_();
foreach ($stmts as $rectorConfigStmt) {
// complex stmts should be skipped, eg: with if else
if (! $rectorConfigStmt instanceof Expression) {
Expand All @@ -112,16 +113,28 @@ public function refactor(Node $node): ?Node
} elseif ($this->isName($rectorConfigStmt->expr->name, 'rules')) {
Assert::isAOf($rectorConfigStmt->expr->getArgs()[0]->value, Array_::class);
$rules->items = array_merge($rules->items, $rectorConfigStmt->expr->getArgs()[0]->value->items);
} elseif ($this->isName($rectorConfigStmt->expr->name, 'paths')) {
Assert::isAOf($rectorConfigStmt->expr->getArgs()[0]->value, Array_::class);
$paths = $rectorConfigStmt->expr->getArgs()[0]->value;
} else {
// implementing method by method
return null;
}
}

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

if ($rules->items !== []) {
$stmt->expr = $this->nodeFactory->createMethodCall($newExpr, 'withRules', [$rules]);
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withRules', [$rules]);
$hasChanged = true;
}

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

if ($hasChanged) {
Expand Down

0 comments on commit 5a5ea60

Please sign in to comment.