generated from remarkablemark/rector-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Laravel database expressions rector
- Loading branch information
1 parent
9b8bdf8
commit 36d64b7
Showing
8 changed files
with
151 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ composer.phar | |
|
||
# PHPUnit | ||
.phpunit.result.cache | ||
.phpunit.cache/ | ||
|
||
# PHP CS Fixer | ||
.php-cs-fixer.cache | ||
|
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
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
This file was deleted.
Oops, something went wrong.
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remarkablemark\RectorLaravelDatabaseExpressions; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class LaravelDatabaseExpressionsRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Fix Laravel 10 database expressions', [ | ||
new CodeSample( | ||
"DB::select(DB::raw('select 1'));", | ||
"DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));" | ||
) | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
/** @var Node */ | ||
$subNode = $node->args[0]->value ?? null; | ||
|
||
if ( | ||
! isset($subNode->class) || | ||
$this->getName($node->name) !== 'select' || | ||
strpos($this->getName($subNode->class), 'DB') === false || | ||
$this->getName($subNode->name) !== 'raw' | ||
) { | ||
return null; | ||
} | ||
|
||
$arguments[] = new Arg( | ||
new StaticCall( | ||
new Name('DB'), | ||
'getQueryGrammar' | ||
) | ||
); | ||
|
||
$node->args[0]->value = new MethodCall( | ||
$subNode, | ||
new Identifier('getValue'), | ||
$arguments | ||
); | ||
|
||
return $node; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
<?php | ||
|
||
namespace Package\Tests\Rector\MyFirstRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function handleLogin(User $user, string $password) | ||
{ | ||
return $user->isCorrectPassword($password); | ||
} | ||
} | ||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
DB::select('select 1'); | ||
|
||
DB::select( | ||
'select 2' | ||
); | ||
|
||
?> | ||
$db->select('select 3'); | ||
|
||
DB::raw('select 4'); | ||
|
||
function select5() { | ||
DB::select('select 5'); | ||
$db->select('select 5'); | ||
} |
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
<?php | ||
|
||
namespace Package\Tests\Rector\MyFirstRector\Fixture; | ||
declare(strict_types=1); | ||
|
||
class SomeClass | ||
{ | ||
public function handlePasswordChange(User $user, string $password) | ||
{ | ||
$user->setPassword($password); | ||
} | ||
} | ||
use Illuminate\Support\Facades\DB; | ||
|
||
?> | ||
DB::select(DB::raw('select 1')); | ||
|
||
DB::select( | ||
DB::raw('select 2') | ||
); | ||
|
||
$db->select(DB::raw('select 3')); | ||
----- | ||
<?php | ||
|
||
namespace Package\Tests\Rector\MyFirstRector\Fixture; | ||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar())); | ||
|
||
class SomeClass | ||
{ | ||
public function handlePasswordChange(User $user, string $password) | ||
{ | ||
$user->changePassword($password); | ||
} | ||
} | ||
DB::select( | ||
DB::raw('select 2')->getValue(DB::getQueryGrammar()) | ||
); | ||
|
||
?> | ||
$db->select(DB::raw('select 3')->getValue(DB::getQueryGrammar())); |