-
Notifications
You must be signed in to change notification settings - Fork 59
Add rector rule for EventManager::on() signature change in CakePHP 6.0 #356
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
9 commits
Select commit
Hold shift + click to select a range
909df22
Initial plan
Copilot 95739cb
Add EventManagerOnRector for EventManager::on() signature change
Copilot 965ccd0
Refine EventManagerOnRector and update test fixture
Copilot 165b94d
Revert accidental composer.json changes
Copilot 824f62b
Change type check to EventManagerInterface instead of EventManager
Copilot 876b895
Fix coding style: remove trailing whitespace
Copilot 1060134
Fix coding style: remove blank line after function opening brace
Copilot 42409da
Fix docblock format: add blank line before last @see tag
Copilot f434bc7
Fix CS errors
ADmad 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Cake\Upgrade\Rector\Rector\MethodCall; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PHPStan\Type\ObjectType; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * Swaps the 2nd and 3rd arguments of EventManagerInterface::on() when called with 3 arguments. | ||
| * | ||
| * @see \Cake\Upgrade\Test\TestCase\Rector\MethodCall\EventManagerOnRector\EventManagerOnRectorTest | ||
| */ | ||
| final class EventManagerOnRector extends AbstractRector | ||
| { | ||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Swaps the 2nd and 3rd arguments of EventManagerInterface::on() to match new signature', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| $eventManager->on('Model.beforeSave', ['priority' => 90], $callable); | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| $eventManager->on('Model.beforeSave', $callable, ['priority' => 90]); | ||
| CODE_SAMPLE, | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<\PhpParser\Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [MethodCall::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param \PhpParser\Node\Expr\MethodCall $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| // Check if this is a call to the 'on' method | ||
| if (!$this->isName($node->name, 'on')) { | ||
| return null; | ||
| } | ||
|
|
||
| // Check if the object implements EventManagerInterface | ||
| if (!$this->isObjectType($node->var, new ObjectType('Cake\Event\EventManagerInterface'))) { | ||
| return null; | ||
| } | ||
|
|
||
| // Only process if there are exactly 3 arguments | ||
| if (count($node->args) !== 3) { | ||
| return null; | ||
| } | ||
|
|
||
| // Swap the 2nd and 3rd arguments | ||
| $secondArg = $node->args[1]; | ||
| $thirdArg = $node->args[2]; | ||
|
|
||
| $node->args[1] = $thirdArg; | ||
| $node->args[2] = $secondArg; | ||
|
|
||
| return $node; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
tests/TestCase/Rector/MethodCall/EventManagerOnRector/EventManagerOnRectorTest.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,28 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\EventManagerOnRector; | ||
|
|
||
| use Iterator; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class EventManagerOnRectorTest extends AbstractRectorTestCase | ||
| { | ||
| /** | ||
| * @dataProvider provideData() | ||
| */ | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
tests/TestCase/Rector/MethodCall/EventManagerOnRector/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,67 @@ | ||
| <?php | ||
|
|
||
| namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\EventManagerOnRector\Fixture; | ||
|
|
||
| use Cake\Event\EventManager; | ||
|
|
||
| class Fixture | ||
| { | ||
| public function run() | ||
| { | ||
| $eventManager = EventManager::instance(); | ||
|
|
||
| // Should be transformed - 3 arguments with array literal | ||
| $eventManager->on('Model.beforeSave', ['priority' => 90], $callable); | ||
|
|
||
| // Should be transformed - 3 arguments with different options | ||
| $eventManager->on('Controller.initialize', ['priority' => 10], function () { | ||
| return true; | ||
| }); | ||
|
|
||
| // Should NOT be transformed - 2 arguments | ||
| $eventManager->on('Model.afterSave', $callable); | ||
|
|
||
| // Should NOT be transformed - 1 argument | ||
| $eventManager->on('Model.afterDelete'); | ||
|
|
||
| // Should be transformed - 3 arguments with variable | ||
| $options = ['priority' => 100]; | ||
| $eventManager->on('Model.beforeFind', $options, $handler); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\EventManagerOnRector\Fixture; | ||
|
|
||
| use Cake\Event\EventManager; | ||
|
|
||
| class Fixture | ||
| { | ||
| public function run() | ||
| { | ||
| $eventManager = EventManager::instance(); | ||
|
|
||
| // Should be transformed - 3 arguments with array literal | ||
| $eventManager->on('Model.beforeSave', $callable, ['priority' => 90]); | ||
|
|
||
| // Should be transformed - 3 arguments with different options | ||
| $eventManager->on('Controller.initialize', function () { | ||
| return true; | ||
| }, ['priority' => 10]); | ||
|
|
||
| // Should NOT be transformed - 2 arguments | ||
| $eventManager->on('Model.afterSave', $callable); | ||
|
|
||
| // Should NOT be transformed - 1 argument | ||
| $eventManager->on('Model.afterDelete'); | ||
|
|
||
| // Should be transformed - 3 arguments with variable | ||
| $options = ['priority' => 100]; | ||
| $eventManager->on('Model.beforeFind', $handler, $options); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
9 changes: 9 additions & 0 deletions
9
tests/TestCase/Rector/MethodCall/EventManagerOnRector/config/configured_rule.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,9 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| use Cake\Upgrade\Rector\Rector\MethodCall\EventManagerOnRector; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(EventManagerOnRector::class); | ||
| }; |
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.
this is not a valid usecase but I can clean that up later