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
66 changes: 56 additions & 10 deletions packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\BetterPhpDocParser\PhpDocNode\Sensio\SensioTemplateTagValueNode;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\Sensio\Helper\TemplateGuesser;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class TemplateAnnotationRector extends AbstractRector
{
Expand Down Expand Up @@ -65,15 +68,57 @@ public function indexAction()
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [ClassMethod::class, Class_::class];
}

/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$phpDocInfo = $this->getPhpDocInfo($node);
if ($node instanceof Class_) {
return $this->addBaseClassIfMissing($node);
}

if ($node instanceof ClassMethod) {
return $this->replaceTemplateAnnotation($node);
}

return null;
}

private function addBaseClassIfMissing(Class_ $node): ?Node
{
if ($node->extends !== null) {
return null;
}

if (! $this->classHasTemplateAnnotations($node)) {
return null;
}

$node->extends = new FullyQualified(AbstractController::class);

return $node;
}

private function classHasTemplateAnnotations(Class_ $node): bool
{
foreach ($node->stmts as $stmtNode) {
$phpDocInfo = $this->getPhpDocInfo($stmtNode);
if ($phpDocInfo === null) {
continue;
}

$templateTagValueNode = $phpDocInfo->getByType(SensioTemplateTagValueNode::class);
if ($templateTagValueNode !== null) {
return true;
}
}

return false;
}

private function replaceTemplateAnnotation(ClassMethod $classMethod): ?Node
{
$phpDocInfo = $this->getPhpDocInfo($classMethod);
if ($phpDocInfo === null) {
return null;
}
Expand All @@ -84,15 +129,15 @@ public function refactor(Node $node): ?Node
}

/** @var Return_|null $returnNode */
$returnNode = $this->betterNodeFinder->findLastInstanceOf((array) $node->stmts, Return_::class);
$returnNode = $this->betterNodeFinder->findLastInstanceOf((array) $classMethod->stmts, Return_::class);

// create "$this->render('template.file.twig.html', ['key' => 'value']);" method call
$renderArguments = $this->resolveRenderArguments($node, $returnNode, $templateTagValueNode);
$renderArguments = $this->resolveRenderArguments($classMethod, $returnNode, $templateTagValueNode);
$thisRenderMethodCall = $this->createMethodCall('this', 'render', $renderArguments);

if ($returnNode === null) {
// or add as last statement in the method
$node->stmts[] = new Return_($thisRenderMethodCall);
$classMethod->stmts[] = new Return_($thisRenderMethodCall);
}

// replace Return_ node value if exists and is not already in correct format
Expand All @@ -101,9 +146,9 @@ public function refactor(Node $node): ?Node
}

// remove annotation
$this->docBlockManipulator->removeTagFromNode($node, SensioTemplateTagValueNode::class);
$this->docBlockManipulator->removeTagFromNode($classMethod, SensioTemplateTagValueNode::class);

return $node;
return $classMethod;
}

/**
Expand Down Expand Up @@ -142,6 +187,7 @@ private function resolveTemplateName(

/**
* Already existing method call
*
* @return Array_[]
*/
private function resolveArrayArgumentsFromMethodCall(Return_ $returnNode): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace AppBundle\Controller;

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

class ClassWithNamedService13Controller
class ClassWithNamedService13Controller extends AbstractController
{
/**
* @Template
Expand Down Expand Up @@ -38,8 +39,9 @@ class ClassWithNamedService13Controller
namespace AppBundle\Controller;

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

class ClassWithNamedService13Controller
class ClassWithNamedService13Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace AppBundle\Controller;

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

class ClassWithNamedService23Controller
class ClassWithNamedService23Controller extends AbstractController
{
/**
* @Template()
Expand Down Expand Up @@ -32,8 +33,9 @@ class ClassWithNamedService23Controller
namespace AppBundle\Controller;

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

class ClassWithNamedService23Controller
class ClassWithNamedService23Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace AppBundle\Controller;

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

class ClassWithNamedService33Controller
class ClassWithNamedService33Controller extends AbstractController
{
/**
* @Template()
Expand Down Expand Up @@ -38,8 +39,9 @@ class ClassWithNamedService33Controller
namespace AppBundle\Controller;

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

class ClassWithNamedService33Controller
class ClassWithNamedService33Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php declare (strict_types=1);

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

class ClassWithNamedService43
class ClassWithNamedService43 extends AbstractController
{
/**
* @Template("AdminBundle:Payment:create.html.twig")
Expand All @@ -20,8 +21,9 @@ class ClassWithNamedService43
<?php declare (strict_types=1);

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

class ClassWithNamedService43
class ClassWithNamedService43 extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Fixture;

class SkipJustTemplateController
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SkipJustTemplateController extends AbstractController
{
/**
* @Template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace AppBundle\Controller;

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

class ClassWithNamedService15Controller
class ClassWithNamedService15Controller extends AbstractController
{
/**
* @Template
Expand Down Expand Up @@ -38,8 +39,9 @@ class ClassWithNamedService15Controller
namespace AppBundle\Controller;

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

class ClassWithNamedService15Controller
class ClassWithNamedService15Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace AppBundle\Controller;

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

class ClassWithNamedService25Controller
class ClassWithNamedService25Controller extends AbstractController
{
/**
* @Template()
Expand Down Expand Up @@ -32,8 +33,9 @@ class ClassWithNamedService25Controller
namespace AppBundle\Controller;

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

class ClassWithNamedService25Controller
class ClassWithNamedService25Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace AppBundle\Controller;

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

class ClassWithNamedService35Controller
class ClassWithNamedService35Controller extends AbstractController
{
/**
* @Template()
Expand Down Expand Up @@ -38,8 +39,9 @@ class ClassWithNamedService35Controller
namespace AppBundle\Controller;

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

class ClassWithNamedService35Controller
class ClassWithNamedService35Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace App\Controller;

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

class ClassWithNamedService45Controller
class ClassWithNamedService45Controller extends AbstractController
{
/**
* @Template("AdminBundle:Payment:create.html.twig")
Expand All @@ -24,8 +25,9 @@ class ClassWithNamedService45Controller
namespace App\Controller;

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

class ClassWithNamedService45Controller
class ClassWithNamedService45Controller extends AbstractController
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace App\Controller;

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

class ClassWithNamedService55Controller
class ClassWithNamedService55Controller extends AbstractController
{
/**
* @Template()
Expand All @@ -22,8 +23,9 @@ class ClassWithNamedService55Controller
namespace App\Controller;

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

class ClassWithNamedService55Controller
class ClassWithNamedService55Controller extends AbstractController
{
public function index()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;

class WithoutBaseClassAndTemplate
{
/**
* @Route("/route", name="route")
*/
public function index(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class WithRouteOptions
class WithRouteOptions extends AbstractController
{
/**
* @Route("/{category}", name="report_overview", defaults={"category":null}, requirements={"category":"\d+"})
Expand All @@ -26,9 +27,10 @@ class WithRouteOptions
namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class WithRouteOptions
class WithRouteOptions extends AbstractController
{
/**
* @Route("/{category}", name="report_overview", defaults={"category":null}, requirements={"category":"\d+"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class WithRouteToo
class WithRouteToo extends AbstractController
{
/**
* @Route("/change", name="facility_change")
Expand All @@ -24,9 +25,10 @@ class WithRouteToo
namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class WithRouteToo
class WithRouteToo extends AbstractController
{
/**
* @Route("/change", name="facility_change")
Expand Down
Loading