Skip to content

Commit 06b1195

Browse files
GeniJahopeterfox
andauthored
Skip refactoring on EloquentOrderByToLatestOrOldestRector when the direction arg is not a string (#310)
Co-authored-by: Peter Fox <[email protected]>
1 parent 08d52b9 commit 06b1195

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

Diff for: src/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
153153
return $methodCall;
154154
}
155155

156+
if (isset($methodCall->args[1]) && (! $methodCall->args[1] instanceof Arg || ! $methodCall->args[1]->value instanceof String_)) {
157+
return $methodCall;
158+
}
159+
156160
if (isset($methodCall->args[1]) && $methodCall->args[1] instanceof Arg && $methodCall->args[1]->value instanceof String_) {
157161
$direction = $methodCall->args[1]->value->value;
158162
} else {
@@ -164,22 +168,22 @@ private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
164168
} else {
165169
$newMethod = $direction === 'asc' ? 'oldest' : 'latest';
166170
}
167-
if ($columnVar instanceof String_ && $columnVar->value === 'created_at') {
168-
$methodCall->name = new Identifier($newMethod);
169-
$methodCall->args = [];
170-
171-
return $methodCall;
172-
}
173171

174-
if ($columnVar instanceof String_) {
175-
$methodCall->name = new Identifier($newMethod);
176-
$methodCall->args = [new Arg(new String_($columnVar->value))];
172+
return $this->createMethodCall($methodCall, $newMethod, $columnVar);
173+
}
177174

178-
return $methodCall;
175+
private function createMethodCall(MethodCall $methodCall, string $newMethod, Expr $expr): MethodCall
176+
{
177+
if ($expr instanceof String_ && $expr->value === 'created_at') {
178+
$args = [];
179+
} elseif ($expr instanceof String_) {
180+
$args = [new Arg(new String_($expr->value))];
181+
} else {
182+
$args = [new Arg($expr)];
179183
}
180184

181185
$methodCall->name = new Identifier($newMethod);
182-
$methodCall->args = [new Arg($columnVar)];
186+
$methodCall->args = $args;
183187

184188
return $methodCall;
185189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
$asc = 'asc';
8+
9+
/** @var Builder $query */
10+
$query->orderBy('created_at', $asc);
11+
12+
?>

0 commit comments

Comments
 (0)