Skip to content
Closed
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
24 changes: 8 additions & 16 deletions src/NodeAnalyzer/Contrib/Symfony/TemplateGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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
Expand All @@ -18,21 +18,13 @@ public function resolveFromClassMethodNode(ClassMethod $classMethodNode): string
$class = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME);
$method = $classMethodNode->name->toString();

// AppBundle\SomeNamespace\ => AppBundle
// App\OtherBundle\SomeNamespace\ => OtherBundle
$templateName = Strings::match($namespace, '/(?<bundle>[A-Za-z]*Bundle)/')['bundle'] ?? '';
$templateName .= ':';
// converts AppBundle\SomeNamespace\ => AppBundle
// converts App\OtherBundle\SomeNamespace\ => OtherBundle
// This check may fail if people dont follow naming conventions
$bundle = Strings::match($namespace, '/(?<bundle>[A-Za-z]*Bundle)/')['bundle'] ?? '';
$controller = Strings::match($class, '/(?<controller>[A-Za-z0-9]*)Controller$/')['controller'] ?? '';
$action = Strings::match($method, '/(?<method>[A-Za-z]*)Action$/')['method'] ?? '';

// SomeSuper\ControllerClass => ControllerClass
$templateName .= Strings::match($class, '/(?<controller>[A-Za-z0-9]*)Controller$/')['controller'] ?? '';
$templateName .= ':';

// indexAction => index
$templateName .= Strings::match($method, '/(?<method>[A-Za-z]*)Action$/')['method'] ?? '';

// suffix
$templateName .= '.html.twig';

return $templateName;
return sprintf('%s:%s:%s.html.twig', $bundle, $controller, $action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,14 +92,16 @@ public function refactor(Node $classMethodNode): ?Node
$renderArguments
);

// replace Return_ node value if exists
if ($returnNode) {
$returnNode->expr = $thisRenderMethodCall;
} else {
// 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) {
$returnNode->expr = $thisRenderMethodCall;
}

// remove annotation
$this->docBlockAnalyzer->removeAnnotationFromNode($classMethodNode, 'Template');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rectors:
Rector\Rector\Dynamic\ClassReplacerRector:
# http-kernel
Rector\Rector\Contrib\Symfony\HttpKernel\TemplateAnnotationRector: ~
3 changes: 0 additions & 3 deletions src/config/level/symfony/symfony33.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare (strict_types=1);

namespace AppBundle\Controller;

class ClassWithNamedService1Controller extends Controller
{
/**
*/
public function indexAction()
{
return $this->render('@App/NameNotFollowingConvention/index.html.twig', array(
'form' => $form->createView()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare (strict_types=1);

class ClassWithNamedService1 extends Controller
{
/**
*/
public function indexAction()
{
return $this->render('payment/new.html.twig', array(
'form' => $form->createView(),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare (strict_types=1);

namespace AppBundle\Controller;

class ClassWithNamedService1Controller extends Controller
{
/**
* @Template()
*/
public function indexAction()
{
return $this->render('@App/NameNotFollowingConvention/index.html.twig', array(
'form' => $form->createView()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare (strict_types=1);

class ClassWithNamedService1 extends Controller
{
/**
* @Template("AdminBundle:Payment:create.html.twig")
*/
public function indexAction()
{
return $this->render('payment/new.html.twig', array(
'form' => $form->createView(),
));
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cherry picked to #291, thank you