-
Notifications
You must be signed in to change notification settings - Fork 61
add custom rector rule to cleanup RouteBuilder #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LordSimal
wants to merge
1
commit into
5.x
Choose a base branch
from
5.x-routebuilder-named-args
base: 5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/Rector/Rector/MethodCall/RouteBuilderCleanupRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Cake\Upgrade\Rector\Rector\MethodCall; | ||
|
|
||
| use Cake\Routing\RouteBuilder; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\ArrowFunction; | ||
| use PhpParser\Node\Expr\Closure; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Type\ObjectType; | ||
| use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| final class RouteBuilderCleanupRector extends AbstractRector implements ConfigurableRectorInterface | ||
| { | ||
| /** | ||
| * @var array<string, string[]> | ||
| * e.g. ['scope' => ['path', 'options', 'callback']] | ||
| */ | ||
| private array $methods = []; | ||
|
|
||
| public function configure(array $configuration): void | ||
| { | ||
| $this->methods = $configuration['methods'] ?? []; | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Normalize RouteBuilder calls to always use named arguments based on configuration', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| $routes->scope('/api', function (RouteBuilder $routes): void {}); | ||
| CODE_SAMPLE, | ||
| <<<'CODE_SAMPLE' | ||
| $routes->scope(path: '/api', options: [], callback: function (RouteBuilder $routes): void {}); | ||
| CODE_SAMPLE, | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| public function getNodeTypes(): array | ||
| { | ||
| return [MethodCall::class]; | ||
| } | ||
|
|
||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if (! $node instanceof MethodCall) { | ||
| return null; | ||
| } | ||
|
|
||
| $methodName = $this->getName($node->name); | ||
| if (! isset($this->methods[$methodName])) { | ||
| return null; | ||
| } | ||
|
|
||
| // Must be called on a Cake\Routing\RouteBuilder | ||
| $callerType = $this->getType($node->var); | ||
| if (! (new ObjectType(RouteBuilder::class))->isSuperTypeOf($callerType)->yes()) { | ||
| return null; | ||
| } | ||
|
|
||
| $argNames = $this->methods[$methodName]; | ||
| $args = $node->args; | ||
|
|
||
| $pathValue = $args[0]->value ?? null; | ||
| $optionsValue = null; | ||
| $callbackValue = null; | ||
|
|
||
| // Handle case where 2nd param is callable or array | ||
| if (isset($args[1])) { | ||
| if ($this->isCallableNode($args[1]->value)) { | ||
| // Case: scope('/api', fn() => null) | ||
| $callbackValue = $args[1]->value; | ||
| } else { | ||
| // Case: scope('/api', [], fn() => null) | ||
| $optionsValue = $args[1]->value; | ||
| $callbackValue = $args[2]->value ?? null; | ||
| } | ||
| } | ||
|
|
||
| if ($callbackValue === null) { | ||
| // no callable = no change | ||
| return null; | ||
| } | ||
|
|
||
| $newArgs = []; | ||
|
|
||
| // always add first argument (path/name) without a named argument | ||
| if (isset($argNames[0]) && $pathValue !== null) { | ||
| $newArgs[] = new Arg( | ||
| $pathValue, | ||
| false, | ||
| false, | ||
| [], | ||
| ); | ||
| } | ||
|
|
||
| // only add options if it existed in original call | ||
| if (isset($argNames[1]) && $optionsValue !== null) { | ||
| $newArgs[] = new Arg( | ||
| $optionsValue, | ||
| false, | ||
| false, | ||
| [], | ||
| new Identifier($argNames[1]), | ||
| ); | ||
| } elseif ($methodName === 'scope') { | ||
| // scope() must always have options | ||
| $newArgs[] = new Arg( | ||
| $optionsValue ?? $this->nodeFactory->createArray([]), | ||
| false, | ||
| false, | ||
| [], | ||
| new Identifier($argNames[1]), | ||
| ); | ||
| } | ||
|
|
||
| // always add callback if present | ||
| if (isset($argNames[2])) { | ||
| $newArgs[] = new Arg( | ||
| $callbackValue, | ||
| false, | ||
| false, | ||
| [], | ||
| new Identifier($argNames[2]), | ||
| ); | ||
| } | ||
|
|
||
| $node->args = $newArgs; | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function isCallableNode(Node $node): bool | ||
| { | ||
| return $node instanceof Closure | ||
| || $node instanceof ArrowFunction | ||
| || $node instanceof FuncCall; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the reaming of $params to $options, we use the term "route params".
We need to first update the method and make $params argument default to empty array and make $callable not nullable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave those changes to the the core to you @ADmad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems only resources() should be $options.