Some rules do a fixed job and need no options - those are registered with withRules(). Others accept configuration to tell them what to change. Rules that implement Rector\Contract\Rector\ConfigurableRectorInterface are configurable, and you register them with withConfiguredRule().
A typical example is Rector\Renaming\Rector\Name\RenameClassRector. It renames a class, but it needs to know the old and new name:
<?php
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withConfiguredRule(RenameClassRector::class, [
'App\SomeOldClass' => 'App\SomeNewClass',
]);
This turns:
-use App\SomeOldClass;
+use App\SomeNewClass;
-$value = new App\SomeOldClass();
+$value = new App\SomeNewClass();
Use Find Rule and open the rule detail. Configurable rules show a configuration example with the exact array shape they expect. You can also check the rule's source - a configurable rule implements ConfigurableRectorInterface.
Mix configurable and non-configurable rules freely. Call withConfiguredRule() once per configured rule, and group the rest in withRules():
<?php
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withConfiguredRule(RenameClassRector::class, [
'App\SomeOldClass' => 'App\SomeNewClass',
])
->withRules([
ClassPropertyAssignToConstructorPromotionRector::class,
]);