forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractRector.php
380 lines (318 loc) · 11.1 KB
/
AbstractRector.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
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
<?php
declare(strict_types=1);
namespace Rector\Core\Rector;
use Nette\Utils\Strings;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Exclusion\ExclusionManager;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector\AbstractRectorTrait;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorInterface
{
use AbstractRectorTrait;
/**
* @var string[]
*/
private const ATTRIBUTES_TO_MIRROR = [
AttributeKey::PARENT_NODE,
AttributeKey::CLASS_NODE,
AttributeKey::CLASS_NAME,
AttributeKey::FILE_INFO,
AttributeKey::METHOD_NODE,
AttributeKey::USE_NODES,
AttributeKey::SCOPE,
AttributeKey::METHOD_NAME,
AttributeKey::NAMESPACE_NAME,
AttributeKey::NAMESPACE_NODE,
AttributeKey::RESOLVED_NAME,
];
/**
* @var string
*/
private const COMMENTS = 'comments';
/**
* @var BuilderFactory
*/
protected $builderFactory;
/**
* @var ParameterProvider
*/
protected $parameterProvider;
/**
* @var PhpVersionProvider
*/
protected $phpVersionProvider;
/**
* @var PhpDocInfoPrinter
*/
protected $phpDocInfoPrinter;
/**
* @var PhpDocInfoFactory
*/
protected $phpDocInfoFactory;
/**
* @var DocBlockManipulator
*/
protected $docBlockManipulator;
/**
* @var StaticTypeMapper
*/
protected $staticTypeMapper;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
/**
* @var ExclusionManager
*/
private $exclusionManager;
/**
* @var CurrentRectorProvider
*/
private $currentRectorProvider;
/**
* @required
*/
public function autowireAbstractRectorDependencies(
SymfonyStyle $symfonyStyle,
PhpVersionProvider $phpVersionProvider,
BuilderFactory $builderFactory,
ExclusionManager $exclusionManager,
PhpDocInfoPrinter $phpDocInfoPrinter,
PhpDocInfoFactory $phpDocInfoFactory,
DocBlockManipulator $docBlockManipulator,
StaticTypeMapper $staticTypeMapper,
ParameterProvider $parameterProvider,
CurrentRectorProvider $currentRectorProvider
): void {
$this->symfonyStyle = $symfonyStyle;
$this->phpVersionProvider = $phpVersionProvider;
$this->builderFactory = $builderFactory;
$this->exclusionManager = $exclusionManager;
$this->phpDocInfoPrinter = $phpDocInfoPrinter;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->docBlockManipulator = $docBlockManipulator;
$this->staticTypeMapper = $staticTypeMapper;
$this->parameterProvider = $parameterProvider;
$this->currentRectorProvider = $currentRectorProvider;
}
/**
* @return int|Node|null
*/
final public function enterNode(Node $node)
{
if (! $this->isMatchingNodeType(get_class($node))) {
return null;
}
$this->currentRectorProvider->changeCurrentRector($this);
// already removed
if ($this->isNodeRemoved($node)) {
return null;
}
if ($this->exclusionManager->isNodeSkippedByRector($this, $node)) {
return null;
}
// show current Rector class on --debug
if ($this->symfonyStyle->isDebug()) {
// indented on purpose to improve log nesting under [refactoring]
$this->symfonyStyle->writeln(' [applying] ' . static::class);
}
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? clone $node;
$originalNodeWithAttributes = clone $node;
$node = $this->refactor($node);
// nothing to change → continue
if ($node === null) {
return null;
}
// changed!
if ($this->hasNodeChanged($originalNode, $node)) {
$this->mirrorAttributes($originalNodeWithAttributes, $node);
$this->updateAttributes($node);
$this->keepFileInfoAttribute($node, $originalNode);
$this->notifyNodeFileInfo($node);
}
// if stmt ("$value;") was replaced by expr ("$value"), add the ending ";" (Expression) to prevent breaking the code
if ($originalNode instanceof Stmt && $node instanceof Expr) {
return new Expression($node);
}
return $node;
}
protected function getNextExpression(Node $node): ?Node
{
$currentExpression = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentExpression instanceof Expression) {
return null;
}
return $currentExpression->getAttribute(AttributeKey::NEXT_NODE);
}
/**
* @param Expr[]|null[] $nodes
* @param mixed[] $expectedValues
*/
protected function areValues(array $nodes, array $expectedValues): bool
{
foreach ($nodes as $i => $node) {
if ($node !== null && $this->isValue($node, $expectedValues[$i])) {
continue;
}
return false;
}
return true;
}
protected function isAtLeastPhpVersion(string $version): bool
{
return $this->phpVersionProvider->isAtLeast($version);
}
protected function isAnonymousClass(Node $node): bool
{
if (! $node instanceof Class_) {
return false;
}
$className = $this->nodeNameResolver->getName($node);
return $className === null || Strings::contains($className, 'AnonymousClass');
}
protected function createCountedValueName(string $countedValueName, ?Scope $scope): string
{
if ($scope === null) {
return $countedValueName;
}
// make sure variable name is unique
if (! $scope->hasVariableType($countedValueName)->yes()) {
return $countedValueName;
}
// we need to add number suffix until the variable is unique
$i = 2;
$countedValueNamePart = $countedValueName;
while ($scope->hasVariableType($countedValueName)->yes()) {
$countedValueName = $countedValueNamePart . $i;
++$i;
}
return $countedValueName;
}
protected function mirrorComments(Node $newNode, Node $oldNode): void
{
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO));
$newNode->setAttribute(self::COMMENTS, $oldNode->getAttribute(self::COMMENTS));
}
/**
* @param Stmt[] $stmts
*/
protected function unwrapStmts(array $stmts, Node $node): void
{
foreach ($stmts as $key => $ifStmt) {
if ($key === 0) {
// move /* */ doc block from if to first element to keep it
$currentPhpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
$ifStmt->setAttribute(AttributeKey::PHP_DOC_INFO, $currentPhpDocInfo);
// move // comments
$ifStmt->setAttribute(self::COMMENTS, $node->getComments());
}
$this->addNodeAfterNode($ifStmt, $node);
}
}
protected function isOnClassMethodCall(Node $node, string $type, string $methodName): bool
{
if (! $node instanceof MethodCall) {
return false;
}
if (! $this->isObjectType($node->var, $type)) {
return false;
}
return $this->isName($node->name, $methodName);
}
protected function isOpenSourceProjectType(): bool
{
$projectType = $this->parameterProvider->provideParameter(Option::PROJECT_TYPE);
return in_array(
$projectType,
// make it typo proof
[Option::PROJECT_TYPE_OPEN_SOURCE, Option::PROJECT_TYPE_OPEN_SOURCE_UNDESCORED],
true
);
}
/**
* @param Expr $expr
*/
protected function createBoolCast(?Node $parentNode, Node $expr): Bool_
{
if ($parentNode instanceof Return_ && $expr instanceof Assign) {
$expr = $expr->expr;
}
return new Bool_($expr);
}
private function isMatchingNodeType(string $nodeClass): bool
{
foreach ($this->getNodeTypes() as $nodeType) {
if (is_a($nodeClass, $nodeType, true)) {
return true;
}
}
return false;
}
private function keepFileInfoAttribute(Node $node, Node $originalNode): void
{
if ($node->getAttribute(AttributeKey::FILE_INFO) instanceof SmartFileInfo) {
return;
}
if ($originalNode->getAttribute(AttributeKey::FILE_INFO) !== null) {
$node->setAttribute(AttributeKey::FILE_INFO, $originalNode->getAttribute(AttributeKey::FILE_INFO));
} elseif ($originalNode->getAttribute(AttributeKey::PARENT_NODE) !== null) {
/** @var Node $parentOriginalNode */
$parentOriginalNode = $originalNode->getAttribute(AttributeKey::PARENT_NODE);
$node->setAttribute(AttributeKey::FILE_INFO, $parentOriginalNode->getAttribute(AttributeKey::FILE_INFO));
}
}
private function mirrorAttributes(Node $oldNode, Node $newNode): void
{
foreach ($oldNode->getAttributes() as $attributeName => $oldNodeAttributeValue) {
if (! in_array($attributeName, self::ATTRIBUTES_TO_MIRROR, true)) {
continue;
}
$newNode->setAttribute($attributeName, $oldNodeAttributeValue);
}
}
private function updateAttributes(Node $node): void
{
// update Resolved name attribute if name is changed
if ($node instanceof Name) {
$node->setAttribute(AttributeKey::RESOLVED_NAME, $node->toString());
}
}
private function hasNodeChanged(Node $originalNode, Node $node): bool
{
if ($this->isNameIdentical($node, $originalNode)) {
return false;
}
return ! $this->areNodesEqual($originalNode, $node);
}
private function isNameIdentical(Node $node, Node $originalNode): bool
{
if (! $originalNode instanceof Name) {
return false;
}
// names are the same
return $this->areNodesEqual($originalNode->getAttribute(AttributeKey::ORIGINAL_NAME), $node);
}
}