Find the best Rector rule to solve your problem
Replace "GET" string by Symfony Request object class constants
use Symfony\Component\Form\FormBuilderInterface;
final class SomeClass
{
public function detail(FormBuilderInterface $formBuilder)
{
- $formBuilder->setMethod('GET');
+ $formBuilder->setMethod(\Symfony\Component\HttpFoundation\Request::GET);
}
}
Make assertSame(200, $response->getStatusCode()) in tests comparing response code to include response contents for faster feedback
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
{
public function run()
{
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $this->processResult();
- $this->assertSame(200, $response->getStatusCode());
+ $this->assertSame(200, $response->getStatusCode(), $response->getContent());
}
}
Turns status code numbers to constants
use Symfony\Component\HttpFoundation\Response;
class SomeController
{
public function index()
{
$response = new Response();
- $response->setStatusCode(200);
+ $response->setStatusCode(Response::HTTP_OK);
- if ($response->getStatusCode() === 200) {
+ if ($response->getStatusCode() === Response::HTTP_OK) {
}
}
}
Change Symfony Event listener class to Event Subscriber based on configuration in service.yaml file
-class SomeListener
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class SomeEventSubscriber implements EventSubscriberInterface
{
+ /**
+ * @return string[]
+ */
+ public static function getSubscribedEvents(): array
+ {
+ return ['some_event' => 'methodToBeCalled'];
+ }
+
public function methodToBeCalled()
{
}
-}
-
-// in config.yaml
-services:
- SomeListener:
- tags:
- - { name: kernel.event_listener, event: 'some_event', method: 'methodToBeCalled' }
+}
Move metadata from loadValidatorMetadata() to property/getter/method annotations
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
final class SomeClass
{
+ /**
+ * @Assert\NotBlank(message="City can't be blank.")
+ */
private $city;
-
- public static function loadValidatorMetadata(ClassMetadata $metadata): void
- {
- $metadata->addPropertyConstraint('city', new Assert\NotBlank([
- 'message' => 'City can\'t be blank.',
- ]));
- }
}
Removes Action suffixes from methods in Symfony Controllers
class SomeController
{
- public function indexAction()
+ public function index()
{
}
}
Remove unused $request parameter from controller action
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
final class SomeController extends Controller
{
- public function run(Request $request, int $id)
+ public function run(int $id)
{
echo $id;
}
}
Complete strict param type declaration based on route annotation
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
final class SomeController extends Controller
{
/**
* @Route(
* requirements={"number"="\d+"},
* )
*/
- public function detailAction($number)
+ public function detailAction(int $number)
{
}
}
Add Response object return type to controller actions
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
final class SomeController extends AbstractController
{
#[Route]
- public function detail()
+ public function detail(): Response
{
return $this->render('some_template');
}
}
Turns redirect to route to short helper method in Controller in Symfony
-$this->redirect($this->generateUrl("homepage"));
+$this->redirectToRoute("homepage");