|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Cake\Upgrade\Rector\Rector\MethodCall; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use PhpParser\Node\Scalar\String_; |
| 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 | + * Transforms $query->newExpr()->aggregate() to $query->func()->aggregate() |
| 18 | + * |
| 19 | + * newExpr() was used to create expression builders, but when followed by aggregate |
| 20 | + * or SQL function calls, it should use func() instead which is the function builder. |
| 21 | + * |
| 22 | + * Handles common FunctionsBuilder methods like: |
| 23 | + * - Aggregates: count(), sum(), avg(), min(), max(), rowNumber(), lag(), lead() |
| 24 | + * - Date functions: dateDiff(), datePart(), extract(), dateAdd(), now() |
| 25 | + * - Other functions: concat(), coalesce(), cast(), rand() |
| 26 | + */ |
| 27 | +final class NewExprToFuncRector extends AbstractRector |
| 28 | +{ |
| 29 | + /** |
| 30 | + * List of FunctionsBuilder methods that should trigger the transformation |
| 31 | + */ |
| 32 | + private const FUNC_BUILDER_METHODS = [ |
| 33 | + // Aggregate functions |
| 34 | + 'count', |
| 35 | + 'sum', |
| 36 | + 'avg', |
| 37 | + 'min', |
| 38 | + 'max', |
| 39 | + 'rowNumber', |
| 40 | + 'lag', |
| 41 | + 'lead', |
| 42 | + // Date/time functions |
| 43 | + 'dateDiff', |
| 44 | + 'datePart', |
| 45 | + 'extract', |
| 46 | + 'dateAdd', |
| 47 | + 'now', |
| 48 | + 'weekday', |
| 49 | + 'dayOfWeek', |
| 50 | + // Other SQL functions |
| 51 | + 'concat', |
| 52 | + 'coalesce', |
| 53 | + 'cast', |
| 54 | + 'rand', |
| 55 | + 'jsonValue', |
| 56 | + 'aggregate', |
| 57 | + ]; |
| 58 | + |
| 59 | + public function getRuleDefinition(): RuleDefinition |
| 60 | + { |
| 61 | + return new RuleDefinition( |
| 62 | + 'Change $query->newExpr()->funcMethod() to $query->func()->funcMethod() for FunctionsBuilder methods', |
| 63 | + [ |
| 64 | + new CodeSample( |
| 65 | + <<<'CODE_SAMPLE' |
| 66 | +$query->newExpr()->count(); |
| 67 | +$query->newExpr()->sum('total'); |
| 68 | +$query->newExpr()->avg('score'); |
| 69 | +CODE_SAMPLE |
| 70 | + , |
| 71 | + <<<'CODE_SAMPLE' |
| 72 | +$query->func()->count('*'); |
| 73 | +$query->func()->sum('total'); |
| 74 | +$query->func()->avg('score'); |
| 75 | +CODE_SAMPLE, |
| 76 | + ), |
| 77 | + ], |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function getNodeTypes(): array |
| 82 | + { |
| 83 | + return [MethodCall::class]; |
| 84 | + } |
| 85 | + |
| 86 | + public function refactor(Node $node): ?Node |
| 87 | + { |
| 88 | + if (!$node instanceof MethodCall) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + // Check if this is a FunctionsBuilder method call |
| 93 | + if (!$node->name instanceof Identifier) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + $methodName = $node->name->toString(); |
| 98 | + if (!in_array($methodName, self::FUNC_BUILDER_METHODS, true)) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + // Check if the var is also a MethodCall (chained call) |
| 103 | + if (!$node->var instanceof MethodCall) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + $innerMethodCall = $node->var; |
| 108 | + |
| 109 | + // Check if the inner call is ->newExpr() |
| 110 | + if (!$innerMethodCall->name instanceof Identifier || $innerMethodCall->name->toString() !== 'newExpr') { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + // Check if the caller is a Query object (Cake\Database\Query or similar) |
| 115 | + $callerType = $this->getType($innerMethodCall->var); |
| 116 | + if (!$callerType instanceof ObjectType) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + if ( |
| 121 | + !$callerType->isInstanceOf('Cake\Database\Query')->yes() && |
| 122 | + !$callerType->isInstanceOf('Cake\ORM\Query')->yes() |
| 123 | + ) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + // Change newExpr to func |
| 128 | + $innerMethodCall->name = new Identifier('func'); |
| 129 | + |
| 130 | + // Add '*' argument to count() if it doesn't have arguments |
| 131 | + if ($methodName === 'count' && empty($node->args)) { |
| 132 | + $node->args = [new Arg(new String_('*'))]; |
| 133 | + } |
| 134 | + |
| 135 | + return $node; |
| 136 | + } |
| 137 | +} |
0 commit comments