|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Cake\Upgrade\Rector\Rector\MethodCall; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PHPStan\Type\ObjectType; |
| 10 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 14 | + |
| 15 | +/** |
| 16 | + * Transforms $request->getParam('?') to $request->getQueryParams() |
| 17 | + * |
| 18 | + * In CakePHP 6.0, the getParam('?') shortcut for accessing query parameters |
| 19 | + * is removed. Instead, use getQueryParams() directly. |
| 20 | + * |
| 21 | + * @see https://book.cakephp.org/6/en/appendices/6-0-migration-guide.html |
| 22 | + */ |
| 23 | +final class QueryParamAccessRector extends AbstractRector |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private ValueResolver $valueResolver, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + public function getRuleDefinition(): RuleDefinition |
| 31 | + { |
| 32 | + return new RuleDefinition( |
| 33 | + 'Change $request->getParam(\'?\') to $request->getQueryParams()', |
| 34 | + [ |
| 35 | + new CodeSample( |
| 36 | + <<<'CODE_SAMPLE' |
| 37 | +$queryParams = $request->getParam('?'); |
| 38 | +CODE_SAMPLE |
| 39 | + , |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +$queryParams = $request->getQueryParams(); |
| 42 | +CODE_SAMPLE, |
| 43 | + ), |
| 44 | + ], |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function getNodeTypes(): array |
| 49 | + { |
| 50 | + return [MethodCall::class]; |
| 51 | + } |
| 52 | + |
| 53 | + public function refactor(Node $node): ?Node |
| 54 | + { |
| 55 | + if (!$node instanceof MethodCall) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + // Must be ->getParam() call |
| 60 | + if (!$node->name instanceof Identifier || $node->name->toString() !== 'getParam') { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + // Must have at least one argument |
| 65 | + if (count($node->args) < 1) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + // First argument must be the string '?' |
| 70 | + $firstArg = $node->args[0]->value; |
| 71 | + if (!$this->valueResolver->isValue($firstArg, '?')) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + // Check if this is called on a Request object |
| 76 | + $callerType = $this->getType($node->var); |
| 77 | + if (!$callerType instanceof ObjectType) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + if ( |
| 82 | + !$callerType->isInstanceOf('Cake\Http\ServerRequest')->yes() && |
| 83 | + !$callerType->isInstanceOf('Psr\Http\Message\ServerRequestInterface')->yes() |
| 84 | + ) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + // Transform to getQueryParams() with no arguments |
| 89 | + $node->name = new Identifier('getQueryParams'); |
| 90 | + $node->args = []; |
| 91 | + |
| 92 | + return $node; |
| 93 | + } |
| 94 | +} |
0 commit comments