Repository navigation
Expand file tree
/
Copy pathRuleErrorTransformer.php
More file actions
396 lines (345 loc) · 11.3 KB
/
Copy pathRuleErrorTransformer.php
File metadata and controls
396 lines (345 loc) · 11.3 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\ErrorHandler\Collecting;
use PhpParser\Internal\TokenStream;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser;
use PhpParser\Token;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\FileReader;
use PHPStan\Fixable\NewClassNamesVisitor;
use PHPStan\Fixable\PathCloningVisitor;
use PHPStan\Fixable\PhpPrinter;
use PHPStan\Fixable\PhpPrinterIndentationDetectorVisitor;
use PHPStan\Fixable\ReplacingNodeVisitor;
use PHPStan\Fixable\SharedNodeCloningVisitor;
use PHPStan\Node\VirtualNode;
use PHPStan\Parser\Parser as AnalysisParser;
use PHPStan\Parser\ParserErrorsException;
use PHPStan\Rules\FileRuleError;
use PHPStan\Rules\FixableNodeRuleError;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\LineRuleError;
use PHPStan\Rules\MetadataRuleError;
use PHPStan\Rules\NodeFix;
use PHPStan\Rules\NonIgnorableRuleError;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrors\TransformedRuleError;
use PHPStan\Rules\TipRuleError;
use PHPStan\ShouldNotHappenException;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use function array_key_exists;
use function array_map;
use function array_shift;
use function array_slice;
use function count;
use function get_class;
use function hash;
use function is_file;
use function min;
use function preg_split;
use function str_contains;
use function str_repeat;
use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;
#[AutowiredService]
final class RuleErrorTransformer
{
private const PARSED_FILES_LIMIT = 4;
private const PRINTERS_LIMIT = 4;
private Differ $differ;
private UnifiedDiffOutputBuilder $diffOutputBuilder;
/** @var array<string, array{code: string, hash: string, tokens: Token[], indent: string|null}> */
private array $parsedFiles = [];
/** @var array<string, PhpPrinter> */
private array $printers = [];
public function __construct(
#[AutowiredParameter(ref: '@currentPhpVersionPhpParser')]
private Parser $parser,
#[AutowiredParameter(ref: '@defaultAnalysisParser')]
private AnalysisParser $analysisParser,
#[AutowiredParameter(ref: '%fixer.importNames%')]
private bool $importNames,
#[AutowiredParameter(ref: '%fixer.preferSelf%')]
private bool $preferSelf,
)
{
$this->diffOutputBuilder = new UnifiedDiffOutputBuilder('', addLineNumbers: true);
$this->differ = new Differ($this->diffOutputBuilder);
}
/**
* @param Node\Stmt[] $fileNodes
*/
public function transform(
RuleError $ruleError,
Scope $scope,
array $fileNodes,
Node $node,
): Error
{
if ($ruleError instanceof TransformedRuleError) {
return $ruleError->getError();
}
$line = $node->getStartLine();
$canBeIgnored = true;
$fileName = $scope->getFileDescription();
$filePath = $scope->getFile();
$traitFilePath = null;
$tip = null;
$identifier = null;
$metadata = [];
if ($scope->isInTrait()) {
$traitReflection = $scope->getTraitReflection();
if ($traitReflection->getFileName() !== null) {
$traitFilePath = $traitReflection->getFileName();
}
}
// the file $fileNodes come from - the error can point to another one with RuleErrorBuilder::file()
$fileNodesFile = $traitFilePath ?? $filePath;
if (
$ruleError instanceof LineRuleError
&& $ruleError->getLine() !== -1
) {
$line = $ruleError->getLine();
}
if (
$ruleError instanceof FileRuleError
&& $ruleError->getFile() !== ''
) {
$fileName = $ruleError->getFileDescription();
$filePath = $ruleError->getFile();
$traitFilePath = null;
}
if ($ruleError instanceof TipRuleError) {
$tip = $ruleError->getTip();
}
if ($ruleError instanceof IdentifierRuleError) {
$identifier = $ruleError->getIdentifier();
}
if ($ruleError instanceof MetadataRuleError) {
$metadata = $ruleError->getMetadata();
}
if ($ruleError instanceof NonIgnorableRuleError) {
$canBeIgnored = false;
}
$fixedErrorDiffs = [];
if ($ruleError instanceof FixableNodeRuleError) {
$fixesByFile = [];
foreach ($ruleError->getNodeFixes() as $nodeFix) {
if ($nodeFix->node instanceof VirtualNode) {
throw new ShouldNotHappenException('Cannot fix virtual node');
}
$fixesByFile[$nodeFix->file ?? $traitFilePath ?? $filePath][] = $nodeFix;
}
foreach ($fixesByFile as $fixingFile => $fixes) {
$fixedErrorDiff = $this->fix(
$fixingFile,
$fixingFile === $fileNodesFile ? $fileNodes : [],
$fixes,
);
if ($fixedErrorDiff === null) {
// the changes are applied together or not at all
$fixedErrorDiffs = [];
break;
}
$fixedErrorDiffs[] = $fixedErrorDiff;
}
}
return new Error(
$ruleError->getMessage(),
$fileName,
$line,
$canBeIgnored,
$filePath,
$traitFilePath,
$tip,
$node->getStartLine(),
get_class($node),
$identifier,
$metadata,
$fixedErrorDiffs,
);
}
/**
* Without $fileNodes (errors of CollectedDataNode rules, errors transformed inside collectors,
* and fixes in another file), the file is parsed with the cached analysis parser. ReplacingNodeVisitor
* finds the fixed nodes by their position, so the rule has to take them from a parse of the same file contents.
* Returns null when some of the nodes cannot be fixed.
*
* @param Node\Stmt[] $fileNodes
* @param non-empty-list<NodeFix> $fixes
*/
private function fix(string $fixingFile, array $fileNodes, array $fixes): ?FixedErrorDiff
{
if ($fileNodes === []) {
if (!is_file($fixingFile)) {
return null;
}
try {
$fileNodes = $this->analysisParser->parseFile($fixingFile);
} catch (ParserErrorsException) {
return null;
}
if ($fileNodes === []) {
return null;
}
}
$parsedFile = $this->getParsedFile($fixingFile);
$indentDetector = null;
$pathCloningVisitor = null;
$visitors = [];
$fixedNodes = array_map(static fn (NodeFix $fix): Node => $fix->node, $fixes);
if ($parsedFile['indent'] === null) {
$indentDetector = new PhpPrinterIndentationDetectorVisitor(new TokenStream($parsedFile['tokens'], PhpPrinter::TAB_WIDTH));
$visitors[] = $indentDetector;
$visitors[] = new CloningVisitor();
} elseif (PathCloningVisitor::canFind($fixedNodes)) {
$pathCloningVisitor = new PathCloningVisitor($fixedNodes);
$visitors[] = $pathCloningVisitor;
} else {
$visitors[] = new CloningVisitor();
}
$replacingVisitor = new ReplacingNodeVisitor($fixes);
$visitors[] = $replacingVisitor;
try {
/** @var Stmt[] $newStmts */
$newStmts = (new NodeTraverser(...$visitors))->traverse($fileNodes);
$indent = $parsedFile['indent'];
if ($indentDetector !== null) {
if (str_contains($indentDetector->indentCharacter, "\t")) {
$indent = "\t";
} else {
$indent = str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize);
}
if ($indentDetector->found) {
$this->parsedFiles[$fixingFile]['indent'] = $indent;
}
}
if (!$replacingVisitor->isComplete()) {
return null;
}
if (NewClassNamesVisitor::hasNewNames($replacingVisitor->getNewNodes())) {
if ($pathCloningVisitor !== null) {
// the visitors below change nodes in place, so the original nodes must not be shared
/** @var Stmt[] $newStmts */
$newStmts = (new NodeTraverser(new SharedNodeCloningVisitor()))->traverse($newStmts);
}
$nameResolver = new NameResolver(new Collecting(), ['replaceNodes' => false]);
/** @var Stmt[] $newStmts */
$newStmts = (new NodeTraverser(
$nameResolver,
new NewClassNamesVisitor($nameResolver->getNameContext(), $this->importNames, $this->preferSelf),
))->traverse($newStmts);
}
$newCode = $this->getPrinter($indent)->printFormatPreserving($newStmts, $fileNodes, $parsedFile['tokens']);
if ($parsedFile['code'] === $newCode) {
return null;
}
return new FixedErrorDiff($fixingFile, $parsedFile['hash'], $this->diff($parsedFile['code'], $newCode));
} finally {
$pathCloningVisitor?->restoreOriginalNodes();
}
}
/**
* A fix that is not made through nodes, like removing a comment.
*/
public function createFixedErrorDiff(string $file, string $originalCode, string $newCode): FixedErrorDiff
{
return new FixedErrorDiff($file, hash('sha256', $originalCode), $this->diff($originalCode, $newCode));
}
/**
* Same output as Differ::diff(), but the common leading and trailing lines are found here in linear time.
* Differ strips them too, but in quadratic time, which made each fix in a large file cost up to a second.
* The middle part is usually just the fixed node.
*/
private function diff(string $from, string $to): string
{
$fromLines = self::splitStringByLines($from);
$toLines = self::splitStringByLines($to);
$fromCount = count($fromLines);
$toCount = count($toLines);
$minCount = min($fromCount, $toCount);
$startCount = 0;
while ($startCount < $minCount && $fromLines[$startCount] === $toLines[$startCount]) {
$startCount++;
}
$endCount = 0;
while (
$endCount < $minCount - $startCount
&& $fromLines[$fromCount - 1 - $endCount] === $toLines[$toCount - 1 - $endCount]
) {
$endCount++;
}
$diff = [];
for ($i = 0; $i < $startCount; $i++) {
$diff[] = [$fromLines[$i], Differ::OLD];
}
foreach ($this->differ->diffToArray(
array_slice($fromLines, $startCount, $fromCount - $startCount - $endCount),
array_slice($toLines, $startCount, $toCount - $startCount - $endCount),
) as $entry) {
if ($entry[1] === Differ::DIFF_LINE_END_WARNING) {
// the warning would be printed as a context line and shift the line numbers in Patcher
continue;
}
$diff[] = $entry;
}
for ($i = $fromCount - $endCount; $i < $fromCount; $i++) {
$diff[] = [$fromLines[$i], Differ::OLD];
}
return $this->diffOutputBuilder->getDiff($diff);
}
/**
* @return list<string>
*/
private static function splitStringByLines(string $input): array
{
$lines = preg_split('/(.*\R)/', $input, flags: PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
if ($lines === false) {
throw new ShouldNotHappenException();
}
return $lines;
}
/**
* @return array{code: string, hash: string, tokens: Token[], indent: string|null}
*/
private function getParsedFile(string $file): array
{
// the file can change between analyses in one process, like PHPStan Pro or tests
$code = FileReader::read($file);
if (array_key_exists($file, $this->parsedFiles) && $this->parsedFiles[$file]['code'] === $code) {
return $this->parsedFiles[$file];
}
unset($this->parsedFiles[$file]);
$this->parser->parse($code);
if (count($this->parsedFiles) >= self::PARSED_FILES_LIMIT) {
array_shift($this->parsedFiles);
}
return $this->parsedFiles[$file] = [
'code' => $code,
'hash' => hash('sha256', $code),
'tokens' => $this->parser->getTokens(),
'indent' => null,
];
}
/**
* Printers are reused because PhpPrinter memoizes several sizeable lookup tables
* the first time printFormatPreserving() is called on it.
*/
private function getPrinter(string $indent): PhpPrinter
{
if (array_key_exists($indent, $this->printers)) {
return $this->printers[$indent];
}
if (count($this->printers) >= self::PRINTERS_LIMIT) {
array_shift($this->printers);
}
return $this->printers[$indent] = new PhpPrinter(['indent' => $indent]);
}
}