From 30f75282069f50dc4365331d36ae5da3618e229f Mon Sep 17 00:00:00 2001 From: geni_jaho Date: Tue, 14 Jan 2025 21:00:57 +0100 Subject: [PATCH] Do not convert to whereLike methods with Expressions as values --- .../MethodCall/WhereToWhereLikeRector.php | 7 +++++++ .../Contracts/Database/Query/Expression.php | 19 +++++++++++++++++++ stubs/Illuminate/Database/Grammar.php | 9 +++++++++ stubs/Illuminate/Support/Facades/DB.php | 16 ++++++++++++++++ .../Default/skip_with_expressions.php.inc | 15 +++++++++++++++ .../Postgres/SkipWithExpressions.php.inc | 15 +++++++++++++++ 6 files changed, 81 insertions(+) create mode 100644 stubs/Illuminate/Contracts/Database/Query/Expression.php create mode 100644 stubs/Illuminate/Database/Grammar.php create mode 100644 stubs/Illuminate/Support/Facades/DB.php create mode 100644 tests/Rector/MethodCall/WhereToWhereLikeRector/Fixture/Default/skip_with_expressions.php.inc create mode 100644 tests/Rector/MethodCall/WhereToWhereLikeRector/Fixture/Postgres/SkipWithExpressions.php.inc diff --git a/src/Rector/MethodCall/WhereToWhereLikeRector.php b/src/Rector/MethodCall/WhereToWhereLikeRector.php index 1ac812ff..6dc4f33f 100644 --- a/src/Rector/MethodCall/WhereToWhereLikeRector.php +++ b/src/Rector/MethodCall/WhereToWhereLikeRector.php @@ -106,6 +106,13 @@ public function refactor(Node $node): ?Node return null; } + // Expressions are not supported with the `like` operator + if ($node->args[2] instanceof Arg && + $this->isObjectType($node->args[2]->value, new ObjectType('Illuminate\Contracts\Database\Query\Expression')) + ) { + return null; + } + $likeParameter = $this->getLikeParameterUsedInQuery($node); if (! in_array($likeParameter, ['like', 'like binary', 'ilike', 'not like', 'not like binary', 'not ilike'], true)) { diff --git a/stubs/Illuminate/Contracts/Database/Query/Expression.php b/stubs/Illuminate/Contracts/Database/Query/Expression.php new file mode 100644 index 00000000..a5623383 --- /dev/null +++ b/stubs/Illuminate/Contracts/Database/Query/Expression.php @@ -0,0 +1,19 @@ +where('name', 'like', DB::raw('Rector')); + } +} +?> diff --git a/tests/Rector/MethodCall/WhereToWhereLikeRector/Fixture/Postgres/SkipWithExpressions.php.inc b/tests/Rector/MethodCall/WhereToWhereLikeRector/Fixture/Postgres/SkipWithExpressions.php.inc new file mode 100644 index 00000000..593e3cb4 --- /dev/null +++ b/tests/Rector/MethodCall/WhereToWhereLikeRector/Fixture/Postgres/SkipWithExpressions.php.inc @@ -0,0 +1,15 @@ +where('name', 'like', DB::raw('Rector')); + } +} +?>