-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwigCompleteCheckRule.php
208 lines (186 loc) · 6.98 KB
/
TwigCompleteCheckRule.php
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace Reveal\RevealTwig\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Registry;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use Reveal\RevealTwig\NodeAnalyzer\SymfonyRenderWithParametersMatcher;
use Reveal\TemplatePHPStanCompiler\ErrorSkipper;
use Reveal\TemplatePHPStanCompiler\PHPStan\FileAnalyserProvider;
use Reveal\TemplatePHPStanCompiler\Reporting\TemplateErrorsFactory;
use Reveal\TemplatePHPStanCompiler\TypeAnalyzer\TemplateVariableTypesResolver;
use Reveal\TemplatePHPStanCompiler\ValueObject\VariableAndType;
use Reveal\TwigPHPStanCompiler\TwigToPhpCompiler;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Symplify\SmartFileSystem\SmartFileSystem;
/**
* @implements Rule<MethodCall>
* @see \Reveal\RevealTwig\Tests\Rules\TwigCompleteCheckRule\TwigCompleteCheckRuleTest
*/
final class TwigCompleteCheckRule implements Rule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Complete analysis of PHP code generated from Twig template';
/**
* List of errors, that do not bring any value.
*
* @var string[]
*/
private const ERROR_IGNORES = [
'#Use separate function calls with readable variable names#',
'#Separate function "array_merge\(\)" in method call to standalone row to improve readability#',
'#Array method calls \[\$this, "method"\] are not allowed\. Use explicit method instead to help PhpStorm, PHPStan and Rector understand your code#',
'#Do not use chained method calls\. Put each on separated lines#',
// ob_start contents magic on {% set %} ...
'#Anonymous function should have native return typehint "string"#',
];
/**
* @var \PHPStan\Rules\Registry
*/
private $registry;
/**
* @var \Reveal\RevealTwig\NodeAnalyzer\SymfonyRenderWithParametersMatcher
*/
private $symfonyRenderWithParametersMatcher;
/**
* @var \Reveal\TwigPHPStanCompiler\TwigToPhpCompiler
*/
private $twigToPhpCompiler;
/**
* @var \Symplify\SmartFileSystem\SmartFileSystem
*/
private $smartFileSystem;
/**
* @var \Reveal\TemplatePHPStanCompiler\PHPStan\FileAnalyserProvider
*/
private $fileAnalyserProvider;
/**
* @var \Reveal\TemplatePHPStanCompiler\ErrorSkipper
*/
private $errorSkipper;
/**
* @var \Reveal\TemplatePHPStanCompiler\TypeAnalyzer\TemplateVariableTypesResolver
*/
private $templateVariableTypesResolver;
/**
* @var \Reveal\TemplatePHPStanCompiler\Reporting\TemplateErrorsFactory
*/
private $templateErrorsFactory;
/**
* @param Rule[] $rules
*/
public function __construct(array $rules, SymfonyRenderWithParametersMatcher $symfonyRenderWithParametersMatcher, TwigToPhpCompiler $twigToPhpCompiler, SmartFileSystem $smartFileSystem, FileAnalyserProvider $fileAnalyserProvider, ErrorSkipper $errorSkipper, TemplateVariableTypesResolver $templateVariableTypesResolver, TemplateErrorsFactory $templateErrorsFactory)
{
$this->symfonyRenderWithParametersMatcher = $symfonyRenderWithParametersMatcher;
$this->twigToPhpCompiler = $twigToPhpCompiler;
$this->smartFileSystem = $smartFileSystem;
$this->fileAnalyserProvider = $fileAnalyserProvider;
$this->errorSkipper = $errorSkipper;
$this->templateVariableTypesResolver = $templateVariableTypesResolver;
$this->templateErrorsFactory = $templateErrorsFactory;
$this->registry = new Registry($rules);
}
/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return MethodCall::class;
}
/**
* @param MethodCall $node
* @return array<string|RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
// 1. find twig template file path with array
$renderTemplatesWithParameters = $this->symfonyRenderWithParametersMatcher->matchTwigRender($node, $scope);
// 2. compile twig to PHP with resolved types in @var docs
$ruleErrors = [];
foreach ($renderTemplatesWithParameters as $renderTemplateWithParameter) {
// resolve passed variable types
$variablesAndTypes = $this->templateVariableTypesResolver->resolveArray(
$renderTemplateWithParameter->getParametersArray(),
$scope
);
$currentRuleErrors = $this->processTemplateFilePath(
$renderTemplateWithParameter->getTemplateFilePath(),
$variablesAndTypes,
$scope,
$node->getLine()
);
$ruleErrors = array_merge($ruleErrors, $currentRuleErrors);
}
return $ruleErrors;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
final class SomeController extends AbstractController
{
public function __invoke()
{
return $this->render(__DIR__ . '/some_file.twig', [
'some' => new SomeObject()
]);
}
}
// some_file.twig
{{ some.non_existing_method }}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
final class SomeController extends AbstractController
{
public function __invoke()
{
return $this->render(__DIR__ . '/some_file.twig', [
'some' => new SomeObject()
]);
}
}
// some_file.twig
{{ some.existing_method }}
CODE_SAMPLE
),
]);
}
/**
* @param VariableAndType[] $variablesAndTypes
* @return RuleError[]
*/
private function processTemplateFilePath(
string $templateFilePath,
array $variablesAndTypes,
Scope $scope,
int $phpLine
): array {
$phpFileContentsWithLineMap = $this->twigToPhpCompiler->compileContent($templateFilePath, $variablesAndTypes);
$phpFileContents = $phpFileContentsWithLineMap->getPhpFileContents();
// 4. print the content to temporary file
$tmpFilePath = sys_get_temp_dir() . '/' . md5($scope->getFile()) . '-twig-compiled.php';
$this->smartFileSystem->dumpFile($tmpFilePath, $phpFileContents);
// 5. get file analyser
$fileAnalyser = $this->fileAnalyserProvider->provide();
// 6. analyse temporary PHP file with full PHPStan rules
$fileAnalyserResult = $fileAnalyser->analyseFile($tmpFilePath, [], $this->registry, null);
$ruleErrors = $this->errorSkipper->skipErrors($fileAnalyserResult->getErrors(), self::ERROR_IGNORES);
return $this->templateErrorsFactory->createErrors(
$ruleErrors,
$scope->getFile(),
$templateFilePath,
$phpFileContentsWithLineMap,
$phpLine
);
}
}