From 909df221701c44dfb45dce42ac3aee0ea0a540c8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:09:43 +0000 Subject: [PATCH 1/9] Initial plan From 95739cba27461a89ed1c10d0b61d957d84352946 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:20:24 +0000 Subject: [PATCH 2/9] Add EventManagerOnRector for EventManager::on() signature change Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- config/rector/sets/cakephp60.php | 4 + .../MethodCall/EventManagerOnRector.php | 76 +++++++++++++++++++ .../EventManagerOnRectorTest.php | 28 +++++++ .../Fixture/fixture.php.inc | 67 ++++++++++++++++ .../config/configured_rule.php | 9 +++ 5 files changed, 184 insertions(+) create mode 100644 src/Rector/Rector/MethodCall/EventManagerOnRector.php create mode 100644 tests/TestCase/Rector/MethodCall/EventManagerOnRector/EventManagerOnRectorTest.php create mode 100644 tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc create mode 100644 tests/TestCase/Rector/MethodCall/EventManagerOnRector/config/configured_rule.php diff --git a/config/rector/sets/cakephp60.php b/config/rector/sets/cakephp60.php index 0aff07bd..729cc195 100644 --- a/config/rector/sets/cakephp60.php +++ b/config/rector/sets/cakephp60.php @@ -1,6 +1,7 @@ rule(EventManagerOnRector::class); + // Changes related to the accessible => patchable rename $rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [ new MethodCallRename('Cake\ORM\Entity', 'setAccess', 'setPatchable'), diff --git a/src/Rector/Rector/MethodCall/EventManagerOnRector.php b/src/Rector/Rector/MethodCall/EventManagerOnRector.php new file mode 100644 index 00000000..3b256193 --- /dev/null +++ b/src/Rector/Rector/MethodCall/EventManagerOnRector.php @@ -0,0 +1,76 @@ +on('Model.beforeSave', ['priority' => 90], $callable); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$eventManager->on('Model.beforeSave', $callable, ['priority' => 90]); +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + 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 is an EventManager + if (!$this->isObjectType($node->var, new ObjectType('Cake\Event\EventManager'))) { + 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; + } +} diff --git a/tests/TestCase/Rector/MethodCall/EventManagerOnRector/EventManagerOnRectorTest.php b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/EventManagerOnRectorTest.php new file mode 100644 index 00000000..074c8dcf --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/EventManagerOnRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..5ceb98da --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc @@ -0,0 +1,67 @@ +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); + } +} + +?> +----- +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); + } +} + +?> diff --git a/tests/TestCase/Rector/MethodCall/EventManagerOnRector/config/configured_rule.php b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/config/configured_rule.php new file mode 100644 index 00000000..84e5dd29 --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/config/configured_rule.php @@ -0,0 +1,9 @@ +rule(EventManagerOnRector::class); +}; From 965ccd0c5324a52ea91ca12d953b2e42eaaa16f6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:28:25 +0000 Subject: [PATCH 3/9] Refine EventManagerOnRector and update test fixture Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- composer.json | 6 +++++- .../MethodCall/EventManagerOnRector/Fixture/fixture.php.inc | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 9f42ee59..91989b8c 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,11 @@ "symfony/string": "^6.0 || ^7.0" }, "require-dev": { - "cakephp/cakephp-codesniffer": "^5.0" + "cakephp/cakephp": "5.x-dev", + "cakephp/cakephp-codesniffer": "^5.0", + "cakephp/migrations": "^4.5.0", + "mikey179/vfsstream": "^1.6.8", + "phpunit/phpunit": "^10.5.38" }, "autoload": { "psr-4": { diff --git a/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc index 5ceb98da..71ef3bd2 100644 --- a/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc +++ b/tests/TestCase/Rector/MethodCall/EventManagerOnRector/Fixture/fixture.php.inc @@ -10,7 +10,7 @@ class Fixture { $eventManager = EventManager::instance(); - // Should be transformed - 3 arguments + // Should be transformed - 3 arguments with array literal $eventManager->on('Model.beforeSave', ['priority' => 90], $callable); // Should be transformed - 3 arguments with different options @@ -44,7 +44,7 @@ class Fixture { $eventManager = EventManager::instance(); - // Should be transformed - 3 arguments + // Should be transformed - 3 arguments with array literal $eventManager->on('Model.beforeSave', $callable, ['priority' => 90]); // Should be transformed - 3 arguments with different options From 165b94d5a43b9ee2760a478fa0c0278ab110b42f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:31:02 +0000 Subject: [PATCH 4/9] Revert accidental composer.json changes Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- composer.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 91989b8c..9f42ee59 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,7 @@ "symfony/string": "^6.0 || ^7.0" }, "require-dev": { - "cakephp/cakephp": "5.x-dev", - "cakephp/cakephp-codesniffer": "^5.0", - "cakephp/migrations": "^4.5.0", - "mikey179/vfsstream": "^1.6.8", - "phpunit/phpunit": "^10.5.38" + "cakephp/cakephp-codesniffer": "^5.0" }, "autoload": { "psr-4": { From 824f62be5227884f07b6d1c3e3a30884ae54bf38 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:39:27 +0000 Subject: [PATCH 5/9] Change type check to EventManagerInterface instead of EventManager Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- src/Rector/Rector/MethodCall/EventManagerOnRector.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Rector/Rector/MethodCall/EventManagerOnRector.php b/src/Rector/Rector/MethodCall/EventManagerOnRector.php index 3b256193..c1695635 100644 --- a/src/Rector/Rector/MethodCall/EventManagerOnRector.php +++ b/src/Rector/Rector/MethodCall/EventManagerOnRector.php @@ -54,8 +54,8 @@ public function refactor(Node $node): ?Node return null; } - // Check if the object is an EventManager - if (!$this->isObjectType($node->var, new ObjectType('Cake\Event\EventManager'))) { + // Check if the object implements EventManagerInterface + if (!$this->isObjectType($node->var, new ObjectType('Cake\Event\EventManagerInterface'))) { return null; } From 876b895adbb2b4f40cba21f97c6c6f850c735f5d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:52:27 +0000 Subject: [PATCH 6/9] Fix coding style: remove trailing whitespace Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- src/Rector/Rector/MethodCall/EventManagerOnRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rector/Rector/MethodCall/EventManagerOnRector.php b/src/Rector/Rector/MethodCall/EventManagerOnRector.php index c1695635..b63dfc33 100644 --- a/src/Rector/Rector/MethodCall/EventManagerOnRector.php +++ b/src/Rector/Rector/MethodCall/EventManagerOnRector.php @@ -67,7 +67,7 @@ public function refactor(Node $node): ?Node // Swap the 2nd and 3rd arguments $secondArg = $node->args[1]; $thirdArg = $node->args[2]; - + $node->args[1] = $thirdArg; $node->args[2] = $secondArg; From 106013490a04d73e47179ad8c4f42bd5c433d244 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 17:05:46 +0000 Subject: [PATCH 7/9] Fix coding style: remove blank line after function opening brace Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- config/rector/sets/cakephp60.php | 1 - 1 file changed, 1 deletion(-) diff --git a/config/rector/sets/cakephp60.php b/config/rector/sets/cakephp60.php index 729cc195..6410f218 100644 --- a/config/rector/sets/cakephp60.php +++ b/config/rector/sets/cakephp60.php @@ -17,7 +17,6 @@ # @see https://book.cakephp.org/6/en/appendices/6-0-migration-guide.html return static function (RectorConfig $rectorConfig): void { - // EventManager::on() signature change $rectorConfig->rule(EventManagerOnRector::class); From 42409dad2ca9034b46571a170ba4beddf268b22f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 17:12:50 +0000 Subject: [PATCH 8/9] Fix docblock format: add blank line before last @see tag Co-authored-by: ADmad <142658+ADmad@users.noreply.github.com> --- src/Rector/Rector/MethodCall/EventManagerOnRector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Rector/Rector/MethodCall/EventManagerOnRector.php b/src/Rector/Rector/MethodCall/EventManagerOnRector.php index b63dfc33..84b98a5a 100644 --- a/src/Rector/Rector/MethodCall/EventManagerOnRector.php +++ b/src/Rector/Rector/MethodCall/EventManagerOnRector.php @@ -14,6 +14,7 @@ * Swaps the 2nd and 3rd arguments of EventManager::on() when called with 3 arguments. * * @see https://github.com/cakephp/cakephp/issues/19068 + * * @see \Cake\Upgrade\Test\TestCase\Rector\MethodCall\EventManagerOnRector\EventManagerOnRectorTest */ final class EventManagerOnRector extends AbstractRector From f434bc76e2ac8c6a3518363af37f86bb18ad8e2d Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 17 Nov 2025 22:58:08 +0530 Subject: [PATCH 9/9] Fix CS errors --- src/Rector/Rector/MethodCall/EventManagerOnRector.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Rector/Rector/MethodCall/EventManagerOnRector.php b/src/Rector/Rector/MethodCall/EventManagerOnRector.php index 84b98a5a..18ced45e 100644 --- a/src/Rector/Rector/MethodCall/EventManagerOnRector.php +++ b/src/Rector/Rector/MethodCall/EventManagerOnRector.php @@ -11,9 +11,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * Swaps the 2nd and 3rd arguments of EventManager::on() when called with 3 arguments. - * - * @see https://github.com/cakephp/cakephp/issues/19068 + * Swaps the 2nd and 3rd arguments of EventManagerInterface::on() when called with 3 arguments. * * @see \Cake\Upgrade\Test\TestCase\Rector\MethodCall\EventManagerOnRector\EventManagerOnRectorTest */ @@ -22,7 +20,7 @@ final class EventManagerOnRector extends AbstractRector public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( - 'Swaps the 2nd and 3rd arguments of EventManager::on() to match new signature', + 'Swaps the 2nd and 3rd arguments of EventManagerInterface::on() to match new signature', [ new CodeSample( <<<'CODE_SAMPLE' @@ -31,9 +29,9 @@ public function getRuleDefinition(): RuleDefinition , <<<'CODE_SAMPLE' $eventManager->on('Model.beforeSave', $callable, ['priority' => 90]); -CODE_SAMPLE +CODE_SAMPLE, ), - ] + ], ); }