-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathRuleErrorTransformer.php
More file actions
174 lines (150 loc) · 4.56 KB
/
RuleErrorTransformer.php
File metadata and controls
174 lines (150 loc) · 4.56 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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Internal\TokenStream;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\Parser;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\FileReader;
use PHPStan\Fixable\PhpPrinter;
use PHPStan\Fixable\PhpPrinterIndentationDetectorVisitor;
use PHPStan\Fixable\ReplacingNodeVisitor;
use PHPStan\Node\VirtualNode;
use PHPStan\Rules\FileRuleError;
use PHPStan\Rules\FixableNodeRuleError;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\LineRuleError;
use PHPStan\Rules\MetadataRuleError;
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 get_class;
use function hash;
use function str_contains;
use function str_repeat;
#[AutowiredService]
final class RuleErrorTransformer
{
private Differ $differ;
public function __construct(
#[AutowiredParameter(ref: '@currentPhpVersionPhpParser')]
private Parser $parser,
)
{
$this->differ = new Differ(new UnifiedDiffOutputBuilder('', addLineNumbers: true));
}
/**
* @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();
}
}
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;
}
$fixedErrorDiff = null;
if ($ruleError instanceof FixableNodeRuleError) {
if ($ruleError->getOriginalNode() instanceof VirtualNode) {
throw new ShouldNotHappenException('Cannot fix virtual node');
}
$fixingFile = $filePath;
if ($traitFilePath !== null) {
$fixingFile = $traitFilePath;
}
$oldCode = FileReader::read($fixingFile);
$this->parser->parse($oldCode);
$hash = hash('sha256', $oldCode);
$oldTokens = $this->parser->getTokens();
$indentTraverser = new NodeTraverser();
$indentDetector = new PhpPrinterIndentationDetectorVisitor(new TokenStream($oldTokens, PhpPrinter::TAB_WIDTH));
$indentTraverser->addVisitor($indentDetector);
$indentTraverser->traverse($fileNodes);
$cloningTraverser = new NodeTraverser();
$cloningTraverser->addVisitor(new CloningVisitor());
/** @var Stmt[] $newStmts */
$newStmts = $cloningTraverser->traverse($fileNodes);
$traverser = new NodeTraverser();
$visitor = new ReplacingNodeVisitor($ruleError->getOriginalNode(), $ruleError->getNewNodeCallable());
$traverser->addVisitor($visitor);
/** @var Stmt[] $newStmts */
$newStmts = $traverser->traverse($newStmts);
if ($visitor->isFound()) {
if (str_contains($indentDetector->indentCharacter, "\t")) {
$indent = "\t";
} else {
$indent = str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize);
}
$printer = new PhpPrinter(['indent' => $indent]);
$newCode = $printer->printFormatPreserving($newStmts, $fileNodes, $oldTokens);
if ($oldCode !== $newCode) {
$fixedErrorDiff = new FixedErrorDiff($hash, $this->differ->diff($oldCode, $newCode));
}
}
}
return new Error(
$ruleError->getMessage(),
$fileName,
$line,
$canBeIgnored,
$filePath,
$traitFilePath,
$tip,
$node->getStartLine(),
get_class($node),
$identifier,
$metadata,
$fixedErrorDiff,
);
}
}