-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathEloquentOrderByToLatestOrOldestRector.php
186 lines (153 loc) · 5.7 KB
/
EloquentOrderByToLatestOrOldestRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \RectorLaravel\Tests\Rector\MethodCall\EloquentOrderByToLatestOrOldestRector\EloquentOrderByToLatestOrOldestRectorTest
*/
class EloquentOrderByToLatestOrOldestRector extends AbstractRector implements ConfigurableRectorInterface
{
final public const string ALLOWED_PATTERNS = 'allowed_patterns';
/**
* @var string[]
*/
private array $allowedPatterns = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes orderBy() to latest() or oldest()',
[
new ConfiguredCodeSample(<<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Builder;
$column = 'tested_at';
$builder->orderBy('created_at');
$builder->orderBy('created_at', 'desc');
$builder->orderBy('submitted_at');
$builder->orderByDesc('submitted_at');
$builder->orderBy($allowed_variable_name);
$builder->orderBy($unallowed_variable_name);
$builder->orderBy('unallowed_column_name');
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Builder;
$column = 'tested_at';
$builder->oldest();
$builder->latest();
$builder->oldest('submitted_at');
$builder->latest('submitted_at');
$builder->oldest($allowed_variable_name);
$builder->orderBy($unallowed_variable_name);
$builder->orderBy('unallowed_column_name');
CODE_SAMPLE
, [self::ALLOWED_PATTERNS => [
'submitted_a*',
'*tested_at',
'$allowed_variable_name', ]]),
]
);
}
public function getNodeTypes(): array
{
return [MethodCall::class];
}
public function refactor(Node $node): ?Node
{
if (! $node instanceof MethodCall) {
return null;
}
if ($this->isOrderByMethodCall($node) && $this->isAllowedPattern($node)) {
return $this->convertOrderByToLatest($node);
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$allowedPatterns = $configuration[self::ALLOWED_PATTERNS] ?? [];
Assert::isArray($allowedPatterns);
Assert::allString($allowedPatterns);
$this->allowedPatterns = $allowedPatterns;
}
private function isOrderByMethodCall(MethodCall $methodCall): bool
{
// Check if it's a method call to `orderBy`
return $this->isObjectType($methodCall->var, new ObjectType('Illuminate\Database\Query\Builder'))
&& $methodCall->name instanceof Identifier
&& ($methodCall->name->name === 'orderBy' || $methodCall->name->name === 'orderByDesc')
&& $methodCall->args !== [];
}
private function isAllowedPattern(MethodCall $methodCall): bool
{
$columnArg = $methodCall->args[0] instanceof Arg ? $methodCall->args[0]->value : null;
// If no patterns are specified, consider all column names as matching
if ($this->allowedPatterns === []) {
return true;
}
if ($columnArg instanceof String_) {
$columnName = $columnArg->value;
// If specified, only allow certain patterns
foreach ($this->allowedPatterns as $allowedPattern) {
if (fnmatch($allowedPattern, $columnName)) {
return true;
}
}
}
if ($columnArg instanceof Variable && is_string($columnArg->name)) {
// Check against allowed patterns
foreach ($this->allowedPatterns as $allowedPattern) {
if (fnmatch(ltrim($allowedPattern, '$'), $columnArg->name)) {
return true;
}
}
}
return false;
}
private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
{
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof VariadicPlaceholder) {
return $methodCall;
}
$columnVar = $methodCall->args[0] instanceof Arg ? $methodCall->args[0]->value : null;
if (! $columnVar instanceof Expr) {
return $methodCall;
}
if (isset($methodCall->args[1]) && $methodCall->args[1] instanceof Arg && $methodCall->args[1]->value instanceof String_) {
$direction = $methodCall->args[1]->value->value;
} else {
$direction = 'asc';
}
if ($this->isName($methodCall->name, 'orderByDesc')) {
$newMethod = 'latest';
} else {
$newMethod = $direction === 'asc' ? 'oldest' : 'latest';
}
if ($columnVar instanceof String_ && $columnVar->value === 'created_at') {
$methodCall->name = new Identifier($newMethod);
$methodCall->args = [];
return $methodCall;
}
if ($columnVar instanceof String_) {
$methodCall->name = new Identifier($newMethod);
$methodCall->args = [new Arg(new String_($columnVar->value))];
return $methodCall;
}
$methodCall->name = new Identifier($newMethod);
$methodCall->args = [new Arg($columnVar)];
return $methodCall;
}
}