Skip to content

Commit

Permalink
feat: replace DB::raw() with first argument string
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Dec 23, 2023
1 parent 51274ad commit e1972dc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ DB::select(DB::raw('select 1'));
### After

```php
DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));
DB::select('select 1');
```

## License
Expand Down
15 changes: 2 additions & 13 deletions src/LaravelDatabaseExpressionsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
"DB::select(DB::raw('select 1'));",
"DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));"
"DB::select('select 1');"
),
]
);
Expand Down Expand Up @@ -60,18 +60,7 @@ public function refactor(Node $node): ?Node
return null;
}

$arguments[] = new Arg(
new StaticCall(
new Name('DB'),
'getQueryGrammar'
)
);

$node->args[0]->value = new MethodCall(
$childNode,
new Identifier('getValue'),
$arguments
);
$node->args[0]->value = $childNode->args[0]->value;

return $node;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/fixture/TestFixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ declare(strict_types=1);

use Illuminate\Support\Facades\DB;

DB::select(DB::raw('select 1')->getValue(DB::getQueryGrammar()));
DB::select('select 1');

DB::select(
DB::raw('select 2')->getValue(DB::getQueryGrammar())
'select 2'
);

$orders = DB::table('orders')
->selectRaw(DB::raw('price * ? as price_with_tax')->getValue(DB::getQueryGrammar()), [1.0825])
->selectRaw('price * ? as price_with_tax', [1.0825])
->get();

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

0 comments on commit e1972dc

Please sign in to comment.