rector/docs/AllRectorsOverview.md

1296 lines
30 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-07-31 21:47:59 +00:00
- [PHPUnit](#phpunit)
- [PHPUnit\SpecificMethod](#phpunitspecificmethod)
- [Sylius\Review](#syliusreview)
- [Symfony\HttpKernel](#symfonyhttpkernel)
- [Symfony\Controller](#symfonycontroller)
- [Symfony\Validator](#symfonyvalidator)
- [Symfony\Console](#symfonyconsole)
- [Symfony\Yaml](#symfonyyaml)
- [Symfony\DependencyInjection](#symfonydependencyinjection)
- [Symfony\VarDumper](#symfonyvardumper)
- [Symfony\FrameworkBundle](#symfonyframeworkbundle)
- [Symfony\Form](#symfonyform)
- [Symfony\Process](#symfonyprocess)
- [Doctrine](#doctrine)
- [PhpParser](#phpparser)
- [Sensio\FrameworkExtraBundle](#sensioframeworkextrabundle)
## PHPUnit
### `ExceptionAnnotationRector`
- class: `Rector\PHPUnit\Rector\ExceptionAnnotationRector`
2018-05-05 12:48:33 +00:00
Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
2018-05-05 12:48:33 +00:00
```diff
-/**
- * @expectedException Exception
- * @expectedExceptionMessage Message
- */
public function test()
{
+ $this->expectException('Exception');
+ $this->expectExceptionMessage('Message');
// tested code
}
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
```
### `ArrayToYieldDataProviderRector`
- class: `Rector\PHPUnit\Rector\ArrayToYieldDataProviderRector`
2018-07-31 12:50:39 +00:00
Turns method data providers in PHPUnit from arrays to yield
2018-07-31 12:50:39 +00:00
```diff
/**
- * @return mixed[]
*/
-public function provide(): array
+public function provide(): Iterator
2018-05-05 12:48:33 +00:00
{
- return [
- ['item']
- ]
+ yield ['item'];
2018-07-31 12:50:39 +00:00
}
```
### `GetMockRector`
- class: `Rector\PHPUnit\Rector\GetMockRector`
2018-07-31 12:50:39 +00:00
Turns getMock*() methods to createMock()
2018-07-31 12:50:39 +00:00
```diff
-$this->getMock("Class")
+$this->createMock("Class")
2018-07-31 12:50:39 +00:00
-$this->getMockWithoutInvokingTheOriginalConstructor("Class")
+$this->createMock("Class"
2018-07-31 12:50:39 +00:00
```
## PHPUnit\SpecificMethod
### `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");
-$this->assertFalse(!$foo, "message");
+$this->assertTrue($foo, "message");
2018-07-31 12:50:39 +00:00
```
### `AssertComparisonToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertComparisonToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
Turns comparison operations to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertTrue($foo === $bar, "message");
+$this->assertSame($bar, $foo, "message");
2018-07-31 12:50:39 +00:00
-$this->assertFalse($foo >= $bar, "message");
+$this->assertLessThanOrEqual($bar, $foo, "message");
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");
-$this->assertFalse(property_exists(new Class, "property"), "message");
+$this->assertClassNotHasAttribute("property", "Class", "message");
2018-07-31 12:50:39 +00:00
```
### `AssertTrueFalseInternalTypeToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertTrueFalseInternalTypeToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
Turns true/false with internal type comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertTrue(is_{internal_type}($anything), "message");
+$this->assertInternalType({internal_type}, $anything, "message");
-$this->assertFalse(is_{internal_type}($anything), "message");
+$this->assertNotInternalType({internal_type}, $anything, "message");
2018-07-31 12:50:39 +00:00
```
### `AssertIssetToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertIssetToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
Turns isset comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertTrue(isset($anything->foo));
+$this->assertFalse(isset($anything["foo"]), "message");
2018-07-31 12:50:39 +00:00
-$this->assertObjectHasAttribute("foo", $anything);
+$this->assertArrayNotHasKey("foo", $anything, "message");
2018-07-31 12:50:39 +00:00
```
### `AssertFalseStrposToContainsRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertFalseStrposToContainsRector`
2018-07-31 12:50:39 +00:00
Turns `strpos`/`stripos` comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertFalse(strpos($anything, "foo"), "message");
+$this->assertNotContains("foo", $anything, "message");
-$this->assertNotFalse(stripos($anything, "foo"), "message");
+$this->assertContains("foo", $anything, "message");
2018-07-31 12:50:39 +00:00
```
### `AssertSameBoolNullToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertSameBoolNullToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertSame(null, $anything);
+$this->assertNull($anything);
-$this->assertNotSame(false, $anything);
+$this->assertNotFalse($anything);
2018-07-31 12:50:39 +00:00
```
### `AssertCompareToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertCompareToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
Turns vague php-only method in PHPUnit TestCase to more specific
2018-07-31 12:50:39 +00:00
```diff
-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
2018-07-31 12:50:39 +00:00
-$this->assertSame($value, {function}($anything), "message");
+$this->assert{function}($value, $anything, "message\");
-$this->assertEquals($value, {function}($anything), "message");
+$this->assert{function}($value, $anything, "message\");
2018-07-31 12:50:39 +00:00
-$this->assertNotSame($value, {function}($anything), "message");
+$this->assertNot{function}($value, $anything, "message")
2018-07-31 12:50:39 +00:00
-$this->assertNotEquals($value, {function}($anything), "message");
+$this->assertNot{function}($value, $anything, "message")
2018-07-31 12:50:39 +00:00
```
### `AssertRegExpRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertRegExpRector`
2018-07-31 12:50:39 +00:00
Turns `preg_match` comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertSame(1, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertRegExp("/^Message for ".*"\.$/", $string, $message);
-$this->assertEquals(false, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertNotRegExp("/^Message for ".*"\.$/", $string, $message);
2018-07-31 12:50:39 +00:00
```
### `AssertInstanceOfComparisonRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertInstanceOfComparisonRector`
2018-07-31 12:50:39 +00:00
Turns instanceof comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
```diff
-$this->assertTrue($foo instanceof Foo, "message");
+$this->assertFalse($foo instanceof Foo, "message");
2018-07-31 12:50:39 +00:00
-$this->assertInstanceOf("Foo", $foo, "message");
+$this->assertNotInstanceOf("Foo", $foo, "message");
2018-05-05 12:48:33 +00:00
```
### `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");
```
## 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
```
## Symfony\HttpKernel
### `GetRequestRector`
2018-07-31 12:50:39 +00:00
- class: `Rector\Symfony\Rector\HttpKernel\GetRequestRector`
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
2018-07-31 12:50:39 +00:00
```diff
+use Symfony\Component\HttpFoundation\Request;
+
class SomeController
{
- public function someAction()
+ public action(Request $request)
{
- $this->getRequest()->...();
+ $request->...();
}
}
```
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
```
## Symfony\Validator
### `ConstraintUrlOptionRector`
- class: `Rector\Symfony\Rector\Validator\ConstraintUrlOptionRector`
2018-07-31 12:50:39 +00:00
Turns true value to `Url::CHECK_DNS_TYPE_ANY` in Validator in Symfony.
2018-07-31 12:50:39 +00:00
```diff
-$constraint = new Url(["checkDNS" => true]);
+$constraint = new Url(["checkDNS" => Url::CHECK_DNS_TYPE_ANY]);
2018-07-31 12:50:39 +00:00
```
## Symfony\Console
### `ConsoleExceptionToErrorEventConstantRector`
2018-07-31 12:50:39 +00:00
- class: `Rector\Symfony\Rector\Console\ConsoleExceptionToErrorEventConstantRector`
Turns old event name with EXCEPTION to ERROR constant in Console in Symfony
2018-07-31 12:50:39 +00:00
```diff
-"console.exception"
+Symfony\Component\Console\ConsoleEvents::ERROR
2018-07-31 12:50:39 +00:00
-Symfony\Component\Console\ConsoleEvents::EXCEPTION
+Symfony\Component\Console\ConsoleEvents::ERROR
2018-07-31 12:50:39 +00:00
```
## Symfony\Yaml
### `SpaceBetweenKeyAndValueYamlRector`
- class: `Rector\Symfony\Rector\Yaml\SpaceBetweenKeyAndValueYamlRector`
2018-07-31 12:50:39 +00:00
Mappings with a colon (:) that is not followed by a whitespace will get one
2018-07-31 12:50:39 +00:00
```diff
-key:value
+key: value
```
2018-07-31 12:50:39 +00:00
### `SessionStrictTrueByDefaultYamlRector`
- class: `Rector\Symfony\Rector\Yaml\SessionStrictTrueByDefaultYamlRector`
session > use_strict_mode is true by default and can be removed
```diff
-session > use_strict_mode: true
+session:
2018-07-31 12:50:39 +00:00
```
## Symfony\DependencyInjection
### `ContainerBuilderCompileEnvArgumentRector`
- class: `Rector\Symfony\Rector\DependencyInjection\ContainerBuilderCompileEnvArgumentRector`
2018-07-31 12:50:39 +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
-$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
```
## Symfony\VarDumper
### `VarDumperTestTraitMethodArgsRector`
2018-05-05 12:48:33 +00:00
- class: `Rector\Symfony\Rector\VarDumper\VarDumperTestTraitMethodArgsRector`
2018-05-05 12:48:33 +00:00
Adds new `$format` argument in `VarDumperTestTrait->assertDumpEquals()` in Validator in Symfony.
2018-05-05 12:48:33 +00:00
```diff
-VarDumperTestTrait->assertDumpEquals($dump, $data, $mesage = "");
+VarDumperTestTrait->assertDumpEquals($dump, $data, $context = null, $mesage = "");
2018-05-05 12:48:33 +00:00
-VarDumperTestTrait->assertDumpMatchesFormat($dump, $format, $mesage = "");
+VarDumperTestTrait->assertDumpMatchesFormat($dump, $format, $context = null, $mesage = "");
2018-05-05 12:48:33 +00:00
```
## Symfony\FrameworkBundle
### `GetParameterToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\Symfony\Rector\FrameworkBundle\GetParameterToConstructorInjectionRector`
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
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
2018-05-04 22:30:32 +00:00
{
+ private $someParameter;
2018-07-31 12:50:39 +00:00
+
+ public function __construct($someParameter)
2018-07-31 12:50:39 +00:00
+ {
+ $this->someParameter = $someParameter;
+ }
+
public function someMethod()
{
- $this->getParameter('someParameter');
+ $this->someParameter;
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
}
```
### `ContainerGetToConstructorInjectionRector`
- class: `Rector\Symfony\Rector\FrameworkBundle\ContainerGetToConstructorInjectionRector`
2018-05-04 22:30:32 +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
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
2018-05-04 22:30:32 +00:00
{
+ public function __construct(SomeService $someService)
2018-05-04 22:30:32 +00:00
+ {
+ $this->someService = $someService;
2018-05-04 22:30:32 +00:00
+ }
+
public function someMethod()
2018-05-04 22:30:32 +00:00
{
// ...
- $this->getContainer()->get('some_service');
- $this->container->get('some_service');
+ $this->someService;
+ $this->someService;
2018-05-04 22:30:32 +00:00
}
}
```
## Symfony\Form
### `FormIsValidRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\Symfony\Rector\Form\FormIsValidRector`
Adds `$form->isSubmitted()` validatoin to all `$form->isValid()` calls in Form in Symfony
2018-05-04 22:30:32 +00:00
```diff
-if ($form->isValid()) { ... };
+if ($form->isSubmitted() && $form->isValid()) { ... };
```
2018-05-04 22:30:32 +00:00
### `OptionNameRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\Symfony\Rector\Form\OptionNameRector`
2018-05-04 22:30:32 +00:00
Turns old option names to new ones in FormTypes in Form in Symfony
```diff
-$builder->add("...", ["precision" => "...", "virtual" => "..."];
+$builder->add("...", ["scale" => "...", "inherit_data" => "..."];
```
### `StringFormTypeToClassRector`
- class: `Rector\Symfony\Rector\Form\StringFormTypeToClassRector`
Turns string Form Type references to their CONSTANT alternatives in FormTypes in Form in Symfony
```diff
-$form->add("name", "form.type.text");
+$form->add("name", \Symfony\Component\Form\Extension\Core\Type\TextType::class);
```
### `FormTypeGetParentRector`
- class: `Rector\Symfony\Rector\Form\FormTypeGetParentRector`
2018-05-04 22:30:32 +00:00
Turns string Form Type references to their CONSTANT alternatives in `getParent()` and `getExtendedType()` methods in Form in Symfony
2018-05-04 22:30:32 +00:00
```diff
-function getParent() { return "collection"; }
+function getParent() { return CollectionType::class; }
2018-05-04 22:30:32 +00:00
-function getExtendedType() { return "collection"; }
+function getExtendedType() { return CollectionType::class; }
2018-05-04 22:30:32 +00:00
```
## Symfony\Process
### `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
```
### `ProcessBuilderInstanceRector`
- class: `Rector\Symfony\Rector\Process\ProcessBuilderInstanceRector`
2018-05-04 22:30:32 +00:00
Turns `ProcessBuilder::instance()` to new ProcessBuilder in Process in Symfony. Part of multi-step Rector.
2018-05-04 22:30:32 +00:00
```diff
-$processBuilder = Symfony\Component\Process\ProcessBuilder::instance($args);
+$processBuilder = new Symfony\Component\Process\ProcessBuilder($args);
2018-05-04 22:30:32 +00:00
```
## Doctrine
### `AliasToClassRector`
- class: `Rector\Doctrine\Rector\AliasToClassRector`
2018-05-04 22:30:32 +00:00
Replaces doctrine alias with class.
2018-05-04 22:30:32 +00:00
```diff
-$em->getRepository("AppBundle:Post");
+$em->getRepository(\App\Entity\Post::class);
2018-05-04 22:30:32 +00:00
```
## PhpParser
### `RemoveNodeRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\PhpParser\Rector\RemoveNodeRector`
Turns integer return to remove node to constant in NodeVisitor of PHP-Parser
2018-05-04 22:30:32 +00:00
```diff
public function leaveNode()
{
- return false;
+ return NodeTraverser::REMOVE_NODE;
}
2018-05-04 22:30:32 +00:00
```
### `ParamAndStaticVarNameRector`
- class: `Rector\PhpParser\Rector\ParamAndStaticVarNameRector`
2018-05-04 22:30:32 +00:00
Turns old string `var` to `var->name` sub-variable in Node of PHP-Parser
2018-05-04 22:30:32 +00:00
```diff
-$paramNode->name;
+$paramNode->var->name;
2018-05-04 22:30:32 +00:00
-$staticVarNode->name;
+$staticVarNode->var->name;
```
### `IdentifierRector`
- class: `Rector\PhpParser\Rector\IdentifierRector`
2018-05-04 22:30:32 +00:00
Turns node string names to Identifier object in php-parser
2018-05-04 22:30:32 +00:00
```diff
$constNode = new \PhpParser\Node\Const_;
-$name = $constNode->name;
+$name = $constNode->name->toString();'
2018-05-04 22:30:32 +00:00
```
### `CatchAndClosureUseNameRector`
- class: `Rector\PhpParser\Rector\CatchAndClosureUseNameRector`
2018-05-04 22:30:32 +00:00
Turns `$catchNode->var` to its new `name` property in php-parser
2018-05-04 22:30:32 +00:00
```diff
-$catchNode->var;
+$catchNode->var->name
2018-05-04 22:30:32 +00:00
```
### `SetLineRector`
- class: `Rector\PhpParser\Rector\SetLineRector`
2018-05-04 22:30:32 +00:00
Turns standalone line method to attribute in Node of PHP-Parser
2018-05-04 22:30:32 +00:00
```diff
-$node->setLine(5);
+$node->setAttribute("line", 5);
2018-05-04 22:30:32 +00:00
```
### `UseWithAliasRector`
- class: `Rector\PhpParser\Rector\UseWithAliasRector`
2018-05-04 22:30:32 +00:00
Turns use property to method and `$node->alias` to last name in UseAlias Node of PHP-Parser
2018-05-04 22:30:32 +00:00
```diff
-$node->alias;
+$node->getAlias();
-$node->name->getLast();
+$node->alias
2018-05-04 22:30:32 +00:00
```
## Sensio\FrameworkExtraBundle
### `TemplateAnnotationRector`
- class: `Rector\Sensio\Rector\FrameworkExtraBundle\TemplateAnnotationRector`
2018-05-04 22:30:32 +00:00
Turns @Template annotation to explicit method call in Controller of FrameworkExtraBundle in Symfony
2018-05-04 22:30:32 +00:00
```diff
2018-07-31 12:50:39 +00:00
-/**
- * @Template()
2018-07-31 12:50:39 +00:00
- */
public function indexAction()
2018-07-31 12:50:39 +00:00
{
+ return $this->render("index.html.twig");
2018-07-31 12:50:39 +00:00
}
2018-05-04 22:30:32 +00:00
```
---
- [Assign](#assign)
- [Dynamic](#dynamic)
- [ValueObjectRemover](#valueobjectremover)
- [MethodCall](#methodcall)
- [MagicDisclosure](#magicdisclosure)
- [DependencyInjection](#dependencyinjection)
- [RepositoryAsService](#repositoryasservice)
- [Interface_](#interface_)
- [Visibility](#visibility)
- [CodeQuality](#codequality)
- [Constant](#constant)
- [Class_](#class_)
## Assign
### `PropertyAssignToMethodCallRector`
- class: `Rector\Rector\Assign\PropertyAssignToMethodCallRector`
Turns property assign of specific type and property name to method call
```diff
-$control->oldProperty = false;
+$control->newMethodCall(false);
```
## Dynamic
### `NamespaceReplacerRector`
- class: `Rector\Rector\Dynamic\NamespaceReplacerRector`
Replaces old namespace by new one.
```diff
-$someObject = new SomeOldNamespace\SomeClass;
+$someObject = new SomeNewNamespace\SomeClass;
```
### `ReturnTypehintRector`
- class: `Rector\Rector\Dynamic\ReturnTypehintRector`
2018-05-04 22:30:32 +00:00
Changes defined return typehint of method and class.
2018-05-04 22:30:32 +00:00
```diff
class SomeClass
{
- public getData();
+ public getData(): array;
}
2018-05-04 22:30:32 +00:00
```
### `FluentReplaceRector`
- class: `Rector\Rector\Dynamic\FluentReplaceRector`
2018-05-04 22:30:32 +00:00
Turns fluent interfaces to classic ones.
2018-05-04 22:30:32 +00:00
```diff
class SomeClass
{
public function someFunction()
{
- return $this;
}
2018-05-04 22:30:32 +00:00
public function otherFunction()
{
- return $this;
}
}
$someClass = new SomeClass();
- $someClass->someFunction()
- ->otherFunction();
+ $someClass->someFunction();
+ $someClass->otherFunction();
2018-05-04 22:30:32 +00:00
```
### `FunctionToMethodCallRector`
- class: `Rector\Rector\Dynamic\FunctionToMethodCallRector`
2018-05-04 22:30:32 +00:00
Turns defined function calls to local method calls.
2018-05-04 22:30:32 +00:00
```diff
-view("...", []);
+$this->render("...", []);
2018-05-04 22:30:32 +00:00
```
### `ArgumentAdderRector`
- class: `Rector\Rector\Dynamic\ArgumentAdderRector`
2018-05-04 22:30:32 +00:00
This Rector adds new default arguments in calls of defined methods and class types.
2018-05-04 22:30:32 +00:00
```diff
$someObject = new SomeClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
2018-05-04 22:30:32 +00:00
class MyCustomClass extends SomeClass
{
- public function someMethod()
+ public function someMethod($value = true)
{
}
}
2018-07-31 12:50:39 +00:00
```
### `ClassReplacerRector`
- class: `Rector\Rector\Class_\ClassReplacerRector`
2018-07-31 12:50:39 +00:00
Replaces defined classes by new ones.
2018-07-31 12:50:39 +00:00
```diff
-$value = new SomeOldClass;
+$value = new SomeNewClass;
2018-05-04 22:30:32 +00:00
```
### `PropertyToMethodRector`
- class: `Rector\Rector\Property\PropertyToMethodRector`
2018-05-04 22:30:32 +00:00
Replaces properties assign calls be defined methods.
2018-05-04 22:30:32 +00:00
```diff
-$result = $object->property;
-$object->property = $value;
+$result = $object->getProperty();
+$object->setProperty($value);
2018-05-04 22:30:32 +00:00
```
### `MethodNameReplacerRector`
- class: `Rector\Rector\Dynamic\MethodNameReplacerRector`
2018-05-04 22:30:32 +00:00
Turns method names to new ones.
2018-05-04 22:30:32 +00:00
```diff
$someObject = new SomeClass;
-$someObject->oldMethod();
+$someObject->newMethod();
2018-05-04 22:30:32 +00:00
-SomeClass::oldStaticMethod();
+SomeClass::newStaticMethod();
2018-05-04 22:30:32 +00:00
```
### `PropertyNameReplacerRector`
- class: `Rector\Rector\Property\PropertyNameReplacerRector`
2018-05-04 22:30:32 +00:00
Replaces defined old properties by new ones.
2018-05-04 22:30:32 +00:00
```diff
-$someObject->someOldProperty;
+$someObject->someNewProperty;
2018-05-04 22:30:32 +00:00
```
### `ArgumentRemoverRector`
- class: `Rector\Rector\Dynamic\ArgumentRemoverRector`
2018-05-04 22:30:32 +00:00
Removes defined arguments in defined methods and their calls.
2018-05-04 22:30:32 +00:00
```diff
$someObject = new SomeClass;
-$someObject->someMethod(true);
+$someObject->someMethod();'
2018-05-04 22:30:32 +00:00
```
### `ArgumentDefaultValueReplacerRector`
- class: `Rector\Rector\Dynamic\ArgumentDefaultValueReplacerRector`
2018-05-04 22:30:32 +00:00
Replaces defined map of arguments in defined methods and their calls.
2018-05-04 22:30:32 +00:00
```diff
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);'
2018-05-04 22:30:32 +00:00
```
### `AnnotationReplacerRector`
- class: `Rector\Rector\Dynamic\AnnotationReplacerRector`
2018-05-04 22:30:32 +00:00
Turns defined annotations above properties and methods to their new values.
2018-05-04 22:30:32 +00:00
```diff
-/** @test */
+/** @scenario */
public function someMethod() {};
2018-05-04 22:30:32 +00:00
```
### `PseudoNamespaceToNamespaceRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector`
2018-05-04 22:30:32 +00:00
Replaces defined Pseudo_Namespaces by Namespace\Ones.
2018-05-04 22:30:32 +00:00
```diff
-$someServie = Some_Object;
+$someServie = Some\Object;
2018-05-04 22:30:32 +00:00
```
### `ParentTypehintedArgumentRector`
- class: `Rector\Rector\Dynamic\ParentTypehintedArgumentRector`
2018-07-31 12:50:39 +00:00
Changes defined parent class typehints.
2018-07-31 12:50:39 +00:00
```diff
interface SomeInterface
2018-07-31 12:50:39 +00:00
{
public read(string $content);
2018-07-31 12:50:39 +00:00
}
class SomeClass implements SomeInterface
{
- public read($content);
+ public read(string $content);
}
```
### `ClassConstantReplacerRector`
- class: `Rector\Rector\Dynamic\ClassConstantReplacerRector`
2018-07-31 12:50:39 +00:00
Replaces defined class constants in their calls.
2018-07-31 12:50:39 +00:00
```diff
-$value = SomeClass::OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
2018-07-31 12:50:39 +00:00
```
## ValueObjectRemover
### `ValueObjectRemoverDocBlockRector`
2018-07-31 12:50:39 +00:00
- class: `Rector\Rector\Dynamic\ValueObjectRemover\ValueObjectRemoverDocBlockRector`
Turns defined value object to simple types in doc blocks
2018-07-31 12:50:39 +00:00
```diff
/**
- * @var ValueObject|null
+ * @var string|null
*/
private $name;
-/** @var ValueObject|null */
+/** @var string|null */
$name;
2018-07-31 12:50:39 +00:00
```
### `ValueObjectRemoverRector`
- class: `Rector\Rector\Dynamic\ValueObjectRemover\ValueObjectRemoverRector`
2018-05-04 22:30:32 +00:00
Remove values objects and use directly the value.
2018-05-04 22:30:32 +00:00
```diff
-$name = new ValueObject("name");
+$name = "name";
-function someFunction(ValueObject $name) { }
+function someFunction(string $name) { }
-function someFunction(): ValueObject { }
+function someFunction(): string { }
-function someFunction(): ?ValueObject { }
+function someFunction(): ?string { }
2018-05-04 22:30:32 +00:00
```
## MethodCall
### `MethodCallToAnotherMethodCallWithArgumentsRector`
- class: `Rector\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector`
2018-05-04 22:30:32 +00:00
Turns old method call with specfici type to new one with arguments
2018-05-04 22:30:32 +00:00
```diff
$serviceDefinition = new Nette\DI\ServiceDefinition;
-$serviceDefinition->setInject();
+$serviceDefinition->addTag('inject');
2018-05-04 22:30:32 +00:00
```
## MagicDisclosure
### `ToStringToMethodCallRector`
- class: `Rector\Rector\MagicDisclosure\ToStringToMethodCallRector`
2018-05-04 22:30:32 +00:00
Turns defined __toString() to specific method calls.
2018-05-04 22:30:32 +00:00
```diff
-$result = (string) $someValue;
+$result = $someValue->someMethod();
2018-07-31 12:50:39 +00:00
-$result = $someValue->__toString();
+$result = $someValue->someMethod();
2018-05-04 22:30:32 +00:00
```
### `GetAndSetToMethodCallRector`
- class: `Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector`
2018-05-04 22:30:32 +00:00
Turns defined `__get`/`__set` to specific method calls.
2018-05-04 22:30:32 +00:00
```diff
-$someService = $container->someService;
+$someService = $container->getService("someService");
-$container->someService = $someService;
+$container->setService("someService", $someService);
2018-05-04 22:30:32 +00:00
```
### `UnsetAndIssetToMethodCallRector`
- class: `Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector`
2018-05-05 00:04:41 +00:00
Turns defined `__isset`/`__unset` calls to specific method calls.
2018-05-05 00:04:41 +00:00
```diff
-isset($container["someKey"]);
+$container->hasService("someKey");
-unset($container["someKey"])
+$container->removeService("someKey");
2018-05-05 00:04:41 +00:00
```
## DependencyInjection
### `AnnotatedPropertyInjectToConstructorInjectionRector`
- class: `Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
Turns non-private properties with @annotation to private properties and constructor injection
2018-05-04 22:30:32 +00:00
```diff
/**
* @var SomeService
- * @annotation
*/
-public $someService;
+private $someService;
+
+public function __construct(SomeService $someService)
+{
+ $this->someService = $someService;
+}
2018-05-04 22:30:32 +00:00
```
### `ReplaceVariableByPropertyFetchRector`
- class: `Rector\Rector\Architecture\DependencyInjection\ReplaceVariableByPropertyFetchRector`
2018-05-04 22:30:32 +00:00
Turns variable in controller action to property fetch, as follow up to action injection variable to property change.
2018-05-04 22:30:32 +00:00
```diff
final class SomeController
{
/**
* @var ProductRepository
*/
private $productRepository;
2018-07-31 12:50:39 +00:00
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
2018-05-04 22:30:32 +00:00
public function default()
{
- $products = $productRepository->fetchAll();
+ $products = $this->productRepository->fetchAll();
}
}
```
### `ActionInjectionToConstructorInjectionRector`
- class: `Rector\Rector\Architecture\DependencyInjection\ActionInjectionToConstructorInjectionRector`
2018-05-04 22:30:32 +00:00
Turns action injection in Controllers to constructor injection
2018-05-04 22:30:32 +00:00
```diff
final class SomeController
2018-05-04 22:30:32 +00:00
{
- public function default(ProductRepository $productRepository)
+ /**
+ * @var ProductRepository
+ */
+ private $productRepository;
+ public function __construct(ProductRepository $productRepository)
{
- $products = $productRepository->fetchAll();
+ $this->productRepository = $productRepository;
2018-05-04 22:30:32 +00:00
+ }
+
+ public function default()
+ {
+ $products = $this->productRepository->fetchAll();
2018-05-04 22:30:32 +00:00
}
}
```
## RepositoryAsService
### `ReplaceParentRepositoryCallsByRepositoryPropertyRector`
- class: `Rector\Rector\Architecture\RepositoryAsService\ReplaceParentRepositoryCallsByRepositoryPropertyRector`
2018-05-04 22:30:32 +00:00
Handles method calls in child of Doctrine EntityRepository and moves them to "$this->repository" property.
2018-05-04 22:30:32 +00:00
```diff
<?php
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
2018-05-04 22:30:32 +00:00
{
public function someMethod()
{
- return $this->findAll();
+ return $this->repository->findAll();
2018-05-04 22:30:32 +00:00
}
}
```
### `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
```
### `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
```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
```
## Interface_
2018-05-04 22:30:32 +00:00
### `MergeInterfacesRector`
- class: `Rector\Rector\Interface_\MergeInterfacesRector`
2018-05-04 22:30:32 +00:00
Merges old interface to a new one, that already has its methods
2018-05-04 22:30:32 +00:00
```diff
-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
{
}
2018-05-04 22:30:32 +00:00
```
## Visibility
### `ChangeMethodVisibilityRector`
- class: `Rector\Rector\Visibility\ChangeMethodVisibilityRector`
2018-05-04 22:30:32 +00:00
Change visibility of method from parent class.
2018-05-04 22:30:32 +00:00
```diff
class FrameworkClass
{
protected someMethod()
{
}
}
2018-07-31 06:38:48 +00:00
class MyClass extends FrameworkClass
{
- public someMethod()
+ protected someMethod()
{
}
}
2018-07-31 06:38:48 +00:00
```
### `ChangePropertyVisibilityRector`
- class: `Rector\Rector\Visibility\ChangePropertyVisibilityRector`
2018-05-04 22:30:32 +00:00
Change visibility of property from parent class.
2018-05-04 22:30:32 +00:00
```diff
class FrameworkClass
{
protected $someProperty;
}
2018-05-04 22:30:32 +00:00
class MyClass extends FrameworkClass
2018-07-31 12:50:39 +00:00
{
- public $someProperty;
+ protected $someProperty;
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
```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
```
## CodeQuality
2018-05-04 22:30:32 +00:00
### `InArrayAndArrayKeysToArrayKeyExistsRector`
- class: `Rector\Rector\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector`
2018-05-04 22:30:32 +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
```diff
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
2018-05-04 22:30:32 +00:00
```
### `UnnecessaryTernaryExpressionRector`
- class: `Rector\Rector\CodeQuality\UnnecessaryTernaryExpressionRector`
2018-05-04 22:30:32 +00:00
Remove unnecessary ternary expressions.
2018-05-04 22:30:32 +00:00
```diff
-$foo === $bar ? true : false;
+$foo === $bar;
2018-05-04 22:30:32 +00:00
```
## Constant
### `RenameClassConstantsUseToStringsRector`
2018-05-04 22:30:32 +00:00
- class: `Rector\Rector\Constant\RenameClassConstantsUseToStringsRector`
2018-05-04 22:30:32 +00:00
Replaces constant by value
2018-07-31 12:50:39 +00:00
```diff
-$value === Nette\Configurator::DEVELOPMENT
+$value === "development"
2018-05-04 22:30:32 +00:00
```
## Class_
### `ParentClassToTraitsRector`
- class: `Rector\Rector\Class_\ParentClassToTraitsRector`
2018-05-04 22:30:32 +00:00
Replaces parent class to specific traits
2018-05-04 22:30:32 +00:00
```diff
-class SomeClass extends Nette\Object
+class SomeClass
2018-07-31 12:50:39 +00:00
{
+ use Nette\SmartObject;
2018-07-31 12:50:39 +00:00
}
2018-05-04 22:30:32 +00:00
```
2018-04-29 09:03:47 +00:00