diff --git a/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php b/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php new file mode 100644 index 00000000000..3c688a7af3b --- /dev/null +++ b/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php @@ -0,0 +1,107 @@ +rule(SomeRector::class); +}; +CODE_SAMPLE +, <<<'CODE_SAMPLE' +return RectorConfig::configure()->rules([SomeRector::class]); +CODE_SAMPLE +)]); + } + /** + * @return array> + */ + public function getNodeTypes() : array + { + return [FileWithoutNamespace::class]; + } + /** + * @param FileWithoutNamespace $node + */ + public function refactor(Node $node) : ?Node + { + $hasChanged = \false; + foreach ($node->stmts as $stmt) { + if (!$stmt instanceof Return_) { + continue; + } + if (!$stmt->expr instanceof Closure) { + continue; + } + if (\count($stmt->expr->params) !== 1) { + continue; + } + $param = $stmt->expr->params[0]; + if (!$param->type instanceof FullyQualified) { + continue; + } + if ($param->type->toString() !== 'Rector\\Config\\RectorConfig') { + continue; + } + $stmts = $stmt->expr->stmts; + if ($stmts === []) { + throw new ShouldNotHappenException('RectorConfig must have at least 1 stmts'); + } + $newExpr = new StaticCall(new FullyQualified('Rector\\Config\\RectorConfig'), 'configure'); + $rules = new Array_(); + foreach ($stmts as $rectorConfigStmt) { + // complex stmts should be skipped, eg: with if else + if (!$rectorConfigStmt instanceof Expression) { + return null; + } + // debugging or variable init? skip + if (!$rectorConfigStmt->expr instanceof MethodCall) { + return null; + } + // another method call? skip + if (!$this->isObjectType($rectorConfigStmt->expr->var, new ObjectType('Rector\\Config\\RectorConfig'))) { + return null; + } + if ($rectorConfigStmt->expr->isFirstClassCallable()) { + return null; + } + if ($this->isName($rectorConfigStmt->expr->name, 'rule')) { + $rules->items[] = new ArrayItem($rectorConfigStmt->expr->getArgs()[0]->value); + } else { + // implementing method by method + return null; + } + } + if ($rules->items !== []) { + $stmt->expr = $this->nodeFactory->createMethodCall($newExpr, 'withRules', [$rules]); + $hasChanged = \true; + } + } + if ($hasChanged) { + return $node; + } + return null; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index e2dd235ae3d..e002d338fe0 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'b85682bedf3d32c32bea9f1fef01d983f3ae89cf'; + public const PACKAGE_VERSION = '081cf7f7e508428f259b83125883178632e350dc'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-02-04 00:26:46'; + public const RELEASE_DATE = '2024-02-04 12:03:25'; /** * @var int */ diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index b054f720f65..e735e4e8e5f 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2262,6 +2262,7 @@ return array( 'Rector\\Transform\\Rector\\Class_\\AddInterfaceByTraitRector' => $baseDir . '/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php', 'Rector\\Transform\\Rector\\Class_\\MergeInterfacesRector' => $baseDir . '/rules/Transform/Rector/Class_/MergeInterfacesRector.php', 'Rector\\Transform\\Rector\\Class_\\ParentClassToTraitsRector' => $baseDir . '/rules/Transform/Rector/Class_/ParentClassToTraitsRector.php', + 'Rector\\Transform\\Rector\\FileWithoutNamespace\\RectorConfigBuilderRector' => $baseDir . '/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php', 'Rector\\Transform\\Rector\\FuncCall\\FuncCallToConstFetchRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php', 'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php', 'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 455bf0f0409..d2647ee431b 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -2476,6 +2476,7 @@ class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3 'Rector\\Transform\\Rector\\Class_\\AddInterfaceByTraitRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php', 'Rector\\Transform\\Rector\\Class_\\MergeInterfacesRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/MergeInterfacesRector.php', 'Rector\\Transform\\Rector\\Class_\\ParentClassToTraitsRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/ParentClassToTraitsRector.php', + 'Rector\\Transform\\Rector\\FileWithoutNamespace\\RectorConfigBuilderRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php', 'Rector\\Transform\\Rector\\FuncCall\\FuncCallToConstFetchRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php', 'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php', 'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',