Skip to content

Commit

Permalink
[Transform] Transform skip() to withSkip() on RectorConfigBuilderRect…
Browse files Browse the repository at this point in the history
…or (#5575)
  • Loading branch information
samsonasik committed Feb 7, 2024
1 parent 5a5ea60 commit 6679831
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
@@ -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->skip([
__DIR__ . '/src/migrations',
]);
$rectorConfig->rule(ReturnUnionTypeRector::class);
};

?>
-----
<?php

declare(strict_types=1);

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

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

?>
Expand Up @@ -88,6 +88,7 @@ public function refactor(Node $node): ?Node

$rules = new Array_();
$paths = new Array_();
$skips = new Array_();
foreach ($stmts as $rectorConfigStmt) {
// complex stmts should be skipped, eg: with if else
if (! $rectorConfigStmt instanceof Expression) {
Expand Down Expand Up @@ -116,6 +117,9 @@ public function refactor(Node $node): ?Node
} elseif ($this->isName($rectorConfigStmt->expr->name, 'paths')) {
Assert::isAOf($rectorConfigStmt->expr->getArgs()[0]->value, Array_::class);
$paths = $rectorConfigStmt->expr->getArgs()[0]->value;
} elseif ($this->isName($rectorConfigStmt->expr->name, 'skip')) {
Assert::isAOf($rectorConfigStmt->expr->getArgs()[0]->value, Array_::class);
$skips = $rectorConfigStmt->expr->getArgs()[0]->value;
} else {
// implementing method by method
return null;
Expand All @@ -127,6 +131,11 @@ public function refactor(Node $node): ?Node
$hasChanged = true;
}

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

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

0 comments on commit 6679831

Please sign in to comment.