-
Notifications
You must be signed in to change notification settings - Fork 59
add custom rector to cleanup command usages #348
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4dd1793
add custom rector to cleanup command usages
LordSimal 7cbf0ea
test with disabled custom rector
LordSimal 671cda4
add unit tests
LordSimal 701cbc5
adjust CI
LordSimal d68f12b
adjust composer.json
LordSimal 3f1ce70
Revert "adjust composer.json"
LordSimal 293b3e2
Merge branch '6.x' into 6.x-command-args-io
LordSimal 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
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
152 changes: 152 additions & 0 deletions
152
src/Rector/Rector/MethodCall/ReplaceCommandArgsIoWithPropertiesRector.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,152 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Cake\Upgrade\Rector\Rector\MethodCall; | ||
|
|
||
| use Cake\Command\Command; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\PropertyFetch; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Param; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use Rector\PhpParser\Node\BetterNodeFinder; | ||
| use Rector\PHPStan\ScopeFetcher; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| final class ReplaceCommandArgsIoWithPropertiesRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| protected BetterNodeFinder $betterNodeFinder, | ||
| protected ReflectionProvider $reflectionProvider, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Replace `$args` and `$io` parameters in Command classes with `$this->args` and `$this->io`', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class TestCommand extends Command | ||
| { | ||
| public function execute(Arguments $args, ConsoleIo $io) | ||
| { | ||
| $io->out('Hello'); | ||
| $this->someMethod($args, $io); | ||
| } | ||
|
|
||
| protected function someMethod(Arguments $args, ConsoleIo $io): void | ||
| { | ||
| $someArg = $args->getArgument('some'); | ||
| $io->warning('Warn'); | ||
| } | ||
| } | ||
| CODE_SAMPLE, | ||
| <<<'CODE_SAMPLE' | ||
| class TestCommand extends Command | ||
| { | ||
| public function execute() | ||
| { | ||
| $this->io->out('Hello'); | ||
| $this->someMethod(); | ||
| } | ||
|
|
||
| protected function someMethod(): void | ||
| { | ||
| $someArg = $this->args->getArgument('some'); | ||
| $this->io->warning('Warn'); | ||
| } | ||
| } | ||
| CODE_SAMPLE, | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| public function getNodeTypes(): array | ||
| { | ||
| return [ClassMethod::class]; | ||
| } | ||
|
|
||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if (! $node instanceof ClassMethod) { | ||
| return null; | ||
| } | ||
|
|
||
| // Make sure we are in a class | ||
| $scope = ScopeFetcher::fetch($node); | ||
| if (!$scope->isInClass()) { | ||
| return null; | ||
| } | ||
| $class = $scope->getClassReflection(); | ||
|
|
||
| // Skip if class doesn't extend Command (you can expand to check parent name) | ||
| $baseCommandClass = $this->reflectionProvider->getClass(Command::class); | ||
| if ($class->getName() === Command::class || $class->isSubclassOfClass($baseCommandClass) === false) { | ||
| return null; | ||
| } | ||
|
|
||
| // Find if params are $args and/or $io | ||
| $argsParam = $this->findParam($node, 'args'); | ||
| $ioParam = $this->findParam($node, 'io'); | ||
|
|
||
| if (! $argsParam && ! $ioParam) { | ||
| return null; | ||
| } | ||
|
|
||
| // Replace all `$args` and `$io` usages inside the method body | ||
| $this->traverseNodesWithCallable($node->stmts ?? [], function (Node $innerNode) use ($argsParam, $ioParam) { | ||
| // Replace `$args` and `$io` variables | ||
| if ($innerNode instanceof Variable) { | ||
| if ($argsParam && $innerNode->name === 'args') { | ||
| return new PropertyFetch(new Variable('this'), 'args'); | ||
| } | ||
|
|
||
| if ($ioParam && $innerNode->name === 'io') { | ||
| return new PropertyFetch(new Variable('this'), 'io'); | ||
| } | ||
| } | ||
|
|
||
| // Remove `$args` / `$io` from method calls on `$this` | ||
| if ( | ||
| $innerNode instanceof MethodCall | ||
| && $innerNode->var instanceof Variable | ||
| && $innerNode->var->name === 'this' | ||
| ) { | ||
| $innerNode->args = array_values(array_filter( | ||
| $innerNode->args, | ||
| fn(Node\Arg $arg) => !($arg->value instanceof Variable && | ||
| in_array($arg->value->name, ['args', 'io'], true)), | ||
| )); | ||
|
|
||
| return $innerNode; | ||
| } | ||
|
|
||
| return null; | ||
| }); | ||
|
|
||
| // Remove the parameters themselves | ||
| $node->params = array_filter($node->params, function (Param $param) { | ||
| return !in_array($this->getName($param), ['args', 'io'], true); | ||
| }); | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function findParam(ClassMethod $method, string $name): ?Param | ||
| { | ||
| foreach ($method->params as $param) { | ||
| if ($this->getName($param) === $name) { | ||
| return $param; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
...stCase/Rector/MethodCall/ReplaceCommandArgsIoWithPorpertiesRector/Fixture/fixture.php.inc
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,49 @@ | ||
| <?php | ||
|
|
||
| namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\AddMethodCallArgsRectorTest\Fixture; | ||
|
|
||
| use Cake\Command\Command; | ||
| use Cake\Console\Arguments; | ||
| use Cake\Console\ConsoleIo; | ||
|
|
||
| class TestCommand extends Command | ||
| { | ||
| public function execute(Arguments $args, ConsoleIo $io) | ||
| { | ||
| $io->out('Hello World'); | ||
| $this->someMethod($args, $io); | ||
| return static::CODE_SUCCESS; | ||
| } | ||
|
|
||
| protected function someMethod(Arguments $args, ConsoleIo $io): void | ||
| { | ||
| $someArg = $args->getArgument('some'); | ||
| $io->warning('Warning'); | ||
| } | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\AddMethodCallArgsRectorTest\Fixture; | ||
|
|
||
| use Cake\Command\Command; | ||
| use Cake\Console\Arguments; | ||
| use Cake\Console\ConsoleIo; | ||
|
|
||
| class TestCommand extends Command | ||
| { | ||
| public function execute() | ||
| { | ||
| $this->io->out('Hello World'); | ||
| $this->someMethod(); | ||
| return static::CODE_SUCCESS; | ||
| } | ||
|
|
||
| protected function someMethod(): void | ||
| { | ||
| $someArg = $this->args->getArgument('some'); | ||
| $this->io->warning('Warning'); | ||
| } | ||
| } | ||
| ?> |
Oops, something went wrong.
Oops, something went wrong.
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.
The operating spacing here doesn't seem to be consistent with the cakephp cs rules. Perhaps we aren't running those phpcs rules on this repo?
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.
according to the phpcs.xml file everything inside the src folder should be checked. don't know why it doesn't fix this kind of cs violation.