forked from driftingly/rector-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEloquentMagicMethodToQueryBuilderRector.php
170 lines (137 loc) · 4.68 KB
/
EloquentMagicMethodToQueryBuilderRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\StaticCall;
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use RectorLaravel\AbstractRector;
use ReflectionException;
use ReflectionMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \RectorLaravel\Tests\Rector\StaticCall\EloquentMagicMethodToQueryBuilderRector\EloquentMagicMethodToQueryBuilderRectorTest
*/
final class EloquentMagicMethodToQueryBuilderRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
final public const EXCLUDE_METHODS = 'exclude_methods';
/**
* @var string[]
*/
private array $excludeMethods = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'The EloquentMagicMethodToQueryBuilderRule is designed to automatically transform certain magic method calls on Eloquent Models into corresponding Query Builder method calls.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use App\Models\User;
$user = User::find(1);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use App\Models\User;
$user = User::query()->find(1);
CODE_SAMPLE
, [
self::EXCLUDE_METHODS => ['find'],
]),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}
/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
$resolvedType = $this->nodeTypeResolver->getType($node->class);
$classNames = $resolvedType->getObjectClassNames();
if ($classNames === []) {
return null;
}
$className = $classNames[0];
$originalClassName = $this->getName($node->class); // like "self" or "App\Models\User"
if ($originalClassName === null) {
return null;
}
// does not extend Eloquent Model
if (! is_subclass_of($className, Model::class)) {
return null;
}
if (! $node->name instanceof Identifier) {
return null;
}
$methodName = $node->name->toString();
// if not a magic method
if (! $this->isMagicMethod($className, $methodName)) {
return null;
}
// if method belongs to Eloquent Query Builder or Query Builder
if (! $this->isPublicMethod(EloquentQueryBuilder::class, $methodName) && ! $this->isPublicMethod(
QueryBuilder::class,
$methodName
)) {
return null;
}
$staticCall = $this->nodeFactory->createStaticCall($originalClassName, 'query');
$methodCall = $this->nodeFactory->createMethodCall($staticCall, $methodName);
foreach ($node->args as $arg) {
$methodCall->args[] = $arg;
}
return $methodCall;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$excludeMethods = $configuration[self::EXCLUDE_METHODS] ?? $configuration;
Assert::isArray($excludeMethods);
Assert::allString($excludeMethods);
$this->excludeMethods = $excludeMethods;
}
public function isMagicMethod(string $className, string $methodName): bool
{
if (in_array($methodName, $this->excludeMethods, true)) {
return false;
}
try {
$reflectionMethod = new ReflectionMethod($className, $methodName);
} catch (ReflectionException) {
return true; // method does not exist => is magic method
}
return false; // not a magic method
}
public function isPublicMethod(string $className, string $methodName): bool
{
try {
$reflectionMethod = new ReflectionMethod($className, $methodName);
// if not public
if (! $reflectionMethod->isPublic()) {
return false;
}
// if static
if ($reflectionMethod->isStatic()) {
return false;
}
} catch (ReflectionException) {
return false; // method does not exist => is magic method
}
return true; // method exist
}
}