-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
RectorConfigBuilderRector.php
181 lines (154 loc) · 5.93 KB
/
RectorConfigBuilderRector.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
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\FileWithoutNamespace;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector\RectorConfigBuilderRectorTest
*/
final class RectorConfigBuilderRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change RectorConfig to RectorConfigBuilder', [
new CodeSample(
<<<'CODE_SAMPLE'
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(SomeRector::class);
};
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return RectorConfig::configure()->rules([SomeRector::class]);
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FileWithoutNamespace::class];
}
/**
* @param FileWithoutNamespace $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;
foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Return_) {
continue;
}
if (! $stmt->expr instanceof Closure) {
continue;
}
if (count($stmt->expr->params) !== 1) {
continue;
}
$param = $stmt->expr->params[0];
if (! $param->type instanceof FullyQualified) {
continue;
}
if ($param->type->toString() !== 'Rector\Config\RectorConfig') {
continue;
}
$stmts = $stmt->expr->stmts;
if ($stmts === []) {
throw new ShouldNotHappenException('RectorConfig must have at least 1 stmts');
}
$newExpr = new StaticCall(new FullyQualified('Rector\Config\RectorConfig'), 'configure');
$rules = new Array_();
$paths = new Array_();
$skips = new Array_();
$autoloadPaths = new Array_();
$bootstrapFiles = new Array_();
foreach ($stmts as $rectorConfigStmt) {
// complex stmts should be skipped, eg: with if else
if (! $rectorConfigStmt instanceof Expression) {
return null;
}
// debugging or variable init? skip
if (! $rectorConfigStmt->expr instanceof MethodCall) {
return null;
}
// another method call? skip
if (! $this->isObjectType($rectorConfigStmt->expr->var, new ObjectType('Rector\Config\RectorConfig'))) {
return null;
}
if ($rectorConfigStmt->expr->isFirstClassCallable()) {
return null;
}
$args = $rectorConfigStmt->expr->getArgs();
$value = $args[0]->value;
$name = $this->getName($rectorConfigStmt->expr->name);
if ($name === 'rule') {
Assert::isAOf($rules, Array_::class);
$rules->items[] = new ArrayItem($value);
} elseif (($name === 'rules')) {
if ($value instanceof Array_) {
Assert::isAOf($rules, Array_::class);
$rules->items = array_merge($rules->items, $value->items);
} else {
$rules = $value;
}
} elseif ($name === 'paths') {
$paths = $value;
} elseif ($name === 'skip') {
$skips = $value;
} elseif ($name === 'autoloadPaths') {
Assert::isAOf($value, Array_::class);
$autoloadPaths = $value;
} elseif ($name === 'bootstrapFiles') {
Assert::isAOf($value, Array_::class);
$bootstrapFiles = $value;
} else {
// implementing method by method
return null;
}
}
if (! $paths instanceof Array_ || $paths->items !== []) {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withPaths', [$paths]);
$hasChanged = true;
}
if (! $skips instanceof Array_ || $skips->items !== []) {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withSkip', [$skips]);
$hasChanged = true;
}
if (! $rules instanceof Array_ || $rules->items !== []) {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withRules', [$rules]);
$hasChanged = true;
}
if ($autoloadPaths->items !== []) {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withAutoloadPaths', [$autoloadPaths]);
$hasChanged = true;
}
if ($bootstrapFiles->items !== []) {
$newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withBootstrapFiles', [$bootstrapFiles]);
$hasChanged = true;
}
if ($hasChanged) {
$stmt->expr = $newExpr;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
}