remarkablemark/rector-laravel-database-expressions

2.0.0 2024-02-26 07:31 UTC

This package is auto-updated.

Last update: 2024-09-09 02:34:13 UTC


README

packagist test

使用Rector重构 Laravel 10 数据库表达式。

Laravel 10

Laravel 10.x 重新编写了数据库 "表达式"(通常通过 DB::raw 生成),以提供未来的附加功能。值得注意的是,语法的原始字符串值现在必须通过表达式的 getValue(Grammar $grammar) 方法检索。不再支持使用 (string) 将表达式转换为字符串。

如果您的应用程序使用 (string) 手动将数据库表达式转换为字符串或直接在表达式中调用 __toString 方法,您应更新代码以调用 getValue 方法。

use Illuminate\Support\Facades\DB;

$expression = DB::raw('select 1');

$string = $expression->getValue(DB::connection()->getQueryGrammar());

需求

PHP >=8.0

安装

使用 Composer 安装

composer require --dev rector/rector remarkablemark/rector-laravel-database-expressions

用法

rector.php 中注册规则

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Remarkablemark\RectorLaravelDatabaseExpressions\LaravelDatabaseExpressionsRector;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/app',
        __DIR__ . '/bootstrap',
        __DIR__ . '/config',
        __DIR__ . '/database',
        __DIR__ . '/public',
        __DIR__ . '/resources',
        __DIR__ . '/routes',
        __DIR__ . '/storage',
        __DIR__ . '/tests',
    ]);
    $rectorConfig->rule(LaravelDatabaseExpressionsRector::class);
};

查看差异

vendor/bin/rector process --dry-run

应用规则

vendor/bin/rector process

清除缓存并应用规则

vendor/bin/rector process --clear-cache

规则

之前

DB::select(DB::raw('select 1'));

之后

DB::select('select 1');

许可

MIT