Find the best Rector rule to solve your problem. Searching through 661 rules.
Found 6 rules:
Turns fetching of dependencies via $this->get()
to constructor injection in Command and Controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
final class SomeController extend Controller
{
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
public function someMethod()
{
- // ...
- $this->get('some_service');
+ $this->someService;
}
}
Change $container->get("some_name") to bare type, useful since Symfony 3.4
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$container = $this->getContainer();
- $someClass = $container->get('some_name');
+ $someClass = $container->get(SomeType::class);
}
}
From $this->get(SomeType::class)
in traits, to autowired method with @required
-// must be used in old Controller class
trait SomeInjects
{
+ private SomeType $someType;
+
+ /**
+ * @required
+ */
+ public function autowireSomeInjects(SomeType $someType): void
+ {
+ $this->someType = $someType;
+ }
+
public function someMethod()
{
- return $this->get(SomeType::class);
+ return $this->someType;
}
}
From $container->get(SomeType::class)
in controllers to constructor injection (step 1/x)
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
final class SomeCommand extends Controller
{
+ public function __construct(private SomeType $someType)
+ {
+ }
+
public function someMethod()
{
- $someType = $this->get(SomeType::class);
+ $someType = $this->someType;
}
}
Converts typical Symfony services like $this->get("validator") in commands/controllers to constructor injection (step 3/x)
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Validator\Validator\ValidatorInterface;
final class SomeController extends Controller
{
+ public function __construct(private ValidatorInterface $validator)
+
public function someMethod()
{
- $someType = $this->get('validator');
+ $someType = $this->validator;
}
}
From $container->get(SomeType::class)
in commands to constructor injection (step 2/x)
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
final class SomeCommand extends ContainerAwareCommand
{
+ public function __construct(private SomeType $someType)
+ {
+ }
+
public function someMethod()
{
- $someType = $this->get(SomeType::class);
+ $someType = $this->someType;
}
}