Skip to content

Commit

Permalink
[ORM-300] add missing set, run fixer and add CastDoctrineExprToString…
Browse files Browse the repository at this point in the history
…Rector
  • Loading branch information
JohJohan committed Mar 6, 2025
1 parent d9be91e commit 2c4154c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 4 deletions.
5 changes: 2 additions & 3 deletions config/sets/doctrine-orm-300.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector;
use Rector\Doctrine\Orm30\Rector\MethodCall\SetParametersArrayToCollectionRector;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
SetParametersArrayToCollectionRector::class,
]);
$rectorConfig->rules([SetParametersArrayToCollectionRector::class, CastDoctrineExprToStringRector::class]);

$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Doctrine\ORM\ORMException' => 'Doctrine\ORM\Exception\ORMException',
Expand Down
98 changes: 98 additions & 0 deletions rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Orm30\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/doctrine/orm/commit/4d73e3ce7801d3bf3254257332e903d8ecea4096
*/
final class CastDoctrineExprToStringRector extends AbstractRector
{
/**
* @var array<string>
*/
private array $targetMethods = [
'like', 'notLike', 'eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'between',
'in', 'notIn', 'isMemberOf', 'isInstanceOf',
];

/**
* @var array<string>
*/
private array $exprFuncMethods = [
'lower', 'upper', 'length', 'trim', 'avg', 'max', 'min', 'count',
'countDistinct', 'exists', 'all', 'some', 'any', 'not', 'abs', 'sqrt',
];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Casts Doctrine Expr\x to string where necessary.',
[
new CodeSample(
<<<'CODE_SAMPLE'
$statements->add(
$builder->expr()->like(
$builder->expr()->lower($column),
$builder->expr()->lower($builder->expr()->literal('%'.$like.'%'))
)
);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$statements->add(
$builder->expr()->like(
(string) $builder->expr()->lower($column),
(string) $builder->expr()->lower($builder->expr()->literal('%'.$like.'%'))
)
);
CODE_SAMPLE
)]
);
}

public function getNodeTypes(): array
{
return [MethodCall::class];
}

public function refactor(Node $node): ?Node
{
if (! $node instanceof MethodCall) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType('Doctrine\ORM\Query\Expr'))) {
return null;
}

if (! in_array($this->getName($node->name), $this->targetMethods, true)) {
return null;
}

// Iterate through method arguments and cast `Expr\Func` calls to string
foreach ($node->args as $arg) {
if (! $arg instanceof Arg) {
continue;
}
if ($arg->value instanceof MethodCall
&& $this->isObjectType($arg->value->var, new ObjectType('Doctrine\ORM\Query\Expr'))
&& in_array($this->getName($arg->value->name), $this->exprFuncMethods, true)
) {
$arg->value = new String_($arg->value);
}
}

return $node;
}
}
2 changes: 1 addition & 1 deletion src/Set/DoctrineSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class DoctrineSetList
/**
* @var string
*/
public const DOCTRINE_ORM_219 = __DIR__.'/../../config/sets/doctrine-orm-219.php';
public const DOCTRINE_ORM_219 = __DIR__ . '/../../config/sets/doctrine-orm-219.php';

/**
* @var string
Expand Down
6 changes: 6 additions & 0 deletions src/Set/SetProvider/DoctrineSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public function provide(): array
'2.14',
__DIR__ . '/../../../config/sets/doctrine-orm-214.php',
),
new ComposerTriggeredSet(
SetGroup::DOCTRINE,
'doctrine/orm',
'3.0',
__DIR__ . '/../../../config/sets/doctrine-orm-300.php',
),

new Set(SetGroup::ATTRIBUTES, 'Doctrine ORM', __DIR__ . '/../../../config/sets/attributes/doctrine.php'),
new Set(SetGroup::ATTRIBUTES, 'Gedmo', __DIR__ . '/../../../config/sets/attributes/gedmo.php'),
Expand Down

0 comments on commit 2c4154c

Please sign in to comment.