Skip to content

Commit

Permalink
Fixes a mix up between latest() and oldest() (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox authored Oct 11, 2023
1 parent 4e05cbb commit 3054e6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ Changes `orderBy()` to `latest()` or `oldest()`
-$builder->orderBy('created_at');
-$builder->orderBy('created_at', 'desc');
-$builder->orderBy('deleted_at');
+$builder->latest();
+$builder->oldest();
+$builder->latest('deleted_at');
+$builder->latest();
+$builder->oldest('deleted_at');
```

<br>
Expand Down
35 changes: 15 additions & 20 deletions src/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
namespace RectorLaravel\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -38,9 +33,9 @@ public function getRuleDefinition(): RuleDefinition
<<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Builder;
$builder->latest();
$builder->oldest();
$builder->latest('deleted_at');
$builder->latest();
$builder->oldest('deleted_at');
CODE_SAMPLE
,
),
Expand Down Expand Up @@ -71,44 +66,44 @@ 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 instanceof Node\Identifier
&& ($methodCall->name->name === 'orderBy' || $methodCall->name->name === 'orderByDesc')
&& $methodCall->args !== [];
&& count($methodCall->args) > 0;
}

private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
{
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof VariadicPlaceholder) {
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof Node\VariadicPlaceholder) {
return $methodCall;
}

$columnVar = $methodCall->args[0]->value ?? null;
if (! $columnVar instanceof Expr) {
if ($columnVar === null) {
return $methodCall;
}

$direction = $methodCall->args[1]->value->value ?? 'asc';
if ($this->isName($methodCall->name, 'orderByDesc')) {
$newMethod = 'oldest';
$newMethod = 'latest';
} else {
$newMethod = $direction === 'asc' ? 'latest' : 'oldest';
$newMethod = $direction === 'asc' ? 'oldest' : 'latest';
}
if ($columnVar instanceof String_ && $columnVar->value === 'created_at') {
$methodCall->name = new Identifier($newMethod);
if ($columnVar instanceof Node\Scalar\String_ && $columnVar->value === 'created_at') {
$methodCall->name = new Node\Identifier($newMethod);
$methodCall->args = [];

return $methodCall;
}

if ($columnVar instanceof String_) {
$methodCall->name = new Identifier($newMethod);
$methodCall->args = [new Arg(new String_($columnVar->value))];
if ($columnVar instanceof Node\Scalar\String_) {
$methodCall->name = new Node\Identifier($newMethod);
$methodCall->args = [new Node\Arg(new Node\Scalar\String_($columnVar->value))];

return $methodCall;
}

$methodCall->name = new Identifier($newMethod);
$methodCall->args = [new Arg($columnVar)];
$methodCall->name = new Node\Identifier($newMethod);
$methodCall->args = [new Node\Arg($columnVar)];

return $methodCall;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ use Illuminate\Database\Query\Builder;
$column = 'tested_at';

/** @var Builder $query */
$query->latest();
$query->oldest();
$query->latest('submitted_at');
$query->latest();
$query->oldest('submitted_at');
$query->latest($column);
$query->latest('submitted_at');
$query->oldest($column);

?>

0 comments on commit 3054e6e

Please sign in to comment.