|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\Doctrine\Orm30\Rector\MethodCall; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr\Cast\String_; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PHPStan\Type\ObjectType; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 14 | +/** |
| 15 | + * @see https://github.com/doctrine/orm/commit/4d73e3ce7801d3bf3254257332e903d8ecea4096 |
| 16 | + */ |
| 17 | +final class CastDoctrineExprToStringRector extends AbstractRector |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var array<string> |
| 21 | + */ |
| 22 | + private array $targetMethods = ['like', 'notLike', 'eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'between', 'in', 'notIn', 'isMemberOf', 'isInstanceOf']; |
| 23 | + /** |
| 24 | + * @var array<string> |
| 25 | + */ |
| 26 | + private array $exprFuncMethods = ['lower', 'upper', 'length', 'trim', 'avg', 'max', 'min', 'count', 'countDistinct', 'exists', 'all', 'some', 'any', 'not', 'abs', 'sqrt']; |
| 27 | + public function getRuleDefinition() : RuleDefinition |
| 28 | + { |
| 29 | + return new RuleDefinition('Casts Doctrine Expr\\x to string where necessary.', [new CodeSample(<<<'CODE_SAMPLE' |
| 30 | +$statements->add( |
| 31 | + $builder->expr()->like( |
| 32 | + $builder->expr()->lower($column), |
| 33 | + $builder->expr()->lower($builder->expr()->literal('%'.$like.'%')) |
| 34 | + ) |
| 35 | +); |
| 36 | +CODE_SAMPLE |
| 37 | +, <<<'CODE_SAMPLE' |
| 38 | +$statements->add( |
| 39 | + $builder->expr()->like( |
| 40 | + (string) $builder->expr()->lower($column), |
| 41 | + (string) $builder->expr()->lower($builder->expr()->literal('%'.$like.'%')) |
| 42 | + ) |
| 43 | +); |
| 44 | +CODE_SAMPLE |
| 45 | +)]); |
| 46 | + } |
| 47 | + public function getNodeTypes() : array |
| 48 | + { |
| 49 | + return [MethodCall::class]; |
| 50 | + } |
| 51 | + public function refactor(Node $node) : ?Node |
| 52 | + { |
| 53 | + if (!$node instanceof MethodCall) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + if (!$this->isObjectType($node->var, new ObjectType('Doctrine\\ORM\\Query\\Expr'))) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + if (!\in_array($this->getName($node->name), $this->targetMethods, \true)) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + // Iterate through method arguments and cast `Expr\Func` calls to string |
| 63 | + $hasChanged = \false; |
| 64 | + foreach ($node->args as $arg) { |
| 65 | + if (!$arg instanceof Arg) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + 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)) { |
| 69 | + $arg->value = new String_($arg->value); |
| 70 | + $hasChanged = \true; |
| 71 | + } |
| 72 | + } |
| 73 | + return $hasChanged ? $node : null; |
| 74 | + } |
| 75 | +} |
0 commit comments