diff --git a/src/Configuration/ConfigMerger.php b/src/Configuration/ConfigMerger.php new file mode 100644 index 00000000000..16bb4335867 --- /dev/null +++ b/src/Configuration/ConfigMerger.php @@ -0,0 +1,37 @@ +rectorClassValidator = $rectorClassValidator; $this->rectorClassNormalizer = $rectorClassNormalizer; + $this->configMerger = $configurationMerger; } /** @@ -36,43 +44,18 @@ final class RectorsExtension extends Extension return; } - $rectors = $this->mergeAllConfigsRecursively($configs); - + $rectors = $this->configMerger->mergeConfigs($configs); $rectors = $this->rectorClassNormalizer->normalize($rectors); $this->rectorClassValidator->validate(array_keys($rectors)); foreach ($rectors as $rectorClass => $arguments) { $rectorDefinition = $containerBuilder->autowire($rectorClass); - if (! count($arguments)) { + if (! $arguments) { continue; } $rectorDefinition->setArguments([$arguments]); } } - - /** - * This magic will merge array recursively - * without making any extra duplications. - * - * Only array_merge doesn't work in this case. - * - * @param mixed[] $configs - * @return mixed[] - */ - private function mergeAllConfigsRecursively(array $configs): array - { - $mergedConfigs = []; - - foreach ($configs as $config) { - $mergedConfigs = array_merge( - $mergedConfigs, - $config, - array_replace_recursive($mergedConfigs, $config) - ); - } - - return $mergedConfigs; - } } diff --git a/src/DependencyInjection/RectorBundle.php b/src/DependencyInjection/RectorBundle.php index f4c250460dd..fbdd3601dec 100644 --- a/src/DependencyInjection/RectorBundle.php +++ b/src/DependencyInjection/RectorBundle.php @@ -2,6 +2,7 @@ namespace Rector\DependencyInjection; +use Rector\Configuration\ConfigMerger; use Rector\Configuration\Normalizer\RectorClassNormalizer; use Rector\Configuration\Validator\RectorClassValidator; use Rector\DependencyInjection\Extension\RectorsExtension; @@ -11,9 +12,6 @@ final class RectorBundle extends Bundle { public function getContainerExtension(): RectorsExtension { - return new RectorsExtension( - new RectorClassValidator(), - new RectorClassNormalizer() - ); + return new RectorsExtension(new RectorClassValidator(), new RectorClassNormalizer(), new ConfigMerger()); } } diff --git a/tests/Configuration/ConfigMergerTest.php b/tests/Configuration/ConfigMergerTest.php new file mode 100644 index 00000000000..37ff3974e69 --- /dev/null +++ b/tests/Configuration/ConfigMergerTest.php @@ -0,0 +1,37 @@ +configMerger = new ConfigMerger(); + } + + public function test(): void + { + $result = $this->configMerger->mergeConfigs([ + ['SomeRector' => ['key' => 'value']] + ]); + + $this->assertSame(['SomeRector' => ['key' => 'value']], $result); + + $result = $this->configMerger->mergeConfigs([ + ['SomeRector' => ['key' => ['value']]], + ['SomeRector' => ['key' => ['another_value']]] + ]); + + $this->assertSame(['SomeRector' => ['key' => 'value']], $result); + + } +} +