Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -133,6 +136,7 @@ private function replaceTemplateAnnotation(ClassMethod $classMethod): ?Node
return null;
}

$this->updateReturnType($classMethod);
$this->refactorClassMethod($classMethod, $sensioTemplateTagValueNode);

// remove annotation
Expand All @@ -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[]
*/
Expand Down Expand Up @@ -221,6 +241,7 @@ private function refactorClassMethod(
$innerClassMethod = $this->parsedNodesByType->findClassMethodByMethodCall($returnNode->expr);
if ($innerClassMethod !== null) {
$this->refactorClassMethod($innerClassMethod, $sensioTemplateTagValueNode);

return;
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class ClassWithReturnTypes extends AbstractController
{
/**
* @Template("AppBundle:Module:index.html.twig")
*
* @return array
*/
public function indexAction(): array
{
return [];
}

/**
* @Template("AppBundle:Module:index2.html.twig")
*
* @return array|Response
*/
public function index2Action()
{
if (true) {
return $this->redirectToRoute('index');
}

return [];
}

/**
* @Template("AppBundle:Module:index3.html.twig")
*
* @return Response
*/
public function index3Action(): Response
{
return $this->render('AppBundle:Module:index3.html.twig');
}
}

?>
-----
<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class ClassWithReturnTypes extends AbstractController
{
/**
* @return Response
*/
public function indexAction(): \Symfony\Component\HttpFoundation\Response
{
return $this->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');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down