-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathFactoryDefinitionRector.php
251 lines (204 loc) · 6.84 KB
/
FactoryDefinitionRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\Namespace_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Namespace_;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use RectorLaravel\AbstractRector;
use RectorLaravel\NodeFactory\ModelFactoryNodeFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://laravel.com/docs/7.x/database-testing#writing-factories
* @changelog https://laravel.com/docs/8.x/database-testing#defining-model-factories
*
* @see \RectorLaravel\Tests\Rector\Namespace_\FactoryDefinitionRector\FactoryDefinitionRectorTest
*/
final class FactoryDefinitionRector extends AbstractRector
{
public function __construct(
private readonly ModelFactoryNodeFactory $modelFactoryNodeFactory
) {}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Upgrade legacy factories to support classes.', [
new CodeSample(
<<<'CODE_SAMPLE'
use Faker\Generator as Faker;
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
];
});
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Faker\Generator as Faker;
class UserFactory extends \Illuminate\Database\Eloquent\Factories\Factory
{
protected $model = App\User::class;
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
];
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Namespace_::class, FileWithoutNamespace::class];
}
/**
* @param Namespace_|FileWithoutNamespace $node
*/
public function refactor(Node $node): ?Node
{
$factories = [];
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
// do not refactor if factory variable changed
if ($this->isReAssignFactoryVariable($stmt->expr)) {
return null;
}
if (! $stmt->expr instanceof MethodCall) {
continue;
}
if ($this->shouldSkipExpression($stmt->expr)) {
continue;
}
/** @var Arg $firstArg */
$firstArg = $stmt->expr->args[0];
$name = $this->getNameFromClassConstFetch($firstArg->value);
if (! $name instanceof Name) {
continue;
}
if (! isset($factories[$name->toString()])) {
$factories[$name->toString()] = $this->createFactory($name->getLast(), $firstArg->value);
}
$this->processFactoryConfiguration($factories[$name->toString()], $stmt->expr);
unset($node->stmts[$key]);
}
if ($factories === []) {
return null;
}
$node->stmts = array_merge($node->stmts, $factories);
return $node;
}
private function isReAssignFactoryVariable(Expr $expr): bool
{
if (! $expr instanceof Assign) {
return false;
}
return $this->isName($expr->var, 'factory');
}
private function shouldSkipExpression(MethodCall $methodCall): bool
{
if (! isset($methodCall->args[0])) {
return true;
}
if (! $methodCall->args[0] instanceof Arg) {
return true;
}
if (! $methodCall->args[0]->value instanceof ClassConstFetch) {
return true;
}
return ! $this->isNames($methodCall->name, ['define', 'state', 'afterMaking', 'afterCreating']);
}
private function getNameFromClassConstFetch(Expr $expr): ?Name
{
if (! $expr instanceof ClassConstFetch) {
return null;
}
if ($expr->class instanceof Expr) {
return null;
}
return $expr->class;
}
private function createFactory(string $name, Expr $expr): Class_
{
return $this->modelFactoryNodeFactory->createEmptyFactory($name, $expr);
}
private function processFactoryConfiguration(Class_ $class, MethodCall $methodCall): void
{
if (! $this->isName($methodCall->var, 'factory')) {
return;
}
if ($this->isName($methodCall->name, 'define')) {
$this->addDefinition($class, $methodCall);
}
if ($this->isName($methodCall->name, 'state')) {
$this->addState($class, $methodCall);
}
if (! $this->isNames($methodCall->name, ['afterMaking', 'afterCreating'])) {
return;
}
$name = $this->getName($methodCall->name);
if ($name === null) {
return;
}
$this->appendAfterCalling($class, $methodCall, $name);
}
private function addDefinition(Class_ $class, MethodCall $methodCall): void
{
if (! isset($methodCall->args[1])) {
return;
}
if (! $methodCall->args[1] instanceof Arg) {
return;
}
$callable = $methodCall->args[1]->value;
if (! $callable instanceof Closure && ! $callable instanceof ArrowFunction) {
return;
}
$class->stmts[] = $this->modelFactoryNodeFactory->createDefinition($callable);
}
private function addState(Class_ $class, MethodCall $methodCall): void
{
$classMethod = $this->modelFactoryNodeFactory->createStateMethod($methodCall);
if (! $classMethod instanceof ClassMethod) {
return;
}
$class->stmts[] = $classMethod;
}
private function appendAfterCalling(Class_ $class, MethodCall $methodCall, string $name): void
{
if (! isset($methodCall->args[1])) {
return;
}
if (! $methodCall->args[1] instanceof Arg) {
return;
}
$callable = $methodCall->args[1]->value;
if (! $callable instanceof Closure && ! $callable instanceof ArrowFunction) {
return;
}
$method = $class->getMethod('configure');
if (! $method instanceof ClassMethod) {
$method = $this->modelFactoryNodeFactory->createEmptyConfigure();
$class->stmts[] = $method;
}
$this->modelFactoryNodeFactory->appendConfigure($method, $name, $callable);
}
}