Skip to content

Commit

Permalink
fix: refactor only when DB::raw is inside of a *Raw method
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Dec 23, 2023
1 parent 35799bf commit 81f70eb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/LaravelDatabaseExpressionsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();",
)
]
);
Expand All @@ -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;
}
Expand All @@ -61,7 +64,7 @@ public function refactor(Node $node): ?Node
);

$node->args[0]->value = new MethodCall(
$subNode,
$childNode,
new Identifier('getValue'),
$arguments
);
Expand Down
14 changes: 14 additions & 0 deletions tests/fixture/SkipRuleTestFixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
24 changes: 12 additions & 12 deletions tests/fixture/TestFixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ 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();
-----
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\DB;

DB::select(DB::raw('select 1')->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();

0 comments on commit 81f70eb

Please sign in to comment.