Skip to content

Commit 81f70eb

Browse files
fix: refactor only when DB::raw is inside of a *Raw method
https://laravel.com/docs/10.x/queries#raw-expressions
1 parent 35799bf commit 81f70eb

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@ vendor/bin/rector process --clear-cache
8282
### Before
8383

8484
```php
85-
DB::select(DB::raw('select 1'));
85+
$orders = DB::table('orders')
86+
->selectRaw(DB::raw('price * ? as price_with_tax'), [1.0825])
87+
->get();
8688
```
8789

8890
### After
8991

9092
```php
91-
DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));
93+
$orders = DB::table('orders')
94+
->selectRaw(DB::raw('price * ? as price_with_tax')->getValue(DB::getQueryGrammar()), [1.0825])
95+
->get();
9296
```
9397

9498
## License

src/LaravelDatabaseExpressionsRector.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public function getRuleDefinition(): RuleDefinition
2121
return new RuleDefinition(
2222
'Fix Laravel 10 database expressions', [
2323
new CodeSample(
24-
"DB::select(DB::raw('select 1'));",
25-
"DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));"
24+
"DB::table('orders')->selectRaw(DB::raw('price * ? as price_with_tax'), [1.0825])->get();",
25+
"DB::table('orders')->selectRaw('price * ? as price_with_tax', [1.0825])->get();",
2626
)
2727
]
2828
);
@@ -42,13 +42,16 @@ public function getNodeTypes(): array
4242
public function refactor(Node $node): ?Node
4343
{
4444
/** @var Node */
45-
$subNode = $node->args[0]->value ?? null;
45+
$childNode = $node->args[0]->value ?? null;
46+
47+
$className = $this->getName($node->name);
48+
$childClassName = isset($childNode->class) ? $this->getName($childNode->class) : '';
49+
$childMethodName = isset($childNode->name) ? $this->getName($childNode->name) : '';
4650

4751
if (
48-
! isset($subNode->class) ||
49-
$this->getName($node->name) !== 'select' ||
50-
strpos($this->getName($subNode->class), 'DB') === false ||
51-
$this->getName($subNode->name) !== 'raw'
52+
! str_ends_with($className, 'Raw') ||
53+
! str_ends_with($childClassName, 'DB') ||
54+
$childMethodName !== 'raw'
5255
) {
5356
return null;
5457
}
@@ -61,7 +64,7 @@ public function refactor(Node $node): ?Node
6164
);
6265

6366
$node->args[0]->value = new MethodCall(
64-
$subNode,
67+
$childNode,
6568
new Identifier('getValue'),
6669
$arguments
6770
);

tests/fixture/SkipRuleTestFixture.php.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,17 @@ function select5() {
1818
DB::select('select 5');
1919
$db->select('select 5');
2020
}
21+
22+
DB::select(DB::raw('select 6'));
23+
24+
DB::select(
25+
DB::raw('select 7')
26+
);
27+
28+
$db->select(DB::raw('select 8'));
29+
30+
$orders = DB::table('orders')
31+
->select('department', DB::raw('SUM(price) as total_sales'))
32+
->groupBy('department')
33+
->havingRaw('SUM(price) > ?', [2500])
34+
->get();

tests/fixture/TestFixture.php.inc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ declare(strict_types=1);
44

55
use Illuminate\Support\Facades\DB;
66

7-
DB::select(DB::raw('select 1'));
7+
$orders = DB::table('orders')
8+
->selectRaw(DB::raw('price * ? as price_with_tax'), [1.0825])
9+
->get();
810

9-
DB::select(
10-
DB::raw('select 2')
11-
);
12-
13-
$db->select(DB::raw('select 3'));
11+
$orders = DB::table('orders')
12+
->whereRaw(DB::raw('price > IF(state = "TX", ?, 100)'), [200])
13+
->get();
1414
-----
1515
<?php
1616

1717
declare(strict_types=1);
1818

1919
use Illuminate\Support\Facades\DB;
2020

21-
DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));
22-
23-
DB::select(
24-
DB::raw('select 2')->getValue(DB::getQueryGrammar())
25-
);
21+
$orders = DB::table('orders')
22+
->selectRaw(DB::raw('price * ? as price_with_tax')->getValue(DB::getQueryGrammar()), [1.0825])
23+
->get();
2624

27-
$db->select(DB::raw('select 3')->getValue(DB::getQueryGrammar()));
25+
$orders = DB::table('orders')
26+
->whereRaw(DB::raw('price > IF(state = "TX", ?, 100)')->getValue(DB::getQueryGrammar()), [200])
27+
->get();

0 commit comments

Comments
 (0)