-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathTwigTemplateExistsRule.php
More file actions
134 lines (105 loc) · 3.41 KB
/
Copy pathTwigTemplateExistsRule.php
File metadata and controls
134 lines (105 loc) · 3.41 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Symfony;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use function count;
use function file_exists;
use function in_array;
use function is_string;
use function preg_match;
use function sprintf;
/**
* @implements Rule<MethodCall>
*/
final class TwigTemplateExistsRule implements Rule
{
/** @var array<string, string|null> */
private $twigTemplateDirectories;
/** @param array<string, string|null> $twigTemplateDirectories */
public function __construct(array $twigTemplateDirectories)
{
$this->twigTemplateDirectories = $twigTemplateDirectories;
}
public function getNodeType(): string
{
return MethodCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (count($this->twigTemplateDirectories) === 0) {
return [];
}
$templateArg = $this->getTwigTemplateArg($node, $scope);
if ($templateArg === null) {
return [];
}
$templateNames = [];
if ($templateArg->value instanceof Variable && is_string($templateArg->value->name)) {
$varType = $scope->getVariableType($templateArg->value->name);
foreach ($varType->getConstantStrings() as $constantString) {
$templateNames[] = $constantString->getValue();
}
} elseif ($templateArg->value instanceof String_) {
$templateNames[] = $templateArg->value->value;
}
if (count($templateNames) === 0) {
return [];
}
$errors = [];
foreach ($templateNames as $templateName) {
if ($this->twigTemplateExists($templateName)) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Twig template "%s" does not exist.',
$templateName
))->line($templateArg->getStartLine())->identifier('twig.templateNotFound')->build();
}
return $errors;
}
private function getTwigTemplateArg(MethodCall $node, Scope $scope): ?Arg
{
if (!$node->name instanceof Identifier) {
return null;
}
$argType = $scope->getType($node->var);
$methodName = $node->name->name;
if ((new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\AbstractController'))->isSuperTypeOf($argType)->yes() && in_array($methodName, ['render', 'renderView', 'renderBlockView', 'renderBlock', 'renderForm', 'stream'], true)) {
return $node->getArgs()[0] ?? null;
}
if ((new ObjectType('Twig\Environment'))->isSuperTypeOf($argType)->yes() && in_array($methodName, ['render', 'display', 'load'], true)) {
return $node->getArgs()[0] ?? null;
}
if ((new ObjectType('Symfony\Bridge\Twig\Mime\TemplatedEmail'))->isSuperTypeOf($argType)->yes() && in_array($methodName, ['htmlTemplate', 'textTemplate'], true)) {
return $node->getArgs()[0] ?? null;
}
return null;
}
private function twigTemplateExists(string $templateName): bool
{
if (preg_match('#^@(.+)\/(.+)$#', $templateName, $matches) === 1) {
$templateNamespace = $matches[1];
$templateName = $matches[2];
} else {
$templateNamespace = null;
}
foreach ($this->twigTemplateDirectories as $twigTemplateDirectory => $namespace) {
if ($namespace !== $templateNamespace) {
continue;
}
$templatePath = $twigTemplateDirectory . '/' . $templateName;
if (file_exists($templatePath)) {
return true;
}
}
return false;
}
}