|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Cake\Upgrade\Rector\Cake6; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr\Array_; |
| 9 | +use PhpParser\Node\Expr\Closure; |
| 10 | +use PhpParser\Node\Expr\ConstFetch; |
| 11 | +use PhpParser\Node\Expr\MethodCall; |
| 12 | +use PhpParser\Node\Name; |
| 13 | +use PHPStan\Type\ObjectType; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | + |
| 18 | +/** |
| 19 | + * Reorders RouteBuilder scope/prefix/plugin/resources arguments |
| 20 | + * to have callback second and params/options third. |
| 21 | + * |
| 22 | + * @see \Cake\Upgrade\Test\TestCase\Rector\MethodCall\RouteBuilderToCallbackFirstRector\RouteBuilderToCallbackFirstRectorTest |
| 23 | + */ |
| 24 | +final class RouteBuilderToCallbackFirstRector extends AbstractRector |
| 25 | +{ |
| 26 | + private const METHODS = ['scope', 'prefix', 'plugin', 'resources']; |
| 27 | + |
| 28 | + public function getRuleDefinition(): RuleDefinition |
| 29 | + { |
| 30 | + return new RuleDefinition( |
| 31 | + 'Reorder RouteBuilder scope/prefix/plugin/resources arguments ' . |
| 32 | + 'to have callback second and params/options third', |
| 33 | + [ |
| 34 | + new CodeSample( |
| 35 | + <<<'CODE_SAMPLE' |
| 36 | +$routes->scope('/api', ['prefix' => 'Api'], function ($routes) { |
| 37 | + $routes->resources('Articles'); |
| 38 | +}); |
| 39 | +$routes->prefix('admin', [], function ($routes) { |
| 40 | + $routes->connect('/', ['controller' => 'Dashboard']); |
| 41 | +}); |
| 42 | +$routes->resources('Articles', ['only' => 'index']); |
| 43 | +$routes->resources('Posts', ['only' => 'index'], function ($routes) { |
| 44 | + $routes->resources('Comments'); |
| 45 | +}); |
| 46 | +CODE_SAMPLE |
| 47 | + , |
| 48 | + <<<'CODE_SAMPLE' |
| 49 | +$routes->scope('/api', function ($routes) { |
| 50 | + $routes->resources('Articles'); |
| 51 | +}, ['prefix' => 'Api']); |
| 52 | +$routes->prefix('admin', function ($routes) { |
| 53 | + $routes->connect('/', ['controller' => 'Dashboard']); |
| 54 | +}); |
| 55 | +$routes->resources('Articles', null, ['only' => 'index']); |
| 56 | +$routes->resources('Posts', function ($routes) { |
| 57 | + $routes->resources('Comments'); |
| 58 | +}, ['only' => 'index']); |
| 59 | +CODE_SAMPLE, |
| 60 | + ), |
| 61 | + ], |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return array<class-string<\PhpParser\Node>> |
| 67 | + */ |
| 68 | + public function getNodeTypes(): array |
| 69 | + { |
| 70 | + return [MethodCall::class]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param \PhpParser\Node\Expr\MethodCall $node |
| 75 | + */ |
| 76 | + public function refactor(Node $node): ?Node |
| 77 | + { |
| 78 | + // Check if the object is a RouteBuilder |
| 79 | + if (!$this->isObjectType($node->var, new ObjectType('Cake\Routing\RouteBuilder'))) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + // Check if this is one of our target methods |
| 84 | + if (!$this->isNames($node->name, self::METHODS)) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + $methodName = $this->getName($node->name); |
| 89 | + $argCount = count($node->args); |
| 90 | + |
| 91 | + // For resources method |
| 92 | + // Old signature: resources(string $name, array $options = [], ?callable $callback = null) |
| 93 | + // New signature: resources(string $name, ?Closure $callback = null, array $options = []) |
| 94 | + if ($methodName === 'resources') { |
| 95 | + // resources('Articles', ['only' => 'index']) -> resources('Articles', null, ['only' => 'index']) |
| 96 | + if ($argCount === 2) { |
| 97 | + $secondArg = $node->args[1]; |
| 98 | + // Check if second arg is an array (options) |
| 99 | + if ($secondArg->value instanceof Array_) { |
| 100 | + // Insert null as second argument, move array to third |
| 101 | + $node->args = [ |
| 102 | + $node->args[0], |
| 103 | + new Arg(new ConstFetch(new Name('null'))), |
| 104 | + $secondArg, |
| 105 | + ]; |
| 106 | + |
| 107 | + return $node; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // resources('Articles', ['only' => 'index'], fn) -> resources('Articles', fn, ['only' => 'index']) |
| 112 | + if ($argCount === 3) { |
| 113 | + $secondArg = $node->args[1]; |
| 114 | + $thirdArg = $node->args[2]; |
| 115 | + |
| 116 | + // Check if third arg is closure and second is array |
| 117 | + if ($thirdArg->value instanceof Closure && $secondArg->value instanceof Array_) { |
| 118 | + // Check if second argument is an empty array - if so, just remove it |
| 119 | + $isEmptyArray = count($secondArg->value->items) === 0; |
| 120 | + |
| 121 | + if ($isEmptyArray) { |
| 122 | + $node->args = [ |
| 123 | + $node->args[0], |
| 124 | + $thirdArg, |
| 125 | + ]; |
| 126 | + } else { |
| 127 | + // Swap: callback becomes second, options becomes third |
| 128 | + $node->args[1] = $thirdArg; |
| 129 | + $node->args[2] = $secondArg; |
| 130 | + } |
| 131 | + |
| 132 | + return $node; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + // For scope/prefix/plugin methods |
| 140 | + // Old signature: method(string $path, array|callable $params, ?callable $callback = null) |
| 141 | + // New signature: method(string $path, Closure $callback, array $params = []) |
| 142 | + |
| 143 | + // Only process if there are exactly 3 arguments |
| 144 | + if ($argCount !== 3) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + $secondArg = $node->args[1]; |
| 149 | + $thirdArg = $node->args[2]; |
| 150 | + |
| 151 | + // Check if the third argument is a closure/callable |
| 152 | + // and second argument is array (the old signature) |
| 153 | + if ($thirdArg->value instanceof Closure) { |
| 154 | + // Check if second argument is an empty array - if so, just remove it |
| 155 | + $isEmptyArray = $secondArg->value instanceof Array_ && count($secondArg->value->items) === 0; |
| 156 | + |
| 157 | + if ($isEmptyArray) { |
| 158 | + // Just use callback as second arg, drop the empty array |
| 159 | + $node->args = [ |
| 160 | + $node->args[0], |
| 161 | + $thirdArg, |
| 162 | + ]; |
| 163 | + } else { |
| 164 | + // Swap: callback becomes second, params becomes third |
| 165 | + $node->args[1] = $thirdArg; |
| 166 | + $node->args[2] = $secondArg; |
| 167 | + } |
| 168 | + |
| 169 | + return $node; |
| 170 | + } |
| 171 | + |
| 172 | + return null; |
| 173 | + } |
| 174 | +} |
0 commit comments