From 343646abca618ea70d90af5de00073ec5a8176d0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 25 Nov 2026 17:41:57 +0100 Subject: [PATCH] Add dump-config command to print the resolved configuration as JSON Prints paths, PHP version, registered rules with their configuration and skips, so external runners can reuse rector.php. --- src/Configuration/ConfigDumper.php | 188 ++++++++++++++++++ src/Console/Command/DumpConfigCommand.php | 37 ++++ .../LazyContainerFactory.php | 2 + tests/Configuration/ConfigDumperTest.php | 111 +++++++++++ 4 files changed, 338 insertions(+) create mode 100644 src/Configuration/ConfigDumper.php create mode 100644 src/Console/Command/DumpConfigCommand.php create mode 100644 tests/Configuration/ConfigDumperTest.php diff --git a/src/Configuration/ConfigDumper.php b/src/Configuration/ConfigDumper.php new file mode 100644 index 00000000000..0ff2963b385 --- /dev/null +++ b/src/Configuration/ConfigDumper.php @@ -0,0 +1,188 @@ +, + * rules: array, + * import_names: bool, + * import_short_classes: bool, + * remove_unused_imports: bool + * } + */ + public function dump(): array + { + $ruleClasses = array_values(array_unique( + SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES) + )); + + return [ + 'php_version' => SimpleParameterProvider::hasParameter(Option::PHP_VERSION_FEATURES) + ? SimpleParameterProvider::provideIntParameter(Option::PHP_VERSION_FEATURES) + : null, + 'paths' => array_values(SimpleParameterProvider::provideArrayParameter(Option::PATHS)), + 'skips' => $this->dumpSkips($ruleClasses), + 'rules' => $this->dumpRules($ruleClasses), + 'import_names' => SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES, false), + 'import_short_classes' => SimpleParameterProvider::provideBoolParameter( + Option::IMPORT_SHORT_CLASSES, + true + ), + 'remove_unused_imports' => SimpleParameterProvider::provideBoolParameter( + Option::REMOVE_UNUSED_IMPORTS, + false + ), + ]; + } + + /** + * @param string[] $ruleClasses + * @return array + */ + private function dumpRules(array $ruleClasses): array + { + $ruleConfigurations = SimpleParameterProvider::provideArrayParameter(Option::RULE_CONFIGURATIONS); + + $rules = []; + foreach ($ruleClasses as $ruleClass) { + $rules[] = [ + 'class' => $ruleClass, + 'config' => isset($ruleConfigurations[$ruleClass]) + ? $this->normalizeValue($ruleConfigurations[$ruleClass]) + : null, + ]; + } + + return $rules; + } + + /** + * Same shape as the Skipper reads Option::SKIP: a path, a class skipped everywhere, or a class skipped on paths + * + * @param string[] $ruleClasses + * @return array + */ + private function dumpSkips(array $ruleClasses): array + { + $skips = []; + + foreach (SimpleParameterProvider::provideArrayParameter(Option::SKIP) as $key => $value) { + if (is_int($key)) { + if (! is_string($value)) { + continue; + } + + if ($this->isClassLike($value)) { + foreach ($this->resolveCoveredClasses($value, $ruleClasses) as $class) { + $skips[] = [ + 'class' => $class, + ]; + } + + continue; + } + + $skips[] = [ + 'path' => $value, + ]; + continue; + } + + // the Skipper only applies class keys + if (! $this->isClassLike($key)) { + continue; + } + + foreach ($this->resolveCoveredClasses($key, $ruleClasses) as $class) { + // a non-array value skips the class everywhere, as in the Skipper + $skips[] = is_array($value) ? [ + 'class' => $class, + 'paths' => array_values($value), + ] : [ + 'class' => $class, + ]; + } + } + + return $skips; + } + + /** + * The Skipper matches with is_a(), so a skipped interface or parent class also names each rule it covers + * + * @param string[] $ruleClasses + * @return string[] + */ + private function resolveCoveredClasses(string $skippedClass, array $ruleClasses): array + { + $classes = [$skippedClass]; + + foreach ($ruleClasses as $ruleClass) { + if ($ruleClass !== $skippedClass && is_a($ruleClass, $skippedClass, true)) { + $classes[] = $ruleClass; + } + } + + return $classes; + } + + private function isClassLike(string $name): bool + { + return $this->reflectionProvider->hasClass($name); + } + + private function normalizeValue(mixed $value): mixed + { + if (is_array($value)) { + return array_map($this->normalizeValue(...), $value); + } + + if (! is_object($value)) { + return $value; + } + + $normalized = [ + '@class' => $value::class, + ]; + + $reflectionObject = new ReflectionObject($value); + foreach ($reflectionObject->getProperties() as $reflectionProperty) { + if ($reflectionProperty->isStatic()) { + continue; + } + + // lazy-initialized property + if (! $reflectionProperty->isInitialized($value)) { + continue; + } + + $normalized[$reflectionProperty->getName()] = $this->normalizeValue( + $reflectionProperty->getValue($value) + ); + } + + return $normalized; + } +} diff --git a/src/Console/Command/DumpConfigCommand.php b/src/Console/Command/DumpConfigCommand.php new file mode 100644 index 00000000000..1a7a90565b1 --- /dev/null +++ b/src/Console/Command/DumpConfigCommand.php @@ -0,0 +1,37 @@ +setName('dump-config'); + $this->setDescription('Dump the resolved configuration (paths, rules with configuration, skips) as JSON'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln(Json::encode($this->configDumper->dump(), pretty: true)); + + return ExitCode::SUCCESS; + } +} diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 0c9feaa1c9a..e6cd38896ce 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -24,6 +24,7 @@ use Rector\Configuration\ConfigurationRuleFilter; use Rector\Configuration\RenamedClassesDataCollector; use Rector\Console\Command\ComposerBasedCommand; +use Rector\Console\Command\DumpConfigCommand; use Rector\Console\Command\ListRulesCommand; use Rector\Console\Command\ProcessCommand; use Rector\Console\Command\ValidateConfigCommand; @@ -130,6 +131,7 @@ private function registerConsole(RectorConfig $rectorConfig): void $rectorConfig->singleton(AllRectorRulesProvider::class); $rectorConfig->singleton(ListRulesCommand::class); $rectorConfig->singleton(ComposerBasedCommand::class); + $rectorConfig->singleton(DumpConfigCommand::class); } diff --git a/tests/Configuration/ConfigDumperTest.php b/tests/Configuration/ConfigDumperTest.php new file mode 100644 index 00000000000..bd1ab92e7d7 --- /dev/null +++ b/tests/Configuration/ConfigDumperTest.php @@ -0,0 +1,111 @@ +freshRectorConfig = new RectorConfig(); + $this->configDumper = $this->make(ConfigDumper::class); + } + + public function testRules(): void + { + $this->freshRectorConfig->paths([__DIR__ . '/Source']); + $this->freshRectorConfig->rule(RemoveUnusedPrivateMethodRector::class); + $this->freshRectorConfig->ruleWithConfiguration(RenameClassRector::class, [ + 'App\OldClass' => 'App\NewClass', + ]); + $this->freshRectorConfig->ruleWithConfiguration(RenameMethodRector::class, [ + new MethodCallRename('App\SomeClass', 'oldMethod', 'newMethod'), + ]); + + $dump = $this->configDumper->dump(); + + $this->assertSame([__DIR__ . '/Source'], $dump['paths']); + $this->assertSame([ + [ + 'class' => RemoveUnusedPrivateMethodRector::class, + 'config' => null, + ], + [ + 'class' => RenameClassRector::class, + 'config' => [ + 'App\OldClass' => 'App\NewClass', + ], + ], + [ + 'class' => RenameMethodRector::class, + 'config' => [ + [ + '@class' => MethodCallRename::class, + 'class' => 'App\SomeClass', + 'oldMethod' => 'oldMethod', + 'newMethod' => 'newMethod', + ], + ], + ], + ], $dump['rules']); + } + + public function testSkips(): void + { + $this->freshRectorConfig->rule(RemoveUnusedPrivateMethodRector::class); + $this->freshRectorConfig->ruleWithConfiguration(RenameClassRector::class, [ + 'App\OldClass' => 'App\NewClass', + ]); + $this->freshRectorConfig->skip([ + __DIR__ . '/Source', + RemoveUnusedPrivateMethodRector::class => [__DIR__ . '/Fixture'], + ConfigurableRectorInterface::class, + ]); + + $dump = $this->configDumper->dump(); + + $this->assertSame([ + [ + 'path' => __DIR__ . '/Source', + ], + [ + 'class' => RemoveUnusedPrivateMethodRector::class, + 'paths' => [__DIR__ . '/Fixture'], + ], + [ + 'class' => ConfigurableRectorInterface::class, + ], + [ + 'class' => RenameClassRector::class, + ], + ], $dump['skips']); + } +}