Find the best Rector rule to solve your problem
Change visibility of method from parent class.
class FrameworkClass
{
protected function someMethod()
{
}
}
class MyClass extends FrameworkClass
{
- public function someMethod()
+ protected function someMethod()
{
}
}
Change new Object to static call
class SomeClass
{
public function run()
{
- new Cookie($name);
+ Cookie::create($name);
}
}
Wrap return value of specific method
final class SomeClass
{
public function getItem()
{
- return 1;
+ return [1];
}
}
This Rector adds new default arguments in calls of defined methods and class types.
$someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
class MyCustomClass extends SomeExampleClass
{
- public function someMethod()
+ public function someMethod($value = true)
{
}
}
Replaces defined map of arguments in defined methods and their calls.
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);
Turns method names to new ones.
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
Replaces defined classes by new ones.
namespace App;
-use SomeOldClass;
+use SomeNewClass;
-function someFunction(SomeOldClass $someOldClass): SomeOldClass
+function someFunction(SomeNewClass $someOldClass): SomeNewClass
{
- if ($someOldClass instanceof SomeOldClass) {
- return new SomeOldClass;
+ if ($someOldClass instanceof SomeNewClass) {
+ return new SomeNewClass;
}
}
Removes defined arguments in defined methods and their calls.
$someObject = new SomeClass;
-$someObject->someMethod(true);
+$someObject->someMethod();
Changes defined return typehint of method and class.
class SomeClass
{
- public function getData()
+ public function getData(): array
{
}
}
Turns fetching of dependencies via $container->get()
in ContainerAware to constructor injection in Command and Controller in Symfony
final class SomeCommand extends ContainerAwareCommand
{
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
public function someMethod()
{
// ...
- $this->getContainer()->get('some_service');
- $this->container->get('some_service');
+ $this->someService;
+ $this->someService;
}
}
Changes TreeBuilder with root() call to constructor passed root and getRootNode() call
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
-$treeBuilder = new TreeBuilder();
-$rootNode = $treeBuilder->root('acme_root');
+$treeBuilder = new TreeBuilder('acme_root');
+$rootNode = $treeBuilder->getRootNode();
$rootNode->someCall();
Changes Process string argument to an array
use Symfony\Component\Process\Process;
-$process = new Process('ls -l');
+$process = new Process(['ls', '-l']);