11
11
use PHPStan \Analyser \Scope ;
12
12
use PHPStan \Rules \Rule ;
13
13
use PHPStan \Rules \RuleErrorBuilder ;
14
+ use PHPStan \Symfony \TwigEnvironmentResolver ;
14
15
use PHPStan \Type \ObjectType ;
15
16
use function count ;
16
- use function file_exists ;
17
17
use function in_array ;
18
18
use function is_string ;
19
- use function preg_match ;
20
19
use function sprintf ;
21
20
22
21
/**
25
24
final class TwigTemplateExistsRule implements Rule
26
25
{
27
26
28
- /** @var array<string, string|null> */
29
- private $ twigTemplateDirectories ;
27
+ /** @var TwigEnvironmentResolver */
28
+ private $ twigEnvironmentResolver ;
30
29
31
- /** @param array<string, string|null> $twigTemplateDirectories */
32
- public function __construct (array $ twigTemplateDirectories )
30
+ public function __construct (TwigEnvironmentResolver $ twigEnvironmentResolver )
33
31
{
34
- $ this ->twigTemplateDirectories = $ twigTemplateDirectories ;
32
+ $ this ->twigEnvironmentResolver = $ twigEnvironmentResolver ;
35
33
}
36
34
37
35
public function getNodeType (): string
@@ -41,10 +39,6 @@ public function getNodeType(): string
41
39
42
40
public function processNode (Node $ node , Scope $ scope ): array
43
41
{
44
- if (count ($ this ->twigTemplateDirectories ) === 0 ) {
45
- return [];
46
- }
47
-
48
42
$ templateArg = $ this ->getTwigTemplateArg ($ node , $ scope );
49
43
50
44
if ($ templateArg === null ) {
@@ -70,7 +64,7 @@ public function processNode(Node $node, Scope $scope): array
70
64
$ errors = [];
71
65
72
66
foreach ($ templateNames as $ templateName ) {
73
- if ($ this ->twigTemplateExists ($ templateName )) {
67
+ if ($ this ->twigEnvironmentResolver -> templateExists ($ templateName )) {
74
68
continue ;
75
69
}
76
70
@@ -107,28 +101,4 @@ private function getTwigTemplateArg(MethodCall $node, Scope $scope): ?Arg
107
101
return null ;
108
102
}
109
103
110
- private function twigTemplateExists (string $ templateName ): bool
111
- {
112
- if (preg_match ('#^@(.+)\/(.+)$# ' , $ templateName , $ matches ) === 1 ) {
113
- $ templateNamespace = $ matches [1 ];
114
- $ templateName = $ matches [2 ];
115
- } else {
116
- $ templateNamespace = null ;
117
- }
118
-
119
- foreach ($ this ->twigTemplateDirectories as $ twigTemplateDirectory => $ namespace ) {
120
- if ($ namespace !== $ templateNamespace ) {
121
- continue ;
122
- }
123
-
124
- $ templatePath = $ twigTemplateDirectory . '/ ' . $ templateName ;
125
-
126
- if (file_exists ($ templatePath )) {
127
- return true ;
128
- }
129
- }
130
-
131
- return false ;
132
- }
133
-
134
104
}
0 commit comments