mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 14:27:14 +01:00
5.2 KiB
5.2 KiB
How to use Dynamic Rectors?
Dynamic Rectors are classes, that are prepared to do specific operation. You can configure them with list of options, so they modify the case you need.
Replace a class name
# phpunit60.yml
services:
Rector\Rector\Dynamic\ClassReplacerRector:
$oldToNewClasses:
# old class: new class
'PHPUnit_Framework_TestCase': 'PHPUnit\Framework\TestCase'
Replace some part of the namespace
# better-reflection20.yml
services:
Rector\Rector\Dynamic\NamespaceReplacerRector:
$oldToNewNamespaces:
# old namespace: new namespace
'BetterReflection': 'Roave\BetterReflection'
Change a method name
services:
Rector\Rector\Dynamic\MethodNameReplacerRector:
$perClassOldToNewMethods:
# class
'Nette\Utils\Html':
# old method: new method
'add': 'addHtml'
# or in case of static methods calls
# class
'Nette\Bridges\FormsLatte\FormMacros':
# old method: [new class, new method]
'renderFormBegin': ['Nette\Bridges\FormsLatte\Runtime', 'renderFormBegin']
Change a property name
services:
Rector\Rector\Dynamic\PropertyNameReplacerRector:
$perClassOldToNewProperties:
# class:
'PhpParser\Node\Param':
# old property: new property
'name': 'var'
Change a class constant name
services:
Rector\Rector\Dynamic\ClassConstantReplacerRector:
$oldToNewConstantsByClass:
# class
'Symfony\Component\Form\FormEvents':
# old constant: new constant
'PRE_BIND': 'PRE_SUBMIT'
'BIND': 'SUBMIT'
'POST_BIND': 'POST_SUBMIT'
Change parameters type hinting according to the parent type
services:
Rector\Rector\Dynamic\ParentTypehintedArgumentRector:
$typehintForArgumentByMethodAndClass:
# class
'PhpParser\Parser':
# method
'parse':
# parameter: typehint
'code': 'string'
Change argument value or remove argument
services:
Rector\Rector\Dynamic\ArgumentRector:
$argumentChangesByMethodAndType:
-
class: 'Symfony\Component\DependencyInjection\ContainerBuilder'
method: 'compile'
position: 0
# change default value
type: 'changed'
default_value: false
# or remove
type: 'removed'
# or replace old default value by new one
type: 'replace_default_value'
replace_map:
'Symfony\Component\DependencyInjection\ContainerBuilder::SCOPE_PROTOTYPE': false
Replace the underscore naming _
with namespaces \
services:
Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector:
$configuration:
# old namespace prefix
- 'PHPUnit_'
# exclude classes
- '!PHPUnit_Framework_MockObject_MockObject'
Modify a property to method
services:
Rector\Rector\Dynamic\PropertyToMethodRector:
$perClassPropertyToMethods:
# type
'Symfony\Component\Translation\Translator':
# property to replace
'locale':
# (prepared key): get method name
'get': 'getLocale'
# (prepared key): set method name
'set': 'setLocale'
Remove a value object and use simple type
services:
Rector\Rector\Dynamic\ValueObjectRemoverRector:
$valueObjectsToSimpleTypes:
# type: new simple type
'ValueObject\Name': 'string'
For example:
- $value = new ValueObject\Name('Tomas');
+ $value = 'Tomas';
/**
-* @var ValueObject\Name
+* @var string
*/
private $name;
- public function someMethod(ValueObject\Name $name) { ...
+ public function someMethod(string $name) { ...
Replace Property and Method Annotations
services:
Rector\Rector\Dynamic\AnnotationReplacerRector:
$classToAnnotationMap:
# type
PHPUnit\Framework\TestCase:
# old annotation: new annotation
scenario: test
final class SomeTest extends PHPUnit\Framework\TestCase
{
/**
- * @scenario
+ * @test
*/
public function test()
{
}
}
Remove Fluent Interface
services:
Rector\Rector\Dynamic\FluentReplaceRector: ~
class SomeClass
{
public function setValue($value)
{
$this->value = $value;
- return $this;
}
public function setAnotherValue($anontherValue)
{
$this->anotherValue = $anotherValue;
- return $this;
}
}
$someClass = new SomeClass();
- $someClass->setValue(5)
+ $someClass->setValue(5);
- ->setAnotherValue(10);
+ $someClass->setAnotherValue(10);
}