-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the EloquentOrderByToLatestOrOldestRector rule (#142)
- Loading branch information
Showing
7 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\MethodCall\EloquentOrderByToLatestOrOldestRector\EloquentOrderByToLatestOrOldestRectorTest | ||
*/ | ||
class EloquentOrderByToLatestOrOldestRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Changes orderBy() to latest() or oldest()', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Database\Eloquent\Builder; | ||
$builder->orderBy('created_at'); | ||
$builder->orderBy('created_at', 'desc'); | ||
$builder->orderBy('deleted_at'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Database\Eloquent\Builder; | ||
$builder->latest(); | ||
$builder->oldest(); | ||
$builder->latest('deleted_at'); | ||
CODE_SAMPLE | ||
, | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
if ($this->isOrderByMethodCall($node)) { | ||
return $this->convertOrderByToLatest($node); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
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 Node\Identifier | ||
&& ($methodCall->name->name === 'orderBy' || $methodCall->name->name === 'orderByDesc') | ||
&& count($methodCall->args) > 0; | ||
} | ||
|
||
private function convertOrderByToLatest(MethodCall $methodCall): MethodCall | ||
{ | ||
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof Node\VariadicPlaceholder) { | ||
return $methodCall; | ||
} | ||
|
||
$columnVar = $methodCall->args[0]->value ?? null; | ||
if ($columnVar === null) { | ||
return $methodCall; | ||
} | ||
|
||
$direction = $methodCall->args[1]->value->value ?? 'asc'; | ||
if ($this->isName($methodCall->name, 'orderByDesc')) { | ||
$newMethod = 'oldest'; | ||
} else { | ||
$newMethod = $direction === 'asc' ? 'latest' : 'oldest'; | ||
} | ||
if ($columnVar instanceof Node\Scalar\String_ && $columnVar->value === 'created_at') { | ||
$methodCall->name = new Node\Identifier($newMethod); | ||
$methodCall->args = []; | ||
|
||
return $methodCall; | ||
} | ||
|
||
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 Node\Identifier($newMethod); | ||
$methodCall->args = [new Node\Arg($columnVar)]; | ||
|
||
return $methodCall; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...dCall/EloquentOrderByToLatestOrOldestRector/EloquentOrderByToLatestOrOldestRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\EloquentOrderByToLatestOrOldestRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class EloquentOrderByToLatestOrOldestRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
|
||
$column = 'tested_at'; | ||
|
||
/** @var Builder $query */ | ||
$query->orderBy('created_at'); | ||
$query->orderBy('created_at', 'desc'); | ||
$query->orderBy('submitted_at'); | ||
$query->orderByDesc('submitted_at'); | ||
$query->orderBy($column); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
|
||
$column = 'tested_at'; | ||
|
||
/** @var Builder $query */ | ||
$query->latest(); | ||
$query->oldest(); | ||
$query->latest('submitted_at'); | ||
$query->oldest('submitted_at'); | ||
$query->latest($column); | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...odCall/EloquentOrderByToLatestOrOldestRector/Fixture/skip_if_not_eloquent_builder.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
$query->orderBy('created_at'); | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...odCall/EloquentOrderByToLatestOrOldestRector/Fixture/skip_if_not_enough_arguments.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
|
||
/** @var Builder $query */ | ||
$query->orderBy(); | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Rector\MethodCall\EloquentOrderByToLatestOrOldestRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(EloquentOrderByToLatestOrOldestRector::class); | ||
}; |