From 81f70eb027db44569f1131e7102dc27884e0c589 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 22 Dec 2023 23:11:43 -0500 Subject: [PATCH] fix: refactor only when `DB::raw` is inside of a `*Raw` method https://laravel.com/docs/10.x/queries#raw-expressions --- README.md | 8 ++++++-- src/LaravelDatabaseExpressionsRector.php | 19 ++++++++++-------- tests/fixture/SkipRuleTestFixture.php.inc | 14 +++++++++++++ tests/fixture/TestFixture.php.inc | 24 +++++++++++------------ 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a25d626..932abe4 100644 --- a/README.md +++ b/README.md @@ -82,13 +82,17 @@ vendor/bin/rector process --clear-cache ### Before ```php -DB::select(DB::raw('select 1')); +$orders = DB::table('orders') + ->selectRaw(DB::raw('price * ? as price_with_tax'), [1.0825]) + ->get(); ``` ### After ```php -DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar())); +$orders = DB::table('orders') + ->selectRaw(DB::raw('price * ? as price_with_tax')->getValue(DB::getQueryGrammar()), [1.0825]) + ->get(); ``` ## License diff --git a/src/LaravelDatabaseExpressionsRector.php b/src/LaravelDatabaseExpressionsRector.php index 36d57b7..6f7afef 100644 --- a/src/LaravelDatabaseExpressionsRector.php +++ b/src/LaravelDatabaseExpressionsRector.php @@ -21,8 +21,8 @@ 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()));" + "DB::table('orders')->selectRaw(DB::raw('price * ? as price_with_tax'), [1.0825])->get();", + "DB::table('orders')->selectRaw('price * ? as price_with_tax', [1.0825])->get();", ) ] ); @@ -42,13 +42,16 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { /** @var Node */ - $subNode = $node->args[0]->value ?? null; + $childNode = $node->args[0]->value ?? null; + + $className = $this->getName($node->name); + $childClassName = isset($childNode->class) ? $this->getName($childNode->class) : ''; + $childMethodName = isset($childNode->name) ? $this->getName($childNode->name) : ''; if ( - ! isset($subNode->class) || - $this->getName($node->name) !== 'select' || - strpos($this->getName($subNode->class), 'DB') === false || - $this->getName($subNode->name) !== 'raw' + ! str_ends_with($className, 'Raw') || + ! str_ends_with($childClassName, 'DB') || + $childMethodName !== 'raw' ) { return null; } @@ -61,7 +64,7 @@ public function refactor(Node $node): ?Node ); $node->args[0]->value = new MethodCall( - $subNode, + $childNode, new Identifier('getValue'), $arguments ); diff --git a/tests/fixture/SkipRuleTestFixture.php.inc b/tests/fixture/SkipRuleTestFixture.php.inc index 31871f5..09884d2 100644 --- a/tests/fixture/SkipRuleTestFixture.php.inc +++ b/tests/fixture/SkipRuleTestFixture.php.inc @@ -18,3 +18,17 @@ function select5() { DB::select('select 5'); $db->select('select 5'); } + +DB::select(DB::raw('select 6')); + +DB::select( + DB::raw('select 7') +); + +$db->select(DB::raw('select 8')); + +$orders = DB::table('orders') + ->select('department', DB::raw('SUM(price) as total_sales')) + ->groupBy('department') + ->havingRaw('SUM(price) > ?', [2500]) + ->get(); diff --git a/tests/fixture/TestFixture.php.inc b/tests/fixture/TestFixture.php.inc index 855ad3c..5b05254 100644 --- a/tests/fixture/TestFixture.php.inc +++ b/tests/fixture/TestFixture.php.inc @@ -4,13 +4,13 @@ declare(strict_types=1); use Illuminate\Support\Facades\DB; -DB::select(DB::raw('select 1')); +$orders = DB::table('orders') + ->selectRaw(DB::raw('price * ? as price_with_tax'), [1.0825]) + ->get(); -DB::select( - DB::raw('select 2') -); - -$db->select(DB::raw('select 3')); +$orders = DB::table('orders') + ->whereRaw(DB::raw('price > IF(state = "TX", ?, 100)'), [200]) + ->get(); ----- getValue(DB::getQueryGrammar())); - -DB::select( - DB::raw('select 2')->getValue(DB::getQueryGrammar()) -); +$orders = DB::table('orders') + ->selectRaw(DB::raw('price * ? as price_with_tax')->getValue(DB::getQueryGrammar()), [1.0825]) + ->get(); -$db->select(DB::raw('select 3')->getValue(DB::getQueryGrammar())); +$orders = DB::table('orders') + ->whereRaw(DB::raw('price > IF(state = "TX", ?, 100)')->getValue(DB::getQueryGrammar()), [200]) + ->get();