Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/remarkablemark/rector-laravel-database-expressions
🐘 Rector Laravel database expressions
https://github.com/remarkablemark/rector-laravel-database-expressions
codemod composer database expressions laravel php rector refactoring
Last synced: 11 days ago
JSON representation
🐘 Rector Laravel database expressions
- Host: GitHub
- URL: https://github.com/remarkablemark/rector-laravel-database-expressions
- Owner: remarkablemark
- License: mit
- Created: 2023-12-20T17:10:26.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-04-25T02:55:30.000Z (7 months ago)
- Last Synced: 2024-05-01T21:25:43.902Z (6 months ago)
- Topics: codemod, composer, database, expressions, laravel, php, rector, refactoring
- Language: PHP
- Homepage: https://packagist.org/packages/remarkablemark/rector-laravel-database-expressions
- Size: 78.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# rector-laravel-database-expressions
[![packagist](https://img.shields.io/packagist/v/remarkablemark/rector-laravel-database-expressions)](https://packagist.org/packages/remarkablemark/rector-laravel-database-expressions)
[![test](https://github.com/remarkablemark/rector-laravel-database-expressions/actions/workflows/test.yml/badge.svg)](https://github.com/remarkablemark/rector-laravel-database-expressions/actions/workflows/test.yml)[Rector](https://github.com/rectorphp/rector) to refactor Laravel 10 database expressions.
From [Laravel 10](https://laravel.com/docs/10.x/upgrade#database-expressions):
> Database "expressions" (typically generated via `DB::raw`) have been rewritten in Laravel 10.x to offer additional functionality in the future. Notably, the grammar's raw string value must now be retrieved via the expression's `getValue(Grammar $grammar)` method. Casting an expression to a string using `(string)` is no longer supported.
>
> If your application is manually casting database expressions to strings using `(string)` or invoking the `__toString` method on the expression directly, you should update your code to invoke the `getValue` method instead:
>
> ```php
> use Illuminate\Support\Facades\DB;
>
> $expression = DB::raw('select 1');
>
> $string = $expression->getValue(DB::connection()->getQueryGrammar());
> ```## Requirements
PHP >=8.0
## Install
Install with [Composer](http://getcomposer.org/):
```sh
composer require --dev rector/rector remarkablemark/rector-laravel-database-expressions
```## Usage
Register rule in `rector.php`:
```php
paths([
__DIR__ . '/app',
__DIR__ . '/bootstrap',
__DIR__ . '/config',
__DIR__ . '/database',
__DIR__ . '/public',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/storage',
__DIR__ . '/tests',
]);
$rectorConfig->rule(LaravelDatabaseExpressionsRector::class);
};
```See the diff:
```php
vendor/bin/rector process --dry-run
```Apply the rule:
```php
vendor/bin/rector process
```Clear the cache and apply the rule:
```php
vendor/bin/rector process --clear-cache
```## Rule
### Before
```php
DB::select(DB::raw('select 1'));
```### After
```php
DB::select('select 1');
```## License
[MIT](LICENSE)