forked from driftingly/rector-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelFactoryNodeFactory.php
More file actions
179 lines (148 loc) · 5.58 KB
/
Copy pathModelFactoryNodeFactory.php
File metadata and controls
179 lines (148 loc) · 5.58 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
<?php
declare(strict_types=1);
namespace RectorLaravel\NodeFactory;
use PhpParser\Builder\Method;
use PhpParser\Builder\Property;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PhpParser\Node\Value\ValueResolver;
final readonly class ModelFactoryNodeFactory
{
private const string THIS = 'this';
public function __construct(
private NodeNameResolver $nodeNameResolver,
private NodeFactory $nodeFactory,
private ValueResolver $valueResolver,
) {}
public function createEmptyFactory(string $name, Expr $expr): Class_
{
$class = new Class_($name . 'Factory', [], [
'startLine' => $expr->getStartLine(),
'endLine' => $expr->getEndLine(),
]);
$class->extends = new FullyQualified('Illuminate\Database\Eloquent\Factories\Factory');
$propertyBuilder = new Property('model');
$propertyBuilder->makeProtected();
$propertyBuilder->setDefault($expr);
$property = $propertyBuilder->getNode();
$class->stmts[] = $property;
// decorate with namespaced names
$nameResolver = new NameResolver(null, [
'replaceNodes' => false,
'preserveOriginalNames' => true,
]);
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor($nameResolver);
$nodeTraverser->traverse([$class]);
return $class;
}
public function createDefinition(Closure|ArrowFunction $callable): ClassMethod
{
if (isset($callable->params[0])) {
$this->fakerVariableToPropertyFetch($callable->getStmts(), $callable->params[0]);
}
return $this->createPublicMethod('definition', $callable->getStmts());
}
public function createStateMethod(MethodCall $methodCall): ?ClassMethod
{
if (! isset($methodCall->args[2])) {
return null;
}
if (! $methodCall->args[2] instanceof Arg) {
return null;
}
$thirdArgValue = $methodCall->args[2]->value;
// the third argument may be closure or array
if (($thirdArgValue instanceof Closure || $thirdArgValue instanceof ArrowFunction) && isset($thirdArgValue->params[0])) {
$this->fakerVariableToPropertyFetch($thirdArgValue->getStmts(), $thirdArgValue->params[0]);
unset($thirdArgValue->params[0]);
}
$expr = $this->nodeFactory->createMethodCall(self::THIS, 'state', [$methodCall->args[2]]);
$return = new Return_($expr);
if (! isset($methodCall->args[1])) {
return null;
}
if (! $methodCall->args[1] instanceof Arg) {
return null;
}
$methodName = $this->valueResolver->getValue($methodCall->args[1]->value);
if (! is_string($methodName)) {
return null;
}
return $this->createPublicMethod($methodName, [$return]);
}
public function createEmptyConfigure(): ClassMethod
{
$return = new Return_(new Variable(self::THIS));
return $this->createPublicMethod('configure', [$return]);
}
public function appendConfigure(ClassMethod $classMethod, string $name, Closure|ArrowFunction $callable): void
{
SimpleCallableNodeTraverser::traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $node) use ($callable, $name): ?Return_ {
if (! $node instanceof Return_) {
return null;
}
if (! $node->expr instanceof Expr) {
return null;
}
if (isset($callable->params[1])) {
$this->fakerVariableToPropertyFetch($callable->getStmts(), $callable->params[1]);
// remove argument $faker
unset($callable->params[1]);
}
$node->expr = $this->nodeFactory->createMethodCall($node->expr, $name, [$callable]);
return $node;
}
);
}
/**
* @param Node\Stmt[] $stmts
*/
private function fakerVariableToPropertyFetch(array $stmts, Param $param): void
{
SimpleCallableNodeTraverser::traverseNodesWithCallable($stmts, function (Node $node) use (
$param
): ?PropertyFetch {
if (! $node instanceof Variable) {
return null;
}
$name = $this->nodeNameResolver->getName($param->var);
if ($name === null) {
return null;
}
if (! $this->nodeNameResolver->isName($node, $name)) {
return null;
}
// use $this->faker instead of $faker
return $this->nodeFactory->createPropertyFetch(self::THIS, 'faker');
});
}
/**
* @param Node\Stmt[] $stmts
*/
private function createPublicMethod(string $name, array $stmts): ClassMethod
{
$method = new Method($name);
$method->makePublic();
$method->addStmts($stmts);
return $method->getNode();
}
}