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