diff --git a/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php b/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php index cfe111776883..aac6cbafb45e 100644 --- a/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php +++ b/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; @@ -18,7 +19,9 @@ use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; use Rector\Sensio\Helper\TemplateGuesser; +use Rector\ValueObject\PhpVersionFeature; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Response; final class TemplateAnnotationRector extends AbstractRector { @@ -133,6 +136,7 @@ private function replaceTemplateAnnotation(ClassMethod $classMethod): ?Node return null; } + $this->updateReturnType($classMethod); $this->refactorClassMethod($classMethod, $sensioTemplateTagValueNode); // remove annotation @@ -141,6 +145,22 @@ private function replaceTemplateAnnotation(ClassMethod $classMethod): ?Node return $classMethod; } + private function updateReturnType(ClassMethod $classMethod): void + { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) { + return; + } + + if ($classMethod->returnType !== null && strpos( + Response::class, + $classMethod->returnType->toString() + ) !== false) { + return; + } + + $classMethod->returnType = new Identifier('\Symfony\Component\HttpFoundation\Response'); + } + /** * @return Arg[] */ @@ -221,6 +241,7 @@ private function refactorClassMethod( $innerClassMethod = $this->parsedNodesByType->findClassMethodByMethodCall($returnNode->expr); if ($innerClassMethod !== null) { $this->refactorClassMethod($innerClassMethod, $sensioTemplateTagValueNode); + return; } } @@ -238,6 +259,8 @@ private function refactorClassMethod( if ($returnNode && ! $returnNode->expr instanceof MethodCall) { $returnNode->expr = $thisRenderMethodCall; } + + $this->updateReturnType($classMethod); } private function getSensioTemplateTagValueNode(ClassMethod $classMethod): ?SensioTemplateTagValueNode diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version5/with_return_types.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version5/with_return_types.php.inc new file mode 100755 index 000000000000..4b709441aca2 --- /dev/null +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version5/with_return_types.php.inc @@ -0,0 +1,87 @@ +redirectToRoute('index'); + } + + return []; + } + + /** + * @Template("AppBundle:Module:index3.html.twig") + * + * @return Response + */ + public function index3Action(): Response + { + return $this->render('AppBundle:Module:index3.html.twig'); + } +} + +?> +----- +render('AppBundle:Module:index.html.twig'); + } + + /** + * @return Response + */ + public function index2Action(): \Symfony\Component\HttpFoundation\Response + { + if (true) { + return $this->redirectToRoute('index'); + } + + return $this->render('AppBundle:Module:index2.html.twig'); + } + + /** + * @return Response + */ + public function index3Action(): Response + { + return $this->render('AppBundle:Module:index3.html.twig'); + } +} + +?> diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture.php.inc index c7a5607be133..6f4bc7101f98 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture.php.inc @@ -43,17 +43,17 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService13Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService13:index.html.twig'); } - public function index2Action() + public function index2Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService13:index2.html.twig', ['someKey' => 'someValue']); } - public function index3Action() + public function index3Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('someFile.toBe.used'); } diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture2.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture2.php.inc index 5bd3dce82410..b6c85f12f1d1 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture2.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture2.php.inc @@ -37,12 +37,12 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService23Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService23:index.html.twig'); } - public function index2Action() + public function index2Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService23:index.html.twig', array( 'form' => $form->createView() diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture3.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture3.php.inc index f02ac63c7540..6085e217b0cd 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture3.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture3.php.inc @@ -43,7 +43,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService33Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { if(true){ return $this->redirectToRoute('rector_is_cool'); @@ -54,7 +54,7 @@ class ClassWithNamedService33Controller extends AbstractController )); } - public function index2Action() + public function index2Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:NameNotFollowingConvention:index.html.twig', array( 'form' => $form->createView() diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture4.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture4.php.inc index 39ae1543ea9a..2d47953efa79 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture4.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/fixture4.php.inc @@ -25,7 +25,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService43 extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { return $this->render('payment/new.html.twig', array( 'form' => $form->createView(), diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/resolve_another_method_call.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/resolve_another_method_call.php.inc index 9cac7c7e1f2d..e5a23d8e1dfc 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/resolve_another_method_call.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion3/resolve_another_method_call.php.inc @@ -34,12 +34,12 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ResolveAnotherMethodCall extends AbstractController { - public function createAction(Request $request) + public function createAction(Request $request): \Symfony\Component\HttpFoundation\Response { return $this->processForm($request); } - private function processForm(Request $request): array + private function processForm(Request $request): \Symfony\Component\HttpFoundation\Response { return $this->render('PAPPSurveyBundle:Survey:create.html.twig', [ 'form' => $request, diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture.php.inc index 01e8fbb93d0e..ba13dd102849 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture.php.inc @@ -43,17 +43,17 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService15Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { return $this->render('@App/class_with_named_service15/index.html.twig'); } - public function index2Action() + public function index2Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('@App/class_with_named_service15/index2.html.twig', ['someKey' => 'someValue']); } - public function index3Action() + public function index3Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('someFile.toBe.used'); } diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture2.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture2.php.inc index 021f9d5c8314..ed1d91b0beb1 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture2.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture2.php.inc @@ -37,12 +37,12 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService25Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService25:index.html.twig'); } - public function index2Action() + public function index2Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService25:index.html.twig', array( 'form' => $form->createView() diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture3.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture3.php.inc index 39080063e1b3..5f9fc41d72f1 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture3.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture3.php.inc @@ -43,7 +43,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService35Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { if(true){ return $this->redirectToRoute('rector_is_cool'); @@ -54,7 +54,7 @@ class ClassWithNamedService35Controller extends AbstractController )); } - public function index2Action() + public function index2Action(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:NameNotFollowingConvention:index.html.twig', array( 'form' => $form->createView() diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture4.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture4.php.inc index 4e65b7e0585f..acab10e96aea 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture4.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture4.php.inc @@ -29,7 +29,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService45Controller extends AbstractController { - public function indexAction() + public function indexAction(): \Symfony\Component\HttpFoundation\Response { return $this->render('payment/new.html.twig', array( 'form' => $form->createView(), diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture5.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture5.php.inc index 46bae9cab1ec..68eda263833b 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture5.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/fixture5.php.inc @@ -27,7 +27,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ClassWithNamedService55Controller extends AbstractController { - public function index() + public function index(): \Symfony\Component\HttpFoundation\Response { return $this->render('class_with_named_service55/index.html.twig'); } diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_options.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_options.php.inc index 1eb14b12f71e..3ea25a133211 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_options.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_options.php.inc @@ -35,7 +35,7 @@ class WithRouteOptions extends AbstractController /** * @Route("/{category}", name="report_overview", defaults={"category":null}, requirements={"category":"\d+"}) */ - public function index($category = null) + public function index($category = null): \Symfony\Component\HttpFoundation\Response { return $this->render('PAPPReportBundle:Report:report_list.html.twig', [ 'category' => $category, diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_too.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_too.php.inc index 4ab86bf21c6b..ef62acc50067 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_too.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/with_route_too.php.inc @@ -33,7 +33,7 @@ class WithRouteToo extends AbstractController /** * @Route("/change", name="facility_change") */ - public function index() + public function index(): \Symfony\Component\HttpFoundation\Response { return $this->render('PAPPUserBundle:Facility:facility.html.twig'); } diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/without_base_class.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/without_base_class.php.inc index 708d1cdb9299..f040f8dd9ad6 100755 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/without_base_class.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/FixtureVersion5/without_base_class.php.inc @@ -31,7 +31,7 @@ class WithoutBaseClass extends \Symfony\Bundle\FrameworkBundle\Controller\Abstra /** * @Route("/change", name="facility_change") */ - public function index() + public function index(): \Symfony\Component\HttpFoundation\Response { return $this->render('AppBundle:ClassWithNamedService25:index.html.twig'); }