forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertRenderTemplateShortNotationToBundleSyntaxRector.php
More file actions
116 lines (98 loc) · 3.27 KB
/
ConvertRenderTemplateShortNotationToBundleSyntaxRector.php
File metadata and controls
116 lines (98 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony43\Rector\MethodCall;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/symfony/symfony/pull/21035
* @changelog https://github.com/symfony/symfony/blob/4.4/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php
* @changelog https://symfony.com/doc/4.4/templates.html#bundle-templates
*
* @see \Rector\Symfony\Tests\Symfony43\Rector\MethodCall\ConvertRenderTemplateShortNotationToBundleSyntaxRector\ConvertRenderTemplateShortNotationToBundleSyntaxRectorTest
*/
final class ConvertRenderTemplateShortNotationToBundleSyntaxRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change Twig template short name to bundle syntax in render calls from controllers',
[
new CodeSample(
<<<'CODE_SAMPLE'
class BaseController extends Controller {
function indexAction()
{
$this->render('appBundle:Landing\Main:index.html.twig');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class BaseController extends Controller {
function indexAction()
{
$this->render('@app/Landing/Main/index.html.twig');
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isName($node->name, 'render') &&
! $this->nodeNameResolver->isName($node->name, 'renderView')) {
return null;
}
$objectType = $this->nodeTypeResolver->getType($node->var);
$controllerType = new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller');
if (! $controllerType->isSuperTypeOf($objectType)->yes()) {
return null;
}
if (! $objectType instanceof ThisType) {
return null;
}
$args = $node->getArgs();
if (! isset($args[0])) {
return null;
}
$tplName = $this->valueResolver->getValue($args[0]->value);
if ($tplName === null) {
return null;
}
$matches = Strings::match($tplName, '/:/', PREG_OFFSET_CAPTURE);
if ($matches === null) {
return null;
}
$newValue = '@' .
Strings::replace(substr((string) $tplName, 0, $matches[0][1]), '/Bundle/', '') .
Strings::replace(substr((string) $tplName, $matches[0][1]), '/:/', '/');
$newValue = str_replace('\\', '/', $newValue);
$node->args[0] = new Arg(new String_($newValue));
return $node;
}
}