rector/docs/AllRectorsOverview.md

2420 lines
51 KiB
Markdown
Raw Normal View History

2018-05-04 22:30:32 +00:00
# All Rectors Overview
2018-04-29 09:03:47 +00:00
2018-08-01 20:09:34 +00:00
- [Projects](#projects)
- [General](#general)
## Projects
2018-09-28 16:33:35 +00:00
- [CakePHP\MethodCall](#cakephpmethodcall)
2018-08-01 20:09:34 +00:00
- [Doctrine](#doctrine)
2018-07-31 21:47:59 +00:00
- [PHPUnit](#phpunit)
- [PHPUnit\SpecificMethod](#phpunitspecificmethod)
2018-08-01 20:09:34 +00:00
- [PhpParser](#phpparser)
2018-10-12 23:15:00 +00:00
- [Php\Assign](#phpassign)
- [Php\BinaryOp](#phpbinaryop)
- [Php\ClassConst](#phpclassconst)
- [Php\ConstFetch](#phpconstfetch)
- [Php\Each](#phpeach)
- [Php\FuncCall](#phpfunccall)
- [Php\FunctionLike](#phpfunctionlike)
- [Php\List_](#phplist_)
- [Php\Name](#phpname)
- [Php\Property](#phpproperty)
- [Php\Ternary](#phpternary)
- [Php\TryCatch](#phptrycatch)
2018-08-01 20:09:34 +00:00
- [Sensio\FrameworkExtraBundle](#sensioframeworkextrabundle)
2018-09-28 16:33:35 +00:00
- [Silverstripe](#silverstripe)
2018-07-31 21:47:59 +00:00
- [Sylius\Review](#syliusreview)
- [Symfony\Console](#symfonyconsole)
2018-08-01 20:09:34 +00:00
- [Symfony\Controller](#symfonycontroller)
2018-07-31 21:47:59 +00:00
- [Symfony\DependencyInjection](#symfonydependencyinjection)
- [Symfony\Form](#symfonyform)
2018-08-01 20:09:34 +00:00
- [Symfony\FrameworkBundle](#symfonyframeworkbundle)
- [Symfony\HttpKernel](#symfonyhttpkernel)
2018-07-31 21:47:59 +00:00
- [Symfony\Process](#symfonyprocess)
2018-08-01 20:09:34 +00:00
- [Symfony\Validator](#symfonyvalidator)
- [Symfony\VarDumper](#symfonyvardumper)
- [Symfony\Yaml](#symfonyyaml)
2018-09-28 16:33:35 +00:00
- [Twig](#twig)
## CakePHP\MethodCall
### `ModalToGetSetRector`
- class: `Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector`
Changes combined set/get `value()` to specific `getValue()` or `setValue(x)`.
```diff
$object = new InstanceConfigTrait;
-$config = $object->config();
-$config = $object->config('key');
+$config = $object->getConfig();
+$config = $object->getConfig('key');
-$object->config('key', 'value');
-$object->config(['key' => 'value']);
+$object->setConfig('key', 'value');
+$object->setConfig(['key' => 'value']);
```
2018-08-01 20:09:34 +00:00
## Doctrine
### `AliasToClassRector`
- class: `Rector\Doctrine\Rector\AliasToClassRector`
Replaces doctrine alias with class.
```diff
2018-08-10 19:07:08 +00:00
$entityManager = new Doctrine\ORM\EntityManager();
-$entityManager->getRepository("AppBundle:Post");
+$entityManager->getRepository(\App\Entity\Post::class);
2018-08-01 20:09:34 +00:00
```
2018-07-31 21:47:59 +00:00
## PHPUnit
2018-10-12 23:15:00 +00:00
### `ArrayToYieldDataProviderRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\PHPUnit\Rector\ArrayToYieldDataProviderRector`
2018-05-05 12:48:33 +00:00
2018-10-12 23:15:00 +00:00
Turns method data providers in PHPUnit from arrays to yield
2018-05-05 12:48:33 +00:00
```diff
2018-10-12 23:15:00 +00:00
/**
- * @return mixed[]
*/
-public function provide(): array
+public function provide(): Iterator
{
2018-10-12 23:15:00 +00:00
- return [
- ['item']
- ]
+ yield ['item'];
}
2018-07-31 12:50:39 +00:00
```
### `DelegateExceptionArgumentsRector`
- class: `Rector\PHPUnit\Rector\DelegateExceptionArgumentsRector`
2018-07-31 12:50:39 +00:00
Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
2018-07-31 12:50:39 +00:00
```diff
-$this->setExpectedException(Exception::class, "Message", "CODE");
+$this->setExpectedException(Exception::class);
+$this->expectExceptionMessage("Message");
+$this->expectExceptionCode("CODE");
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `GetMockRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\PHPUnit\Rector\GetMockRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Turns getMock*() methods to createMock()
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-$this->getMock("Class");
+$this->createMock("Class");
```
```diff
-$this->getMockWithoutInvokingTheOriginalConstructor("Class");
+$this->createMock("Class");
2018-07-31 12:50:39 +00:00
```
2018-09-28 16:33:35 +00:00
### `TryCatchToExpectExceptionRector`
- class: `Rector\PHPUnit\Rector\TryCatchToExpectExceptionRector`
Turns try/catch to expectException() call
```diff
-try {
- $someService->run();
-} catch (Throwable $exception) {
- $this->assertInstanceOf(RuntimeException::class, $e);
- $this->assertContains('There was an error executing the following script', $e->getMessage());
-}
+$this->expectException(RuntimeException::class);
+$this->expectExceptionMessage('There was an error executing the following script');
+$someService->run();
```
2018-10-12 23:15:00 +00:00
### `ExceptionAnnotationRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\PHPUnit\Rector\ExceptionAnnotationRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-/**
- * @expectedException Exception
- * @expectedExceptionMessage Message
- */
public function test()
{
+ $this->expectException('Exception');
+ $this->expectExceptionMessage('Message');
// tested code
}
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
## PHPUnit\SpecificMethod
### `AssertTrueFalseInternalTypeToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertTrueFalseInternalTypeToSpecificMethodRector`
Turns true/false with internal type comparisons to their method name alternatives in PHPUnit TestCase
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-$this->assertTrue(is_{internal_type}($anything), "message");
+$this->assertInternalType({internal_type}, $anything, "message");
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
```diff
-$this->assertFalse(is_{internal_type}($anything), "message");
+$this->assertNotInternalType({internal_type}, $anything, "message");
```
### `AssertNotOperatorRector`
2018-07-31 12:50:39 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertNotOperatorRector`
Turns not-operator comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertTrue(!$foo, "message");
+$this->assertFalse($foo, "message");
2018-08-01 20:09:34 +00:00
```
2018-08-01 20:09:34 +00:00
```diff
-$this->assertFalse(!$foo, "message");
+$this->assertTrue($foo, "message");
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `AssertInstanceOfComparisonRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertInstanceOfComparisonRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Turns instanceof comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-$this->assertTrue($foo instanceof Foo, "message");
+$this->assertFalse($foo instanceof Foo, "message");
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-$this->assertInstanceOf("Foo", $foo, "message");
+$this->assertNotInstanceOf("Foo", $foo, "message");
```
### `AssertSameBoolNullToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertSameBoolNullToSpecificMethodRector`
Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertSame(null, $anything);
+$this->assertNull($anything);
```
```diff
-$this->assertNotSame(false, $anything);
+$this->assertNotFalse($anything);
2018-07-31 12:50:39 +00:00
```
### `AssertPropertyExistsRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertPropertyExistsRector`
2018-07-31 12:50:39 +00:00
Turns `property_exists` comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertTrue(property_exists(new Class, "property"), "message");
+$this->assertClassHasAttribute("property", "Class", "message");
2018-08-01 20:09:34 +00:00
```
2018-08-01 20:09:34 +00:00
```diff
-$this->assertFalse(property_exists(new Class, "property"), "message");
+$this->assertClassNotHasAttribute("property", "Class", "message");
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `AssertIssetToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertIssetToSpecificMethodRector`
Turns isset comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertTrue(isset($anything->foo));
+$this->assertFalse(isset($anything["foo"]), "message");
```
```diff
-$this->assertObjectHasAttribute("foo", $anything);
+$this->assertArrayNotHasKey("foo", $anything, "message");
```
### `AssertTrueFalseToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertTrueFalseToSpecificMethodRector`
Turns true/false comparisons to their method name alternatives in PHPUnit TestCase when possible
```diff
-$this->assertTrue(is_readable($readmeFile), "message");
+$this->assertIsReadable($readmeFile, "message");
```
### `AssertComparisonToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertComparisonToSpecificMethodRector`
Turns comparison operations to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertTrue($foo === $bar, "message");
+$this->assertSame($bar, $foo, "message");
```
```diff
-$this->assertFalse($foo >= $bar, "message");
+$this->assertLessThanOrEqual($bar, $foo, "message");
```
### `AssertCompareToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertCompareToSpecificMethodRector`
Turns vague php-only method in PHPUnit TestCase to more specific
```diff
-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
```
```diff
-$this->assertSame($value, {function}($anything), "message");
+$this->assert{function}($value, $anything, "message\");
```
```diff
-$this->assertEquals($value, {function}($anything), "message");
+$this->assert{function}($value, $anything, "message\");
```
```diff
-$this->assertNotSame($value, {function}($anything), "message");
+$this->assertNot{function}($value, $anything, "message")
```
```diff
-$this->assertNotEquals($value, {function}($anything), "message");
+$this->assertNot{function}($value, $anything, "message")
```
### `AssertRegExpRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertRegExpRector`
Turns `preg_match` comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertSame(1, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertRegExp("/^Message for ".*"\.$/", $string, $message);
```
```diff
-$this->assertEquals(false, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertNotRegExp("/^Message for ".*"\.$/", $string, $message);
```
### `AssertFalseStrposToContainsRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertFalseStrposToContainsRector`
Turns `strpos`/`stripos` comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertFalse(strpos($anything, "foo"), "message");
+$this->assertNotContains("foo", $anything, "message");
```
```diff
-$this->assertNotFalse(stripos($anything, "foo"), "message");
+$this->assertContains("foo", $anything, "message");
```
## PhpParser
### `IdentifierRector`
- class: `Rector\PhpParser\Rector\IdentifierRector`
Turns node string names to Identifier object in php-parser
```diff
$constNode = new PhpParser\Node\Const_;
-$name = $constNode->name;
+$name = $constNode->name->toString();'
```
### `ParamAndStaticVarNameRector`
- class: `Rector\PhpParser\Rector\ParamAndStaticVarNameRector`
Turns old string `var` to `var->name` sub-variable in Node of PHP-Parser
```diff
-$paramNode->name;
+$paramNode->var->name;
```
```diff
-$staticVarNode->name;
+$staticVarNode->var->name;
```
### `RemoveNodeRector`
- class: `Rector\PhpParser\Rector\RemoveNodeRector`
Turns integer return to remove node to constant in NodeVisitor of PHP-Parser
```diff
public function leaveNode()
{
- return false;
+ return NodeTraverser::REMOVE_NODE;
}
```
### `SetLineRector`
- class: `Rector\PhpParser\Rector\SetLineRector`
Turns standalone line method to attribute in Node of PHP-Parser
```diff
-$node->setLine(5);
+$node->setAttribute("line", 5);
```
### `UseWithAliasRector`
- class: `Rector\PhpParser\Rector\UseWithAliasRector`
Turns use property to method and `$node->alias` to last name in UseAlias Node of PHP-Parser
```diff
-$node->alias;
+$node->getAlias();
```
```diff
-$node->name->getLast();
+$node->alias
```
### `CatchAndClosureUseNameRector`
- class: `Rector\PhpParser\Rector\CatchAndClosureUseNameRector`
Turns `$catchNode->var` to its new `name` property in php-parser
```diff
-$catchNode->var;
+$catchNode->var->name
```
## Php\Assign
### `AssignArrayToStringRector`
- class: `Rector\Php\Rector\Assign\AssignArrayToStringRector`
String cannot be turned into array by assignment anymore
```diff
-$string = '';
+$string = [];
$string[] = 1;
```
## Php\BinaryOp
### `IsCountableRector`
- class: `Rector\Php\Rector\BinaryOp\IsCountableRector`
Changes is_array + Countable check to is_countable
```diff
-is_array($foo) || $foo instanceof Countable;
+is_countable($foo);
```
### `IsIterableRector`
- class: `Rector\Php\Rector\BinaryOp\IsIterableRector`
Changes is_array + Traversable check to is_iterable
```diff
-is_array($foo) || $foo instanceof Traversable;
+is_iterable($foo);
```
## Php\ClassConst
### `PublicConstantVisibilityRector`
- class: `Rector\Php\Rector\ClassConst\PublicConstantVisibilityRector`
Add explicit public constant visibility.
```diff
class SomeClass
{
- const HEY = 'you';
+ public const HEY = 'you';
}
```
## Php\ConstFetch
2018-10-12 23:15:00 +00:00
### `SensitiveConstantNameRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\ConstFetch\SensitiveConstantNameRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Changes case insensitive constants to sensitive ones.
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
define('FOO', 42, true);
var_dump(FOO);
-var_dump(foo);
+var_dump(FOO);
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
## Php\Each
2018-10-12 23:15:00 +00:00
### `WhileEachToForeachRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\Each\WhileEachToForeachRector`
each() function is deprecated, use foreach() instead.
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-while (list($key, $callback) = each($callbacks)) {
+foreach ($callbacks as $key => $callback) {
// ...
}
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-while (list($key) = each($callbacks)) {
+foreach (array_keys($callbacks) as $key) {
// ...
}
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `ListEachRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\Each\ListEachRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
each() function is deprecated, use foreach() instead.
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-list($key, $callback) = each($callbacks);
+$key = key($opt->option);
+$val = current($opt->option);
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
## Php\FuncCall
### `RandomFunctionRector`
- class: `Rector\Php\Rector\FuncCall\RandomFunctionRector`
Changes rand, srand and getrandmax by new md_* alternatives.
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-rand();
+mt_rand();
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `ArrayKeyFirstLastRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FuncCall\ArrayKeyFirstLastRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Make use of array_key_first() and array_key_last()
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
2018-08-01 20:09:34 +00:00
```
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-end($items);
-$lastKey = key($items);
+$lastKey = array_key_last($items);
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `PowToExpRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FuncCall\PowToExpRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Changes pow(val, val2) to ** (exp) parameter
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-pow(1, 2);
+1**2;
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
### `SensitiveDefineRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FuncCall\SensitiveDefineRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Changes case insensitive constants to sensitive ones.
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-define('FOO', 42, true);
+define('FOO', 42);
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `MultiDirnameRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FuncCall\MultiDirnameRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Changes multiple dirname() calls to one with nesting level
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-dirname(dirname($path));
+dirname($path, 2);
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
### `EregToPregMatchRector`
- class: `Rector\Php\Rector\FuncCall\EregToPregMatchRector`
Changes ereg*() to preg*() calls
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-ereg("hi")
+preg_match("#hi#");
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `CallUserMethodRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FuncCall\CallUserMethodRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
Changes call_user_method()/call_user_method_array() to call_user_func()/call_user_func_array()
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-call_user_method($method, $obj, "arg1", "arg2");
+call_user_func(array(&$obj, "method"), "arg1", "arg2");
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
### `CountOnNullRector`
- class: `Rector\Php\Rector\FuncCall\CountOnNullRector`
Changes count() on null to safe ternary check
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
$values = null;
-$count = count($values);
+$count = is_array($values) || $values instanceof Countable ? count($values) : 0;
2018-05-05 12:48:33 +00:00
```
2018-10-12 23:15:00 +00:00
## Php\FunctionLike
2018-10-12 23:15:00 +00:00
### `ExceptionHandlerTypehintRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FunctionLike\ExceptionHandlerTypehintRector`
Changes property @var annotations from annotation to type.
```diff
2018-10-12 23:15:00 +00:00
-function handler(Exception $exception) { ... }
+function handler(Throwable $exception) { ... }
set_exception_handler('handler');
```
2018-10-12 23:15:00 +00:00
### `Php4ConstructorRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\FunctionLike\Php4ConstructorRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
Changes PHP 4 style constructor to __construct.
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
class SomeClass
2018-08-01 20:09:34 +00:00
{
2018-10-12 23:15:00 +00:00
- public function SomeClass()
+ public function __construct()
{
}
2018-08-01 20:09:34 +00:00
}
```
2018-10-12 23:15:00 +00:00
## Php\List_
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
### `ListSplitStringRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\List_\ListSplitStringRector`
list() cannot split string directly anymore, use str_split()
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-list($foo) = "string";
+list($foo) = str_split("string");
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
### `EmptyListRector`
- class: `Rector\Php\Rector\List_\EmptyListRector`
list() cannot be empty
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-list() = $values;
+list($generated) = $values;
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
### `ListSwapArrayOrderRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\List_\ListSwapArrayOrderRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
list() assigns variables in reverse order - relevant in array assign
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-list($a[], $a[]) = [1, 2];
+list($a[], $a[]) = array_reverse([1, 2])];
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
## Php\Name
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
### `ReservedObjectRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\Name\ReservedObjectRector`
Changes reserved "Object" name to "<Smart>Object" where <Smart> can be configured
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-class Object
+class SmartObject
{
}
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
## Php\Property
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
### `TypedPropertyRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\Property\TypedPropertyRector`
Changes property @var annotations from annotation to type.
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
final class SomeClass
{
- /**
- * @var int
- */
- private count;
+ private int count;
}
2018-08-01 20:09:34 +00:00
```
2018-10-12 23:15:00 +00:00
## Php\Ternary
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
### `TernaryToNullCoalescingRector`
2018-08-01 20:09:34 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Php\Rector\Ternary\TernaryToNullCoalescingRector`
Changes unneeded null check to ?? operator
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-$value === null ? 10 : $value;
+$value ?? 10;
2018-08-01 20:09:34 +00:00
```
```diff
2018-10-12 23:15:00 +00:00
-isset($value) ? $value : 10;
+$value ?? 10;
```
## Php\TryCatch
### `MultiExceptionCatchRector`
- class: `Rector\Php\Rector\TryCatch\MultiExceptionCatchRector`
Changes multi catch of same exception to single one | separated.
```diff
try {
// Some code...
-} catch (ExceptionType1 $exception) {
- $sameCode;
-} catch (ExceptionType2 $exception) {
+} catch (ExceptionType1 | ExceptionType2 $exception) {
$sameCode;
}
2018-08-01 20:09:34 +00:00
```
## Sensio\FrameworkExtraBundle
### `TemplateAnnotationRector`
- class: `Rector\Sensio\Rector\FrameworkExtraBundle\TemplateAnnotationRector`
Turns @Template annotation to explicit method call in Controller of FrameworkExtraBundle in Symfony
```diff
-/**
- * @Template()
- */
public function indexAction()
{
+ return $this->render("index.html.twig");
}
```
2018-09-28 16:33:35 +00:00
## Silverstripe
### `ConstantToStaticCallRector`
- class: `Rector\Silverstripe\Rector\ConstantToStaticCallRector`
Turns defined constant to static method call.
```diff
-SS_DATABASE_NAME;
+Environment::getEnv("SS_DATABASE_NAME");
```
### `DefineConstantToStaticCallRector`
- class: `Rector\Silverstripe\Rector\DefineConstantToStaticCallRector`
Turns defined function call to static method call.
```diff
-defined("SS_DATABASE_NAME");
+Environment::getEnv("SS_DATABASE_NAME");
```
## Sylius\Review
### `ReplaceCreateMethodWithoutReviewerRector`
- class: `Rector\Sylius\Rector\Review\ReplaceCreateMethodWithoutReviewerRector`
2018-07-31 12:50:39 +00:00
Turns `createForSubjectWithReviewer()` with null review to standalone method in Sylius
2018-07-31 12:50:39 +00:00
```diff
-$this->createForSubjectWithReviewer($subject, null)
+$this->createForSubject($subject)
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\Console
2018-08-01 20:09:34 +00:00
### `ConsoleExceptionToErrorEventConstantRector`
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\Console\ConsoleExceptionToErrorEventConstantRector`
2018-08-01 20:09:34 +00:00
Turns old event name with EXCEPTION to ERROR constant in Console in Symfony
2018-07-31 12:50:39 +00:00
```diff
2018-08-01 20:09:34 +00:00
-"console.exception"
+Symfony\Component\Console\ConsoleEvents::ERROR
```
```diff
-Symfony\Component\Console\ConsoleEvents::EXCEPTION
+Symfony\Component\Console\ConsoleEvents::ERROR
```
2018-07-31 12:50:39 +00:00
## Symfony\Controller
2018-07-31 12:50:39 +00:00
### `AddFlashRector`
2018-07-31 12:50:39 +00:00
- class: `Rector\Symfony\Rector\Controller\AddFlashRector`
Turns long flash adding to short helper method in Controller in Symfony
```diff
class SomeController extends Controller
{
public function some(Request $request)
{
- $request->getSession()->getFlashBag()->add("success", "something");
+ $this->addFlash("success", "something");
}
}
2018-07-31 12:50:39 +00:00
```
### `RedirectToRouteRector`
2018-07-31 12:50:39 +00:00
- class: `Rector\Symfony\Rector\Controller\RedirectToRouteRector`
2018-07-31 12:50:39 +00:00
Turns redirect to route to short helper method in Controller in Symfony
2018-07-31 12:50:39 +00:00
```diff
-$this->redirect($this->generateUrl("homepage"));
+$this->redirectToRoute("homepage");
```
### `ActionSuffixRemoverRector`
- class: `Rector\Symfony\Rector\Controller\ActionSuffixRemoverRector`
Removes Action suffixes from methods in Symfony Controllers
2018-07-31 12:50:39 +00:00
```diff
class SomeController
{
- public function indexAction()
+ public function index()
{
}
}
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\DependencyInjection
2018-08-01 20:09:34 +00:00
### `ContainerBuilderCompileEnvArgumentRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\DependencyInjection\ContainerBuilderCompileEnvArgumentRector`
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
Turns old default value to parameter in ContinerBuilder->build() method in DI in Symfony
2018-07-31 12:50:39 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$containerBuilder = new Symfony\Component\DependencyInjection\ContainerBuilder(); $containerBuilder->compile();
+$containerBuilder = new Symfony\Component\DependencyInjection\ContainerBuilder(); $containerBuilder->compile(true);
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\Form
2018-10-12 23:15:00 +00:00
### `FormTypeGetParentRector`
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Symfony\Rector\Form\FormTypeGetParentRector`
2018-10-12 23:15:00 +00:00
Turns string Form Type references to their CONSTANT alternatives in `getParent()` and `getExtendedType()` methods in Form in Symfony
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-function getParent() { return "collection"; }
+function getParent() { return CollectionType::class; }
2018-07-31 12:50:39 +00:00
```
```diff
2018-10-12 23:15:00 +00:00
-function getExtendedType() { return "collection"; }
+function getExtendedType() { return CollectionType::class; }
```
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
### `StringFormTypeToClassRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\Form\StringFormTypeToClassRector`
2018-08-01 20:09:34 +00:00
Turns string Form Type references to their CONSTANT alternatives in FormTypes in Form in Symfony
```diff
2018-09-28 16:33:35 +00:00
$formBuilder = new Symfony\Component\Form\FormBuilder;
-$formBuilder->add('name', 'form.type.text');
+$form->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class);
2018-07-31 12:50:39 +00:00
```
2018-10-12 23:15:00 +00:00
### `OptionNameRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Symfony\Rector\Form\OptionNameRector`
2018-10-12 23:15:00 +00:00
Turns old option names to new ones in FormTypes in Form in Symfony
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
$builder = new FormBuilder;
-$builder->add("...", ["precision" => "...", "virtual" => "..."];
+$builder->add("...", ["scale" => "...", "inherit_data" => "..."];
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-10-12 23:15:00 +00:00
### `FormIsValidRector`
- class: `Rector\Symfony\Rector\Form\FormIsValidRector`
Adds `$form->isSubmitted()` validatoin to all `$form->isValid()` calls in Form in Symfony
2018-07-31 12:50:39 +00:00
```diff
2018-10-12 23:15:00 +00:00
-if ($form->isValid()) {
+if ($form->isSubmitted() && $form->isValid()) {
}
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\FrameworkBundle
2018-10-12 23:15:00 +00:00
### `ContainerGetToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Symfony\Rector\FrameworkBundle\ContainerGetToConstructorInjectionRector`
2018-10-12 23:15:00 +00:00
Turns fetching of dependencies via `$container->get()` in ContainerAware to constructor injection in Command and Controller in Symfony
2018-05-04 22:30:32 +00:00
```diff
2018-10-12 23:15:00 +00:00
-final class SomeCommand extends ContainerAwareCommand
+final class SomeCommand extends Command
2018-05-04 22:30:32 +00:00
{
2018-10-12 23:15:00 +00:00
+ public function __construct(SomeService $someService)
2018-07-31 12:50:39 +00:00
+ {
2018-10-12 23:15:00 +00:00
+ $this->someService = $someService;
+ }
+
public function someMethod()
{
2018-10-12 23:15:00 +00:00
// ...
- $this->getContainer()->get('some_service');
- $this->container->get('some_service');
+ $this->someService;
+ $this->someService;
2018-05-04 22:30:32 +00:00
}
}
```
### `GetToConstructorInjectionRector`
- class: `Rector\Symfony\Rector\FrameworkBundle\GetToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
2018-05-04 22:30:32 +00:00
```diff
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
2018-05-04 22:30:32 +00:00
{
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
2018-07-31 12:50:39 +00:00
public function someMethod()
{
- // ...
- $this->get('some_service');
+ $this->someService;
2018-07-31 12:50:39 +00:00
}
2018-05-04 22:30:32 +00:00
}
```
2018-10-12 23:15:00 +00:00
### `GetParameterToConstructorInjectionRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Symfony\Rector\FrameworkBundle\GetParameterToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
Turns fetching of parameters via `getParameter()` in ContainerAware to constructor injection in Command and Controller in Symfony
2018-05-04 22:30:32 +00:00
```diff
2018-10-12 23:15:00 +00:00
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
2018-05-04 22:30:32 +00:00
{
2018-10-12 23:15:00 +00:00
+ private $someParameter;
+
+ public function __construct($someParameter)
2018-05-04 22:30:32 +00:00
+ {
2018-10-12 23:15:00 +00:00
+ $this->someParameter = $someParameter;
2018-05-04 22:30:32 +00:00
+ }
+
public function someMethod()
2018-05-04 22:30:32 +00:00
{
2018-10-12 23:15:00 +00:00
- $this->getParameter('someParameter');
+ $this->someParameter;
2018-05-04 22:30:32 +00:00
}
}
```
2018-08-01 20:09:34 +00:00
## Symfony\HttpKernel
2018-08-01 20:09:34 +00:00
### `GetRequestRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\HttpKernel\GetRequestRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
+use Symfony\Component\HttpFoundation\Request;
+
class SomeController
{
- public function someAction()
+ public action(Request $request)
{
- $this->getRequest()->...();
+ $request->...();
}
}
2018-05-04 22:30:32 +00:00
```
## Symfony\Process
2018-10-12 23:15:00 +00:00
### `ProcessBuilderInstanceRector`
- class: `Rector\Symfony\Rector\Process\ProcessBuilderInstanceRector`
Turns `ProcessBuilder::instance()` to new ProcessBuilder in Process in Symfony. Part of multi-step Rector.
```diff
-$processBuilder = Symfony\Component\Process\ProcessBuilder::instance($args);
+$processBuilder = new Symfony\Component\Process\ProcessBuilder($args);
```
### `ProcessBuilderGetProcessRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\Symfony\Rector\Process\ProcessBuilderGetProcessRector`
2018-05-04 22:30:32 +00:00
Removes `$processBuilder->getProcess()` calls to $processBuilder in Process in Symfony, because ProcessBuilder was removed. This is part of multi-step Rector and has very narrow focus.
2018-07-31 12:50:39 +00:00
```diff
$processBuilder = new Symfony\Component\Process\ProcessBuilder;
-$process = $processBuilder->getProcess();
-$commamdLine = $processBuilder->getProcess()->getCommandLine();
+$process = $processBuilder;
+$commamdLine = $processBuilder->getCommandLine();
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\Validator
2018-08-01 20:09:34 +00:00
### `ConstraintUrlOptionRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\Validator\ConstraintUrlOptionRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Turns true value to `Url::CHECK_DNS_TYPE_ANY` in Validator in Symfony.
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$constraint = new Url(["checkDNS" => true]);
+$constraint = new Url(["checkDNS" => Url::CHECK_DNS_TYPE_ANY]);
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\VarDumper
2018-08-01 20:09:34 +00:00
### `VarDumperTestTraitMethodArgsRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\VarDumper\VarDumperTestTraitMethodArgsRector`
2018-08-01 20:09:34 +00:00
Adds new `$format` argument in `VarDumperTestTrait->assertDumpEquals()` in Validator in Symfony.
2018-05-04 22:30:32 +00:00
```diff
2018-08-10 19:07:08 +00:00
-$varDumperTestTrait->assertDumpEquals($dump, $data, $mesage = "");
+$varDumperTestTrait->assertDumpEquals($dump, $data, $context = null, $mesage = "");
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
```diff
2018-08-10 19:07:08 +00:00
-$varDumperTestTrait->assertDumpMatchesFormat($dump, $format, $mesage = "");
+$varDumperTestTrait->assertDumpMatchesFormat($dump, $format, $context = null, $mesage = "");
2018-08-01 20:09:34 +00:00
```
2018-08-01 20:09:34 +00:00
## Symfony\Yaml
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
### `ParseFileRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Symfony\Rector\Yaml\ParseFileRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
session > use_strict_mode is true by default and can be removed
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
-session > use_strict_mode: true
+session:
```
2018-08-01 20:09:34 +00:00
### `SessionStrictTrueByDefaultYamlRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Symfony\Rector\Yaml\SessionStrictTrueByDefaultYamlRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
session > use_strict_mode is true by default and can be removed
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-session > use_strict_mode: true
+session:
2018-05-04 22:30:32 +00:00
```
2018-10-12 23:15:00 +00:00
### `SpaceBetweenKeyAndValueYamlRector`
2018-09-28 16:33:35 +00:00
2018-10-12 23:15:00 +00:00
- class: `Rector\Symfony\Rector\Yaml\SpaceBetweenKeyAndValueYamlRector`
2018-09-28 16:33:35 +00:00
2018-10-12 23:15:00 +00:00
Mappings with a colon (:) that is not followed by a whitespace will get one
2018-09-28 16:33:35 +00:00
```diff
2018-10-12 23:15:00 +00:00
-key:value
+key: value
2018-09-28 16:33:35 +00:00
```
## Twig
### `SimpleFunctionAndFilterRector`
- class: `Rector\Twig\Rector\SimpleFunctionAndFilterRector`
Changes Twig_Function_Method to Twig_SimpleFunction calls in TwigExtension.
```diff
class SomeExtension extends Twig_Extension
{
public function getFunctions()
{
return [
- 'is_mobile' => new Twig_Function_Method($this, 'isMobile'),
+ new Twig_SimpleFunction('is_mobile', [$this, 'isMobile']),
];
}
- public function getFilters()
+ public function getFilteres()
{
return [
- 'is_mobile' => new Twig_Filter_Method($this, 'isMobile'),
+ new Twig_SimpleFilter('is_mobile', [$this, 'isMobile']),
];
}
}
```
2018-08-01 20:09:34 +00:00
---
## General
2018-08-01 20:09:34 +00:00
- [Annotation](#annotation)
- [Argument](#argument)
- [Assign](#assign)
- [Class_](#class_)
- [CodeQuality](#codequality)
- [Constant](#constant)
- [DependencyInjection](#dependencyinjection)
- [Function_](#function_)
- [Interface_](#interface_)
- [MagicDisclosure](#magicdisclosure)
- [MethodBody](#methodbody)
- [MethodCall](#methodcall)
- [Namespace_](#namespace_)
- [Property](#property)
2018-09-28 16:33:35 +00:00
- [Psr4](#psr4)
2018-08-01 20:09:34 +00:00
- [RepositoryAsService](#repositoryasservice)
2018-08-12 18:34:27 +00:00
- [StaticCall](#staticcall)
2018-08-01 20:09:34 +00:00
- [Typehint](#typehint)
- [ValueObjectRemover](#valueobjectremover)
- [Visibility](#visibility)
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
## Annotation
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
### `AnnotationReplacerRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Annotation\AnnotationReplacerRector`
2018-08-01 20:09:34 +00:00
Turns defined annotations above properties and methods to their new values.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Annotation\AnnotationReplacerRector:
$classToAnnotationMap:
PHPUnit\Framework\TestCase:
test: scenario
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
2018-08-10 19:07:08 +00:00
- /**
- * @test
+ /**
+ * @scenario
*/
2018-08-01 20:09:34 +00:00
public function someMethod()
{
}
}
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Argument
2018-08-01 20:09:34 +00:00
### `ArgumentAdderRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Argument\ArgumentAdderRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
This Rector adds new default arguments in calls of defined methods and class types.
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Argument\ArgumentAdderRector:
$argumentChangesByMethodAndType:
class: SomeClass
method: someMethod
position: 0
default_value: 'true'
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
2018-08-01 20:09:34 +00:00
```diff
$someObject = new SomeClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
```
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Argument\ArgumentAdderRector:
$argumentChangesByMethodAndType:
class: SomeClass
method: someMethod
position: 0
default_value: 'true'
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
class MyCustomClass extends SomeClass
2018-07-31 12:50:39 +00:00
{
2018-08-01 20:09:34 +00:00
- public function someMethod()
+ public function someMethod($value = true)
{
}
2018-07-31 12:50:39 +00:00
}
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
### `ArgumentRemoverRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Argument\ArgumentRemoverRector`
2018-08-01 20:09:34 +00:00
Removes defined arguments in defined methods and their calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Argument\ArgumentRemoverRector:
$argumentChangesByMethodAndType:
class: SomeClass
method: someMethod
position: 0
value: 'true'
```
2018-08-01 20:09:34 +00:00
```diff
2018-08-01 20:09:34 +00:00
$someObject = new SomeClass;
-$someObject->someMethod(true);
+$someObject->someMethod();'
```
2018-08-01 20:09:34 +00:00
### `ArgumentDefaultValueReplacerRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Argument\ArgumentDefaultValueReplacerRector`
2018-08-01 20:09:34 +00:00
Replaces defined map of arguments in defined methods and their calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Argument\ArgumentDefaultValueReplacerRector:
$argumentChangesByMethodAndType:
class: SomeClass
method: someMethod
position: 0
before: 'SomeClass::OLD_CONSTANT'
after: 'false'
```
2018-08-01 20:09:34 +00:00
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);'
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Assign
2018-08-01 20:09:34 +00:00
### `PropertyAssignToMethodCallRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Assign\PropertyAssignToMethodCallRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Turns property assign of specific type and property name to method call
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Assign\PropertyAssignToMethodCallRector:
$types:
- SomeClass
$oldPropertyName: oldProperty
$newMethodName: newMethodCall
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$someObject = new SomeClass;
-$someObject->oldProperty = false;
+$someObject = new SomeClass;
+$someObject->newMethodCall(false);
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Class_
2018-08-01 20:09:34 +00:00
### `ClassReplacerRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Class_\ClassReplacerRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Replaces defined classes by new ones.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Class_\ClassReplacerRector:
$oldToNewClasses:
SomeOldClass: SomeNewClass
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
2018-07-31 12:50:39 +00:00
```diff
-$value = new SomeOldClass;
+$value = new SomeNewClass;
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
### `ParentClassToTraitsRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Class_\ParentClassToTraitsRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Replaces parent class to specific traits
```yaml
services:
Rector\Rector\Class_\ParentClassToTraitsRector:
$parentClassToTraits:
Nette\Object:
- Nette\SmartObject
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-class SomeClass extends Nette\Object
+class SomeClass
{
+ use Nette\SmartObject;
}
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## CodeQuality
2018-10-12 23:15:00 +00:00
### `CombinedAssignRector`
- class: `Rector\CodeQuality\Rector\CombinedAssignRector`
2018-10-12 23:15:00 +00:00
Simplify $value = $value + 5; assignments to shorter ones
```diff
-$value = $value + 5;
+$value += 5;
```
2018-08-01 20:09:34 +00:00
### `InArrayAndArrayKeysToArrayKeyExistsRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\CodeQuality\Rector\InArrayAndArrayKeysToArrayKeyExistsRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Simplify `in_array` and `array_keys` functions combination into `array_key_exists` when `array_keys` has one argument only
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```diff
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
### `UnnecessaryTernaryExpressionRector`
- class: `Rector\CodeQuality\Rector\UnnecessaryTernaryExpressionRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Remove unnecessary ternary expressions.
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$foo === $bar ? true : false;
+$foo === $bar;
2018-05-04 22:30:32 +00:00
```
2018-10-12 23:15:00 +00:00
### `SimplifyConditionsRector`
- class: `Rector\CodeQuality\Rector\SimplifyConditionsRector`
2018-10-12 23:15:00 +00:00
Simplify conditions
```diff
-if (! ($foo !== 'bar')) {...
+if ($foo === 'bar') {...
```
2018-08-01 20:09:34 +00:00
## Constant
### `RenameClassConstantsUseToStringsRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Constant\RenameClassConstantsUseToStringsRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Replaces constant by value
```yaml
services:
Rector\Rector\Constant\RenameClassConstantsUseToStringsRector:
$class: Nette\Configurator
$oldConstantToNewValue:
DEVELOPMENT: development
PRODUCTION: production
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$value === Nette\Configurator::DEVELOPMENT
+$value === "development"
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
### `ClassConstantReplacerRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Constant\ClassConstantReplacerRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Replaces defined class constants in their calls.
```yaml
services:
Rector\Rector\Constant\ClassConstantReplacerRector:
$oldToNewConstantsByClass:
SomeClass:
OLD_CONSTANT: NEW_CONSTANT
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$value = SomeClass::OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## DependencyInjection
2018-10-12 23:15:00 +00:00
### `ReplaceVariableByPropertyFetchRector`
- class: `Rector\Rector\Architecture\DependencyInjection\ReplaceVariableByPropertyFetchRector`
Turns variable in controller action to property fetch, as follow up to action injection variable to property change.
```diff
final class SomeController
{
/**
* @var ProductRepository
*/
private $productRepository;
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
public function default()
{
- $products = $productRepository->fetchAll();
+ $products = $this->productRepository->fetchAll();
}
}
```
2018-08-01 20:09:34 +00:00
### `AnnotatedPropertyInjectToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector`
Turns non-private properties with @annotation to private properties and constructor injection
```yaml
services:
Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector:
$annotation: inject
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
/**
* @var SomeService
- * @inject
*/
-public $someService;
+private $someService;
+
+public function __construct(SomeService $someService)
+{
+ $this->someService = $someService;
+}
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
### `ActionInjectionToConstructorInjectionRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Architecture\DependencyInjection\ActionInjectionToConstructorInjectionRector`
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
Turns action injection in Controllers to constructor injection
2018-07-31 12:50:39 +00:00
```diff
2018-08-01 20:09:34 +00:00
final class SomeController
2018-07-31 12:50:39 +00:00
{
2018-08-01 20:09:34 +00:00
- public function default(ProductRepository $productRepository)
+ /**
+ * @var ProductRepository
+ */
+ private $productRepository;
+ public function __construct(ProductRepository $productRepository)
{
- $products = $productRepository->fetchAll();
+ $this->productRepository = $productRepository;
+ }
+
+ public function default()
+ {
+ $products = $this->productRepository->fetchAll();
}
2018-07-31 12:50:39 +00:00
}
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
## Function_
2018-10-12 23:15:00 +00:00
### `FunctionReplaceRector`
- class: `Rector\Rector\Function_\FunctionReplaceRector`
Turns defined function call new one.
```yaml
services:
Rector\Rector\Function_\FunctionReplaceRector:
$functionToStaticCall:
view: Laravel\Templating\render
```
```diff
-view("...", []);
+Laravel\Templating\render("...", []);
```
2018-08-01 20:09:34 +00:00
### `FunctionToMethodCallRector`
- class: `Rector\Rector\Function_\FunctionToMethodCallRector`
Turns defined function calls to local method calls.
```yaml
services:
Rector\Rector\Function_\FunctionToMethodCallRector:
$functionToMethodCall:
view:
- this
- render
```
```diff
-view("...", []);
+$this->render("...", []);
```
2018-08-12 18:34:27 +00:00
### `FunctionToStaticCallRector`
- class: `Rector\Rector\Function_\FunctionToStaticCallRector`
Turns defined function call to static method call.
```yaml
services:
Rector\Rector\Function_\FunctionToStaticCallRector:
$functionToStaticCall:
view:
- SomeStaticClass
- render
```
```diff
-view("...", []);
+SomeClass::render("...", []);
```
2018-08-01 20:09:34 +00:00
## Interface_
### `MergeInterfacesRector`
- class: `Rector\Rector\Interface_\MergeInterfacesRector`
Merges old interface to a new one, that already has its methods
```yaml
services:
Rector\Rector\Interface_\MergeInterfacesRector:
$oldToNewInterfaces:
SomeOldInterface: SomeInterface
```
```diff
-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
{
}
```
2018-08-01 20:09:34 +00:00
## MagicDisclosure
### `ToStringToMethodCallRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\MagicDisclosure\ToStringToMethodCallRector`
2018-07-31 12:50:39 +00:00
2018-08-10 19:07:08 +00:00
Turns defined code uses of "__toString()" method to specific method calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\MagicDisclosure\ToStringToMethodCallRector:
$typeToMethodCalls:
SomeObject:
toString: getPath
```
2018-07-31 12:50:39 +00:00
```diff
2018-08-10 19:07:08 +00:00
$someValue = new SomeObject;
2018-08-01 20:09:34 +00:00
-$result = (string) $someValue;
-$result = $someValue->__toString();
+$result = $someValue->someMethod();
+$result = $someValue->someMethod();
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
### `GetAndSetToMethodCallRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector`
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
Turns defined `__get`/`__set` to specific method calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
$typeToMethodCalls:
SomeContainer:
set: addService
```
2018-07-31 12:50:39 +00:00
```diff
2018-08-10 19:07:08 +00:00
$container = new SomeContainer;
2018-08-01 20:09:34 +00:00
-$container->someService = $someService;
+$container->setService("someService", $someService);
```
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
$typeToMethodCalls:
SomeContainer:
get: getService
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
2018-08-01 20:09:34 +00:00
```diff
2018-08-10 19:07:08 +00:00
$container = new SomeContainer;
2018-08-01 20:09:34 +00:00
-$someService = $container->someService;
+$someService = $container->getService("someService");
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
### `UnsetAndIssetToMethodCallRector`
- class: `Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector`
Turns defined `__isset`/`__unset` calls to specific method calls.
```yaml
services:
Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
$typeToMethodCalls:
Nette\DI\Container:
isset: hasService
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-isset($container["someKey"]);
+$container->hasService("someKey");
```
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
-
$typeToMethodCalls:
Nette\DI\Container:
unset: removeService
```
2018-08-01 20:09:34 +00:00
2018-08-01 20:09:34 +00:00
```diff
-unset($container["someKey"])
+$container->removeService("someKey");
```
## MethodBody
### `FluentReplaceRector`
- class: `Rector\Rector\MethodBody\FluentReplaceRector`
2018-09-28 16:33:35 +00:00
Turns fluent interface calls to classic ones.
```yaml
services:
Rector\Rector\MethodBody\FluentReplaceRector:
$classesToDefluent:
- SomeClass
```
```diff
$someClass = new SomeClass();
-$someClass->someFunction()
- ->otherFunction();
+$someClass->someFunction();
+$someClass->otherFunction();
```
### `NormalToFluentRector`
- class: `Rector\Rector\MethodBody\NormalToFluentRector`
Turns fluent interface calls to classic ones.
```yaml
services:
Rector\Rector\MethodBody\NormalToFluentRector:
$fluentMethodsByType:
SomeClass:
- someFunction
- otherFunction
```
```diff
$someObject = new SomeClass();
-$someObject->someFunction();
-$someObject->otherFunction();
+$someObject->someFunction()
+ ->otherFunction();
```
### `ReturnThisRemoveRector`
- class: `Rector\Rector\MethodBody\ReturnThisRemoveRector`
Removes "return $this;" from *fluent interfaces* for specified classes.
```yaml
services:
Rector\Rector\MethodBody\ReturnThisRemoveRector:
$classesToDefluent:
- SomeClass
```
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
{
public function someFunction()
{
- return $this;
}
public function otherFunction()
{
- return $this;
}
}
2018-05-04 22:30:32 +00:00
```
## MethodCall
2018-10-12 23:15:00 +00:00
### `MethodNameReplacerRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Rector\MethodCall\MethodNameReplacerRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
Turns method names to new ones.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2018-10-12 23:15:00 +00:00
Rector\Rector\MethodCall\MethodNameReplacerRector:
$perClassOldToNewMethods:
SomeClass:
oldMethod: newMethod
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
```diff
2018-10-12 23:15:00 +00:00
$someObject = new SomeClass;
-$someObject->oldMethod();
+$someObject->newMethod();
2018-05-04 22:30:32 +00:00
```
2018-10-12 23:15:00 +00:00
### `MethodCallToAnotherMethodCallWithArgumentsRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector`
2018-10-12 23:15:00 +00:00
Turns old method call with specfici type to new one with arguments
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2018-10-12 23:15:00 +00:00
Rector\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector:
$serviceDefinitionClass: Nette\DI\ServiceDefinition
$oldMethod: setInject
$newMethod: addTag
$newMethodArguments:
- inject
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2018-10-12 23:15:00 +00:00
$serviceDefinition = new Nette\DI\ServiceDefinition;
-$serviceDefinition->setInject();
+$serviceDefinition->addTag('inject');
2018-05-04 22:30:32 +00:00
```
2018-08-10 19:07:08 +00:00
### `StaticMethodNameReplacerRector`
- class: `Rector\Rector\MethodCall\StaticMethodNameReplacerRector`
Turns method names to new ones.
2018-08-01 20:09:34 +00:00
```yaml
services:
2018-08-10 19:07:08 +00:00
Rector\Rector\MethodCall\StaticMethodNameReplacerRector:
2018-08-01 20:09:34 +00:00
$perClassOldToNewMethods:
SomeClass:
oldMethod:
- SomeClass
- newMethod
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-SomeClass::oldStaticMethod();
+SomeClass::newStaticMethod();
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Namespace_
### `NamespaceReplacerRector`
- class: `Rector\Rector\Namespace_\NamespaceReplacerRector`
2018-08-01 20:09:34 +00:00
Replaces old namespace by new one.
2018-05-05 00:04:41 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Namespace_\NamespaceReplacerRector:
$oldToNewNamespaces:
SomeOldNamespace: SomeNewNamespace
```
2018-05-05 00:04:41 +00:00
2018-08-01 20:09:34 +00:00
2018-08-01 20:09:34 +00:00
```diff
-$someObject = new SomeOldNamespace\SomeClass;
+$someObject = new SomeNewNamespace\SomeClass;
2018-05-05 00:04:41 +00:00
```
2018-08-01 20:09:34 +00:00
### `PseudoNamespaceToNamespaceRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Namespace_\PseudoNamespaceToNamespaceRector`
2018-08-01 20:09:34 +00:00
Replaces defined Pseudo_Namespaces by Namespace\Ones.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Namespace_\PseudoNamespaceToNamespaceRector:
2018-08-10 19:07:08 +00:00
$pseudoNamespacePrefixes:
2018-08-01 20:09:34 +00:00
- Some_
2018-08-10 19:07:08 +00:00
$excludedClasses: { }
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-10 19:07:08 +00:00
-$someService = Some_Object;
+$someService = Some\Object;
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## Property
2018-08-01 20:09:34 +00:00
### `PropertyToMethodRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Property\PropertyToMethodRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Replaces properties assign calls be defined methods.
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Property\PropertyToMethodRector:
$perClassPropertyToMethods:
SomeObject:
property:
2018-09-28 16:33:35 +00:00
get: getProperty
set: setProperty
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```diff
-$result = $object->property;
-$object->property = $value;
+$result = $object->getProperty();
+$object->setProperty($value);
```
2018-09-28 16:33:35 +00:00
```yaml
services:
Rector\Rector\Property\PropertyToMethodRector:
$perClassPropertyToMethods:
SomeObject:
property:
get:
method: getConfig
arguments:
- someArg
```
```diff
-$result = $object->property;
+$result = $object->getProperty('someArg');
```
2018-08-01 20:09:34 +00:00
### `PropertyNameReplacerRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Property\PropertyNameReplacerRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Replaces defined old properties by new ones.
```yaml
services:
Rector\Rector\Property\PropertyNameReplacerRector:
$perClassOldToNewProperties:
SomeClass:
someOldProperty: someNewProperty
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
-$someObject->someOldProperty;
+$someObject->someNewProperty;
2018-05-04 22:30:32 +00:00
```
2018-09-28 16:33:35 +00:00
## Psr4
### `MultipleClassFileToPsr4ClassesRector`
- class: `Rector\Rector\Psr4\MultipleClassFileToPsr4ClassesRector`
Turns namespaced classes in one file to standalone PSR-4 classes.
```diff
+// new file: "app/Exceptions/FirstException.php"
namespace App\Exceptions;
use Exception;
final class FirstException extends Exception
{
}
+
+// new file: "app/Exceptions/SecondException.php"
+namespace App\Exceptions;
+
+use Exception;
final class SecondException extends Exception
{
}
```
## RepositoryAsService
### `ServiceLocatorToDIRector`
- class: `Rector\Rector\Architecture\RepositoryAsService\ServiceLocatorToDIRector`
2018-05-04 22:30:32 +00:00
Turns "$this->getRepository()" in Symfony Controller to constructor injection and private property access.
2018-05-04 22:30:32 +00:00
```diff
class ProductController extends Controller
2018-07-31 12:50:39 +00:00
{
+ /**
+ * @var ProductRepository
+ */
+ private $productRepository;
+
+ public function __construct(ProductRepository $productRepository)
2018-07-31 12:50:39 +00:00
+ {
+ $this->productRepository = $productRepository;
2018-07-31 12:50:39 +00:00
+ }
+
public function someAction()
2018-07-31 12:50:39 +00:00
{
$entityManager = $this->getDoctrine()->getManager();
- $entityManager->getRepository('SomethingBundle:Product')->findSomething(...);
+ $this->productRepository->findSomething(...);
2018-07-31 12:50:39 +00:00
}
}
2018-05-04 22:30:32 +00:00
```
2018-10-12 23:15:00 +00:00
### `ReplaceParentRepositoryCallsByRepositoryPropertyRector`
- class: `Rector\Rector\Architecture\RepositoryAsService\ReplaceParentRepositoryCallsByRepositoryPropertyRector`
Handles method calls in child of Doctrine EntityRepository and moves them to "$this->repository" property.
```diff
<?php
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
{
public function someMethod()
{
- return $this->findAll();
+ return $this->repository->findAll();
}
}
```
### `MoveRepositoryFromParentToConstructorRector`
- class: `Rector\Rector\Architecture\RepositoryAsService\MoveRepositoryFromParentToConstructorRector`
2018-05-04 22:30:32 +00:00
Turns parent EntityRepository class to constructor dependency
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Architecture\RepositoryAsService\MoveRepositoryFromParentToConstructorRector:
$entityRepositoryClass: Doctrine\ORM\EntityRepository
$entityManagerClass: Doctrine\ORM\EntityManager
```
2018-05-04 22:30:32 +00:00
```diff
namespace App\Repository;
2018-05-04 22:30:32 +00:00
+use App\Entity\Post;
use Doctrine\ORM\EntityRepository;
2018-05-04 22:30:32 +00:00
-final class PostRepository extends EntityRepository
+final class PostRepository
{
+ /**
+ * @var \Doctrine\ORM\EntityRepository
+ */
+ private $repository;
+ public function __construct(\Doctrine\ORM\EntityManager $entityManager)
+ {
+ $this->repository = $entityManager->getRepository(\App\Entity\Post::class);
+ }
}
2018-07-31 12:50:39 +00:00
```
2018-08-12 18:34:27 +00:00
## StaticCall
### `StaticCallToFunctionRector`
- class: `Rector\Rector\StaticCall\StaticCallToFunctionRector`
Turns static call to function call.
```yaml
services:
Rector\Rector\StaticCall\StaticCallToFunctionRector:
$staticCallToFunction:
'OldClass::oldMethod': new_function
```
```diff
-OldClass::oldMethod("args");
+new_function("args");
```
2018-08-01 20:09:34 +00:00
## Typehint
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
### `ReturnTypehintRector`
2018-08-01 20:09:34 +00:00
- class: `Rector\Rector\Typehint\ReturnTypehintRector`
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
Changes defined return typehint of method and class.
```yaml
services:
Rector\Rector\Typehint\ReturnTypehintRector:
$typehintForMethodByClass:
SomeClass:
getData: array
```
2018-05-04 22:30:32 +00:00
```diff
2018-08-01 20:09:34 +00:00
class SomeClass
{
- public getData();
+ public getData(): array;
}
```
### `ParentTypehintedArgumentRector`
- class: `Rector\Rector\Typehint\ParentTypehintedArgumentRector`
Changes defined parent class typehints.
```yaml
services:
Rector\Rector\Typehint\ParentTypehintedArgumentRector:
$typehintForArgumentByMethodAndClass:
SomeInterface:
read:
$content: string
```
```diff
interface SomeInterface
{
public read(string $content);
}
class SomeClass implements SomeInterface
{
2018-08-01 20:09:34 +00:00
- public read($content);
+ public read(string $content);
}
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
## ValueObjectRemover
### `ValueObjectRemoverDocBlockRector`
- class: `Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverDocBlockRector`
2018-08-01 20:09:34 +00:00
Turns defined value object to simple types in doc blocks
```yaml
services:
Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverDocBlockRector:
2018-08-01 20:09:34 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
```diff
/**
- * @var ValueObject|null
+ * @var string|null
*/
private $name;
```
```yaml
services:
Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverDocBlockRector:
2018-08-01 20:09:34 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
```diff
-/** @var ValueObject|null */
+/** @var string|null */
$name;
```
### `ValueObjectRemoverRector`
- class: `Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverRector`
2018-08-01 20:09:34 +00:00
Remove values objects and use directly the value.
```yaml
services:
Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverRector:
2018-08-01 20:09:34 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
```diff
-$name = new ValueObject("name");
+$name = "name";
```
```yaml
services:
Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverRector:
2018-08-01 20:09:34 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
```diff
-function someFunction(ValueObject $name) { }
+function someFunction(string $name) { }
```
```yaml
services:
Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverRector:
2018-08-01 20:09:34 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
```diff
-function someFunction(): ValueObject { }
+function someFunction(): string { }
```
```yaml
services:
Rector\DomainDrivenDesign\Rector\ValueObjectRemover\ValueObjectRemoverRector:
2018-08-01 20:09:34 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
```diff
-function someFunction(): ?ValueObject { }
+function someFunction(): ?string { }
```
## Visibility
2018-10-12 23:15:00 +00:00
### `ChangePropertyVisibilityRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Rector\Visibility\ChangePropertyVisibilityRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
Change visibility of property from parent class.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2018-10-12 23:15:00 +00:00
Rector\Rector\Visibility\ChangePropertyVisibilityRector:
$propertyToVisibilityByClass:
2018-08-01 20:09:34 +00:00
FrameworkClass:
2018-10-12 23:15:00 +00:00
someProperty: protected
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
```diff
class FrameworkClass
{
2018-10-12 23:15:00 +00:00
protected $someProperty;
}
2018-07-31 06:38:48 +00:00
class MyClass extends FrameworkClass
{
2018-10-12 23:15:00 +00:00
- public $someProperty;
+ protected $someProperty;
}
2018-07-31 06:38:48 +00:00
```
2018-10-12 23:15:00 +00:00
### `ChangeMethodVisibilityRector`
2018-10-12 23:15:00 +00:00
- class: `Rector\Rector\Visibility\ChangeMethodVisibilityRector`
2018-05-04 22:30:32 +00:00
2018-10-12 23:15:00 +00:00
Change visibility of method from parent class.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2018-10-12 23:15:00 +00:00
Rector\Rector\Visibility\ChangeMethodVisibilityRector:
$methodToVisibilityByClass:
2018-08-01 20:09:34 +00:00
FrameworkClass:
2018-10-12 23:15:00 +00:00
someMethod: protected
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
```diff
class FrameworkClass
{
2018-10-12 23:15:00 +00:00
protected someMethod()
{
}
}
2018-05-04 22:30:32 +00:00
class MyClass extends FrameworkClass
2018-07-31 12:50:39 +00:00
{
2018-10-12 23:15:00 +00:00
- public someMethod()
+ protected someMethod()
{
}
2018-07-31 12:50:39 +00:00
}
2018-05-04 22:30:32 +00:00
```
### `ChangeConstantVisibilityRector`
- class: `Rector\Rector\Visibility\ChangeConstantVisibilityRector`
2018-05-04 22:30:32 +00:00
Change visibility of constant from parent class.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
Rector\Rector\Visibility\ChangeConstantVisibilityRector:
Rector\Tests\Rector\Visibility\ChangeConstantVisibilityRector\Source\ParentObject:
$constantToVisibilityByClass:
SOME_CONSTANT: protected
```
2018-05-04 22:30:32 +00:00
```diff
class FrameworkClass
{
protected const SOME_CONSTANT = 1;
}
2018-07-31 12:50:39 +00:00
class MyClass extends FrameworkClass
{
- public const SOME_CONSTANT = 1;
+ protected const SOME_CONSTANT = 1;
}
2018-05-04 22:30:32 +00:00
```