From d532406e6ea3ef70c469fdc6ad46331f67c17345 Mon Sep 17 00:00:00 2001 From: mssimi Date: Tue, 16 Jan 2018 18:37:57 +0100 Subject: [PATCH 1/4] added tests and fixed code --- .../HttpKernel/TemplateAnnotationRector.php | 10 ++++++---- .../Correct/correct7.php.inc | 15 +++++++++++++++ .../Correct/correct8.php.inc | 13 +++++++++++++ .../TemplateAnnotationRectorTest.php | 2 ++ .../Wrong/wrong7.php.inc | 16 ++++++++++++++++ .../Wrong/wrong8.php.inc | 14 ++++++++++++++ 6 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc create mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct8.php.inc create mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc create mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong8.php.inc diff --git a/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php b/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php index 04ce324302d2..8c19f22becc2 100644 --- a/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php +++ b/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php @@ -92,14 +92,16 @@ public function refactor(Node $classMethodNode): ?Node $renderArguments ); - // replace Return_ node value if exists - if ($returnNode) { - $returnNode->expr = $thisRenderMethodCall; - } else { + if (!$returnNode) { // or add as last statement in the method $classMethodNode->stmts[] = new Return_($thisRenderMethodCall); } + // replace Return_ node value if exists and is not already in correct format + if ($returnNode && !$returnNode->expr instanceof MethodCall) { + $returnNode->expr = $thisRenderMethodCall; + } + // remove annotation $this->docBlockAnalyzer->removeAnnotationFromNode($classMethodNode, 'Template'); diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc new file mode 100644 index 000000000000..7ccfc9a79c90 --- /dev/null +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc @@ -0,0 +1,15 @@ +render('AppBundle:NameNotFollowingConvention:index.html.twig', array( + 'form' => $form->createView() + )); + } +} diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct8.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct8.php.inc new file mode 100644 index 000000000000..8d1b0f95bf6b --- /dev/null +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct8.php.inc @@ -0,0 +1,13 @@ +render('payment/new.html.twig', array( + 'form' => $form->createView(), + )); + } +} diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php index 78616fd0d436..a71c87ab1280 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php @@ -27,6 +27,8 @@ public function provideWrongToFixedFiles(): array [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'], [__DIR__ . '/Wrong/wrong5.php.inc', __DIR__ . '/Correct/correct5.php.inc'], [__DIR__ . '/Wrong/wrong6.php.inc', __DIR__ . '/Correct/correct6.php.inc'], + [__DIR__ . '/Wrong/wrong7.php.inc', __DIR__ . '/Correct/correct7.php.inc'], + [__DIR__ . '/Wrong/wrong8.php.inc', __DIR__ . '/Correct/correct8.php.inc'], ]; } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc new file mode 100644 index 000000000000..c455a6b221d3 --- /dev/null +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc @@ -0,0 +1,16 @@ +render('AppBundle:NameNotFollowingConvention:index.html.twig', array( + 'form' => $form->createView() + )); + } +} diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong8.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong8.php.inc new file mode 100644 index 000000000000..4823e21edfbc --- /dev/null +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong8.php.inc @@ -0,0 +1,14 @@ +render('payment/new.html.twig', array( + 'form' => $form->createView(), + )); + } +} From 602efe1f1cd036cc13c68d8b6f08dc45e504a50a Mon Sep 17 00:00:00 2001 From: mssimi Date: Tue, 16 Jan 2018 20:14:22 +0100 Subject: [PATCH 2/4] added tests and fixed code, re-implemented template guesser --- .../Contrib/Symfony/TemplateGuesser.php | 36 +++++++++++-------- .../Correct/correct.php.inc | 2 +- .../Correct/correct2.php.inc | 2 +- .../Correct/correct4.php.inc | 2 +- .../Correct/correct5.php.inc | 2 +- .../Correct/correct6.php.inc | 2 +- .../Correct/correct7.php.inc | 2 +- .../Correct/correct9.php.inc | 14 ++++++++ .../TemplateAnnotationRectorTest.php | 1 + .../Wrong/wrong4.php.inc | 2 +- .../Wrong/wrong5.php.inc | 2 +- .../Wrong/wrong7.php.inc | 2 +- .../Wrong/wrong9.php.inc | 15 ++++++++ 13 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc create mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc diff --git a/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php b/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php index 7f43b3ffc80e..dd66aaa3c2f8 100644 --- a/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php +++ b/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php @@ -12,27 +12,33 @@ */ final class TemplateGuesser { + public function resolveFromClassMethodNode(ClassMethod $classMethodNode): string { + $controllerPatterns[] = '/Controller\\\(.+)Controller$/'; + $namespace = (string) $classMethodNode->getAttribute(Attribute::NAMESPACE_NAME); - $class = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME); + $className = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME); $method = $classMethodNode->name->toString(); // AppBundle\SomeNamespace\ => AppBundle // App\OtherBundle\SomeNamespace\ => OtherBundle - $templateName = Strings::match($namespace, '/(?[A-Za-z]*Bundle)/')['bundle'] ?? ''; - $templateName .= ':'; - - // SomeSuper\ControllerClass => ControllerClass - $templateName .= Strings::match($class, '/(?[A-Za-z0-9]*)Controller$/')['controller'] ?? ''; - $templateName .= ':'; - - // indexAction => index - $templateName .= Strings::match($method, '/(?[A-Za-z]*)Action$/')['method'] ?? ''; - - // suffix - $templateName .= '.html.twig'; - - return $templateName; + $bundleName = Strings::match($namespace, '/(?[A-Za-z]*Bundle)/')['bundle'] ?? ''; + $bundleName = preg_replace('/Bundle$/', '', $bundleName); + + $matchController = null; + foreach ($controllerPatterns as $pattern) { + if (preg_match($pattern, $className, $tempMatch)) { + $matchController = str_replace('\\', '/', strtolower(preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $tempMatch[1]))); + break; + } + } + if (null === $matchController) { + throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (its FQN must match one of the following regexps: "%s")', $className, implode('", "', $controllerPatterns))); + } + + $matchAction = preg_replace('/Action$/', '', $method); + + return sprintf(($bundleName ? '@'.$bundleName.'/' : '').$matchController.($matchController ? '/' : '').$matchAction.'.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc index d2845d88b442..25ea1291bc6c 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig'); + return $this->render('@App/class_with_named_service1/index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc index ee1eb6fc951c..ed9060dd621c 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', ['someKey' => 'someValue']); + return $this->render('@App/class_with_named_service1/index.html.twig', ['someKey' => 'someValue']); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc index d2845d88b442..25ea1291bc6c 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig'); + return $this->render('@App/class_with_named_service1/index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc index 100fee2b620a..11074b6ea716 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc @@ -8,7 +8,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', array( + return $this->render('@App/class_with_named_service1/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc index 3cf6bce712a1..d2eef7209b80 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc @@ -12,7 +12,7 @@ class ClassWithNamedService1Controller extends Controller return $this->redirectToRoute('rector_is_cool'); } - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', array( + return $this->render('@App/class_with_named_service1/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc index 7ccfc9a79c90..c6e1edaecb2e 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct7.php.inc @@ -8,7 +8,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:NameNotFollowingConvention:index.html.twig', array( + return $this->render('@App/NameNotFollowingConvention/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc new file mode 100644 index 000000000000..c03bdfbba14e --- /dev/null +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc @@ -0,0 +1,14 @@ +render('app/index.html.twig'); + } +} diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php index a71c87ab1280..b4883b90f3d1 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php @@ -29,6 +29,7 @@ public function provideWrongToFixedFiles(): array [__DIR__ . '/Wrong/wrong6.php.inc', __DIR__ . '/Correct/correct6.php.inc'], [__DIR__ . '/Wrong/wrong7.php.inc', __DIR__ . '/Correct/correct7.php.inc'], [__DIR__ . '/Wrong/wrong8.php.inc', __DIR__ . '/Correct/correct8.php.inc'], + [__DIR__ . '/Wrong/wrong9.php.inc', __DIR__ . '/Correct/correct9.php.inc'], ]; } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc index 043dd10dd3c1..a5144b175fd1 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc @@ -9,6 +9,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig'); + return $this->render('@App/class_with_named_service1/index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc index 57cd1586c89b..3a693a514579 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc @@ -9,7 +9,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', array( + return $this->render('@App/class_with_named_service1/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc index c455a6b221d3..c14d30834c73 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong7.php.inc @@ -9,7 +9,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('AppBundle:NameNotFollowingConvention:index.html.twig', array( + return $this->render('@App/NameNotFollowingConvention/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc new file mode 100644 index 000000000000..25779ce05801 --- /dev/null +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc @@ -0,0 +1,15 @@ + Date: Tue, 16 Jan 2018 20:30:07 +0100 Subject: [PATCH 3/4] tests wrong for bundles --- .../HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc | 2 +- .../TemplateAnnotationRector/Correct/correct2.php.inc | 2 +- .../TemplateAnnotationRector/Correct/correct4.php.inc | 2 +- .../TemplateAnnotationRector/Correct/correct5.php.inc | 2 +- .../TemplateAnnotationRector/Correct/correct6.php.inc | 2 +- .../HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc | 2 +- .../HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc index 25ea1291bc6c..740db1b33268 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/class_with_named_service1/index.html.twig'); + return $this->render('@App/ClassWithNamedService1/index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc index ed9060dd621c..de3ba37782b6 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/class_with_named_service1/index.html.twig', ['someKey' => 'someValue']); + return $this->render('@App/ClassWithNamedService1/index.html.twig', ['someKey' => 'someValue']); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc index 25ea1291bc6c..740db1b33268 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/class_with_named_service1/index.html.twig'); + return $this->render('@App/ClassWithNamedService1/index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc index 11074b6ea716..a1c4eec659d2 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc @@ -8,7 +8,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/class_with_named_service1/index.html.twig', array( + return $this->render('@App/ClassWithNamedService1/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc index d2eef7209b80..b521f94bcbb1 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc @@ -12,7 +12,7 @@ class ClassWithNamedService1Controller extends Controller return $this->redirectToRoute('rector_is_cool'); } - return $this->render('@App/class_with_named_service1/index.html.twig', array( + return $this->render('@App/ClassWithNamedService1/index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc index a5144b175fd1..7b0245f58298 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc @@ -9,6 +9,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/class_with_named_service1/index.html.twig'); + return $this->render('@App/ClassWithNamedService1/index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc index 3a693a514579..48afb2a57a00 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc @@ -9,7 +9,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/class_with_named_service1/index.html.twig', array( + return $this->render('@App/ClassWithNamedService1/index.html.twig', array( 'form' => $form->createView() )); } From 7fb2c3dd6482cd2236de9af1e2ff0fa8a6bc8681 Mon Sep 17 00:00:00 2001 From: mssimi Date: Tue, 16 Jan 2018 22:53:36 +0100 Subject: [PATCH 4/4] rector for sensio-framework-extra-bundle-30 --- .../Contrib/Symfony/TemplateGuesser.php | 32 ++++++------------- .../HttpKernel/TemplateAnnotationRector.php | 8 ++--- .../sensio-framework-extra-bundle-30.yml | 4 +++ src/config/level/symfony/symfony33.yml | 3 -- .../Correct/correct.php.inc | 2 +- .../Correct/correct2.php.inc | 2 +- .../Correct/correct4.php.inc | 2 +- .../Correct/correct5.php.inc | 2 +- .../Correct/correct6.php.inc | 2 +- .../Correct/correct9.php.inc | 14 -------- .../TemplateAnnotationRectorTest.php | 1 - .../Wrong/wrong4.php.inc | 2 +- .../Wrong/wrong5.php.inc | 2 +- .../Wrong/wrong9.php.inc | 15 --------- 14 files changed, 24 insertions(+), 67 deletions(-) create mode 100644 src/config/level/sensio-framework-extra-bundle/sensio-framework-extra-bundle-30.yml delete mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc delete mode 100644 tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc diff --git a/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php b/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php index dd66aaa3c2f8..8a30ed4184f7 100644 --- a/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php +++ b/src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php @@ -7,38 +7,24 @@ use Rector\Node\Attribute; /** - * Mimics https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/072c00c52b947e88a1e619e9ff426cee6c8c482b/Templating/TemplateGuesser.php + * Mimics https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/v3.0.29/Templating/TemplateGuesser.php * only without Symfony dependency */ final class TemplateGuesser { - public function resolveFromClassMethodNode(ClassMethod $classMethodNode): string { - $controllerPatterns[] = '/Controller\\\(.+)Controller$/'; - $namespace = (string) $classMethodNode->getAttribute(Attribute::NAMESPACE_NAME); - $className = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME); + $class = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME); $method = $classMethodNode->name->toString(); - // AppBundle\SomeNamespace\ => AppBundle - // App\OtherBundle\SomeNamespace\ => OtherBundle - $bundleName = Strings::match($namespace, '/(?[A-Za-z]*Bundle)/')['bundle'] ?? ''; - $bundleName = preg_replace('/Bundle$/', '', $bundleName); - - $matchController = null; - foreach ($controllerPatterns as $pattern) { - if (preg_match($pattern, $className, $tempMatch)) { - $matchController = str_replace('\\', '/', strtolower(preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $tempMatch[1]))); - break; - } - } - if (null === $matchController) { - throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (its FQN must match one of the following regexps: "%s")', $className, implode('", "', $controllerPatterns))); - } - - $matchAction = preg_replace('/Action$/', '', $method); + // converts AppBundle\SomeNamespace\ => AppBundle + // converts App\OtherBundle\SomeNamespace\ => OtherBundle + // This check may fail if people dont follow naming conventions + $bundle = Strings::match($namespace, '/(?[A-Za-z]*Bundle)/')['bundle'] ?? ''; + $controller = Strings::match($class, '/(?[A-Za-z0-9]*)Controller$/')['controller'] ?? ''; + $action = Strings::match($method, '/(?[A-Za-z]*)Action$/')['method'] ?? ''; - return sprintf(($bundleName ? '@'.$bundleName.'/' : '').$matchController.($matchController ? '/' : '').$matchAction.'.html.twig'); + return sprintf('%s:%s:%s.html.twig', $bundle, $controller, $action); } } diff --git a/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php b/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php index 8c19f22becc2..27e9eb917cc0 100644 --- a/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php +++ b/src/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector.php @@ -23,7 +23,7 @@ * * into: * - public function indexAction() { - * - $this->render('index.html.twig'); + * - return $this->render('@App/App/index.html.twig'); * - } */ final class TemplateAnnotationRector extends AbstractRector @@ -92,13 +92,13 @@ public function refactor(Node $classMethodNode): ?Node $renderArguments ); - if (!$returnNode) { - // or add as last statement in the method + // add as last statement in the method + if (! $returnNode) { $classMethodNode->stmts[] = new Return_($thisRenderMethodCall); } // replace Return_ node value if exists and is not already in correct format - if ($returnNode && !$returnNode->expr instanceof MethodCall) { + if ($returnNode && ! $returnNode->expr instanceof MethodCall) { $returnNode->expr = $thisRenderMethodCall; } diff --git a/src/config/level/sensio-framework-extra-bundle/sensio-framework-extra-bundle-30.yml b/src/config/level/sensio-framework-extra-bundle/sensio-framework-extra-bundle-30.yml new file mode 100644 index 000000000000..d0407f00ec37 --- /dev/null +++ b/src/config/level/sensio-framework-extra-bundle/sensio-framework-extra-bundle-30.yml @@ -0,0 +1,4 @@ +rectors: + Rector\Rector\Dynamic\ClassReplacerRector: + # http-kernel + Rector\Rector\Contrib\Symfony\HttpKernel\TemplateAnnotationRector: ~ \ No newline at end of file diff --git a/src/config/level/symfony/symfony33.yml b/src/config/level/symfony/symfony33.yml index 5ed8f8d3b19a..eb414d7c71d0 100644 --- a/src/config/level/symfony/symfony33.yml +++ b/src/config/level/symfony/symfony33.yml @@ -25,9 +25,6 @@ rectors: # debug 'Symfony\Component\Debug\Exception\ContextErrorException': 'ErrorException' - # http-kernel - Rector\Rector\Contrib\Symfony\HttpKernel\TemplateAnnotationRector: ~ - # dependency-injection 'Symfony\Component\DependencyInjection\DefinitionDecorator': 'Symfony\Component\DependencyInjection\ChildDefinition' diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc index 740db1b33268..d2845d88b442 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/ClassWithNamedService1/index.html.twig'); + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc index de3ba37782b6..ee1eb6fc951c 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct2.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/ClassWithNamedService1/index.html.twig', ['someKey' => 'someValue']); + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', ['someKey' => 'someValue']); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc index 740db1b33268..d2845d88b442 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct4.php.inc @@ -8,6 +8,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/ClassWithNamedService1/index.html.twig'); + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc index a1c4eec659d2..100fee2b620a 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct5.php.inc @@ -8,7 +8,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/ClassWithNamedService1/index.html.twig', array( + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc index b521f94bcbb1..3cf6bce712a1 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct6.php.inc @@ -12,7 +12,7 @@ class ClassWithNamedService1Controller extends Controller return $this->redirectToRoute('rector_is_cool'); } - return $this->render('@App/ClassWithNamedService1/index.html.twig', array( + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc deleted file mode 100644 index c03bdfbba14e..000000000000 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Correct/correct9.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -render('app/index.html.twig'); - } -} diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php index b4883b90f3d1..a71c87ab1280 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/TemplateAnnotationRectorTest.php @@ -29,7 +29,6 @@ public function provideWrongToFixedFiles(): array [__DIR__ . '/Wrong/wrong6.php.inc', __DIR__ . '/Correct/correct6.php.inc'], [__DIR__ . '/Wrong/wrong7.php.inc', __DIR__ . '/Correct/correct7.php.inc'], [__DIR__ . '/Wrong/wrong8.php.inc', __DIR__ . '/Correct/correct8.php.inc'], - [__DIR__ . '/Wrong/wrong9.php.inc', __DIR__ . '/Correct/correct9.php.inc'], ]; } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc index 7b0245f58298..043dd10dd3c1 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong4.php.inc @@ -9,6 +9,6 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/ClassWithNamedService1/index.html.twig'); + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig'); } } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc index 48afb2a57a00..57cd1586c89b 100644 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc +++ b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong5.php.inc @@ -9,7 +9,7 @@ class ClassWithNamedService1Controller extends Controller */ public function indexAction() { - return $this->render('@App/ClassWithNamedService1/index.html.twig', array( + return $this->render('AppBundle:ClassWithNamedService1:index.html.twig', array( 'form' => $form->createView() )); } diff --git a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc b/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc deleted file mode 100644 index 25779ce05801..000000000000 --- a/tests/Rector/Contrib/Symfony/HttpKernel/TemplateAnnotationRector/Wrong/wrong9.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -