Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions src/Configuration/ConfigDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

declare(strict_types=1);

namespace Rector\Configuration;

use PHPStan\Reflection\ReflectionProvider;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use ReflectionObject;

/**
* Turns the resolved rector.php configuration into plain data - paths, rules with their configuration
* and skips - for external runners to consume.
*
* @see \Rector\Tests\Configuration\ConfigDumperTest
*/
final readonly class ConfigDumper
{
public function __construct(
private ReflectionProvider $reflectionProvider
) {
}

/**
* @return array{
* php_version: int|null,
* paths: string[],
* skips: array<array{path?: string, class?: string, paths?: string[]}>,
* rules: array<array{class: string, config: mixed}>,
* 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<array{class: string, config: mixed}>
*/
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<array{path?: string, class?: string, paths?: string[]}>
*/
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;
}
}
37 changes: 37 additions & 0 deletions src/Console/Command/DumpConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Console\Command;

use Nette\Utils\Json;
use Rector\Configuration\ConfigDumper;
use Rector\Console\ExitCode;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @see \Rector\Tests\Configuration\ConfigDumperTest
*/
final class DumpConfigCommand extends Command
{
public function __construct(
private readonly ConfigDumper $configDumper
) {
parent::__construct();
}

protected function configure(): void
{
$this->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;
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

}

Expand Down
111 changes: 111 additions & 0 deletions tests/Configuration/ConfigDumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Configuration;

use Rector\Config\RectorConfig;
use Rector\Configuration\ConfigDumper;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class ConfigDumperTest extends AbstractLazyTestCase
{
private RectorConfig $freshRectorConfig;

private ConfigDumper $configDumper;

protected function setUp(): void
{
parent::setUp();

// the parameter provider is static, reset it between tests
foreach ([
Option::PATHS,
Option::SKIP,
Option::REGISTERED_RECTOR_RULES,
Option::RULE_CONFIGURATIONS,
] as $optionName) {
SimpleParameterProvider::setParameter($optionName, []);
}

$this->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']);
}
}
Loading