Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Merge Config instead of Replace on RectorConfig->ruleWithConfiguration() take - 2 #2656

Closed
wants to merge 17 commits into from
Closed
13 changes: 13 additions & 0 deletions packages/Config/RectorConfig.php
Expand Up @@ -20,6 +20,11 @@
*/
final class RectorConfig extends ContainerConfigurator
{
/**
* @var array<string, mixed>
*/
private array $configuration = [];

/**
* @param string[] $paths
*/
Expand Down Expand Up @@ -123,6 +128,12 @@ public function ruleWithConfiguration(string $rectorClass, array $configuration)
Assert::isAOf($rectorClass, ConfigurableRectorInterface::class);

$services = $this->services();
$originalConfiguration = $configuration;

if (isset($this->configuration[$rectorClass]) && is_array($this->configuration[$rectorClass])) {
$configuration = array_merge($this->configuration[$rectorClass], $originalConfiguration);
$originalConfiguration = $configuration;
}

// decorate with value object inliner so Symfony understands, see https://getrector.org/blog/2020/09/07/how-to-inline-value-object-in-symfony-php-config
array_walk_recursive($configuration, static function (&$value) {
Expand All @@ -135,6 +146,8 @@ public function ruleWithConfiguration(string $rectorClass, array $configuration)

$services->set($rectorClass)
->call('configure', [$configuration]);

$this->configuration[$rectorClass] = $originalConfiguration;
}

/**
Expand Down
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo2;

class RenameMethodCall2
{
private function call()
{
$foo = new Foo2();
$foo->old();
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo2;

class RenameMethodCall2
{
private function call()
{
$foo = new Foo2();
$foo->new();
}
}

?>
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

final class Foo2 implements FooInterface
{
public function old(): void
{
}

public function new(): void
{
}
}
Expand Up @@ -10,6 +10,7 @@
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\CustomType;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\DifferentInterface;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo2;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\NewInterface;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\SomeSubscriber;

Expand All @@ -25,4 +26,10 @@
// with array key
new MethodCallRenameWithArrayKey('Nette\Utils\Html', 'addToArray', 'addToHtmlArray', 'hey'),
]);

$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
// add new config on purpose
// to ensure configs are merged
new MethodCallRename(Foo2::class, 'old', 'new'),
]);
};
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture;

use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClassWithTypo2;

class NameInsensitive2 extends OldClassWithTypO2
{
public function run(): OLDClassWithTYPO2
{
$oldClassWithTypo = new OldClassWithTYPO2;
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture;

use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClassWithTypo2;

class NameInsensitive2 extends \Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClassWithoutTypo2
{
public function run(): \Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClassWithoutTypo2
{
$oldClassWithTypo = new \Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClassWithoutTypo2;
}
}

?>
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source;

class NewClassWithoutTypo2
{

}
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source;

class OldClassWithTypo2
{

}
Expand Up @@ -13,8 +13,10 @@
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\Contract\ThirdInterface;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClass;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClassWithoutTypo;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClassWithoutTypo2;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClass;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClassWithTypo;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClassWithTypo2;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\SomeFinalClass;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\SomeNonFinalClass;

Expand Down Expand Up @@ -47,4 +49,11 @@
'Doctrine\DBAL\DBALException' => 'Doctrine\DBAL\Exception',
'Bar' => 'BarInterface',
]);

$rectorConfig
->ruleWithConfiguration(RenameClassRector::class, [
// add new config on purpose
// to ensure configs are merged
OldClassWithTypo2::class => NewClassWithoutTypo2::class,
]);
};