From 584f27f6a641ab997eab0c62d5b17fadaa332960 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sun, 29 Nov 2026 12:44:40 +0100 Subject: [PATCH] Add more CakePHP 6 upgrade rules - Rename Cake\Core container classes and league/container classes to Cake\Container (cakephp/cakephp#19559) - Add void return type to Controller callbacks (cakephp/cakephp#18660) - Remove assignments from Table::deleteManyOrFail() (cakephp/cakephp#19433) - Use ConsoleIoInterface in beforeExecute()/afterExecute() (cakephp/cakephp#18391) - Rename Entity::$_fields to $dynamicFields (cakephp/cakephp#19381) - Don't strip $args/$io from command event hooks (cakephp/cakephp#18983) --- config/rector/sets/cakephp60.php | 93 ++++++++++++++++++- ...placeCommandArgsIoWithPropertiesRector.php | 5 + .../Fixture/skip_event_hooks.php.inc | 22 +++++ .../src/Command/TestCommand.php | 9 ++ .../src/Container/AppServiceProvider.php | 23 +++++ .../src/Container/ContainerUsage.php | 18 ++++ .../src/Controller/ArticlesController.php | 30 ++++++ .../src/Model/Entity/Category.php | 4 + .../src/VoidMethods.php | 7 ++ .../src/Command/TestCommand.php | 9 ++ .../src/Container/AppServiceProvider.php | 23 +++++ .../src/Container/ContainerUsage.php | 18 ++++ .../src/Controller/ArticlesController.php | 30 ++++++ .../src/Model/Entity/Category.php | 4 + .../src/VoidMethods.php | 6 ++ 15 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 tests/TestCase/Rector/MethodCall/ReplaceCommandArgsIoWithPorpertiesRector/Fixture/skip_event_hooks.php.inc create mode 100644 tests/test_apps/original/RectorCommand-testApply60/src/Container/AppServiceProvider.php create mode 100644 tests/test_apps/original/RectorCommand-testApply60/src/Container/ContainerUsage.php create mode 100644 tests/test_apps/original/RectorCommand-testApply60/src/Controller/ArticlesController.php create mode 100644 tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/AppServiceProvider.php create mode 100644 tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/ContainerUsage.php create mode 100644 tests/test_apps/upgraded/RectorCommand-testApply60/src/Controller/ArticlesController.php diff --git a/config/rector/sets/cakephp60.php b/config/rector/sets/cakephp60.php index c1e2feba..19644776 100644 --- a/config/rector/sets/cakephp60.php +++ b/config/rector/sets/cakephp60.php @@ -17,6 +17,7 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\StringType; use PHPStan\Type\UnionType; +use PHPStan\Type\VoidType; use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope; use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector; use Rector\Arguments\ValueObject\ArgumentAdder; @@ -69,6 +70,70 @@ new RenameProperty('Cake\ORM\Entity', '_accessible', 'patchable'), // View::$_helpers conflicts with View::$helpers (config array), so rename to $helperRegistry new RenameProperty('Cake\View\View', '_helpers', 'helperRegistry'), + // Entity::$_fields became $dynamicFields, not $fields + // @see https://github.com/cakephp/cakephp/pull/19381 + new RenameProperty('Cake\ORM\Entity', '_fields', 'dynamicFields'), + new RenameProperty('Cake\Datasource\EntityTrait', '_fields', 'dynamicFields'), + ]); + + // ===== Dependency injection: league/container is gone, use Cake\Container ===== + // @see https://github.com/cakephp/cakephp/pull/19559 + $containerClasses = [ + 'Argument\ArgumentInterface', + 'Argument\ArgumentResolverInterface', + 'Argument\ArgumentResolverTrait', + 'Argument\DefaultValueArgument', + 'Argument\DefaultValueInterface', + 'Argument\Literal\ArrayArgument', + 'Argument\Literal\BooleanArgument', + 'Argument\Literal\CallableArgument', + 'Argument\Literal\FloatArgument', + 'Argument\Literal\IntegerArgument', + 'Argument\Literal\ObjectArgument', + 'Argument\Literal\StringArgument', + 'Argument\LiteralArgument', + 'Argument\LiteralArgumentInterface', + 'Argument\ResolvableArgument', + 'Argument\ResolvableArgumentInterface', + 'Container', + 'ContainerAwareInterface', + 'ContainerAwareTrait', + 'Definition\Definition', + 'Definition\DefinitionAggregate', + 'Definition\DefinitionAggregateInterface', + 'Definition\DefinitionInterface', + 'Exception\ContainerException', + 'Exception\NotFoundException', + 'Inflector\Inflector', + 'Inflector\InflectorAggregate', + 'Inflector\InflectorAggregateInterface', + 'Inflector\InflectorInterface', + 'ReflectionContainer', + 'ServiceProvider\AbstractServiceProvider', + 'ServiceProvider\BootableServiceProviderInterface', + 'ServiceProvider\ServiceProviderAggregate', + 'ServiceProvider\ServiceProviderAggregateInterface', + 'ServiceProvider\ServiceProviderInterface', + ]; + $containerRenames = [ + 'Cake\Core\Container' => 'Cake\Container\Container', + 'Cake\Core\ContainerInterface' => 'Cake\Container\ContainerInterface', + 'Cake\Core\ServiceProvider' => 'Cake\Container\ServiceProvider', + 'Cake\Container\DefinitionContainerInterface' => 'Cake\Container\ContainerInterface', + 'League\Container\DefinitionContainerInterface' => 'Cake\Container\ContainerInterface', + ]; + foreach ($containerClasses as $containerClass) { + $containerRenames['League\\Container\\' . $containerClass] = 'Cake\\Container\\' . $containerClass; + } + $rectorConfig->ruleWithConfiguration(RenameClassRector::class, $containerRenames); + + // Controller callbacks now declare a void return type + // @see https://github.com/cakephp/cakephp/pull/18660 + $rectorConfig->ruleWithConfiguration(AddReturnTypeDeclarationRector::class, [ + new AddReturnTypeDeclaration('Cake\Controller\Controller', 'beforeFilter', new VoidType()), + new AddReturnTypeDeclaration('Cake\Controller\Controller', 'beforeRender', new VoidType()), + new AddReturnTypeDeclaration('Cake\Controller\Controller', 'afterFilter', new VoidType()), + new AddReturnTypeDeclaration('Cake\Controller\Controller', 'beforeRedirect', new VoidType()), ]); $rectorConfig->ruleWithConfiguration(RenameStringRector::class, [ @@ -326,7 +391,7 @@ 'Cake\Datasource\ModelAwareTrait' => ['_modelFactories', '_modelType'], 'Cake\Datasource\ConnectionManager' => ['_dsnClassMap', '_registry', '_aliasMap'], 'Cake\Datasource\EntityTrait' => [ - '_fields', '_original', '_originalFields', '_hidden', '_virtual', '_dirty', + '_original', '_originalFields', '_hidden', '_virtual', '_dirty', '_accessors', '_new', '_errors', '_invalid', '_registryAlias', '_hasBeenVisited', ], ], @@ -649,6 +714,30 @@ 1, new ObjectType('Cake\Console\ConsoleIoInterface'), ), + new AddParamTypeDeclaration( + 'Cake\\Command\\Command', + 'beforeExecute', + 2, + new ObjectType('Cake\\Console\\ConsoleIoInterface'), + ), + new AddParamTypeDeclaration( + 'Cake\\Console\\BaseCommand', + 'beforeExecute', + 2, + new ObjectType('Cake\\Console\\ConsoleIoInterface'), + ), + new AddParamTypeDeclaration( + 'Cake\\Command\\Command', + 'afterExecute', + 2, + new ObjectType('Cake\\Console\\ConsoleIoInterface'), + ), + new AddParamTypeDeclaration( + 'Cake\\Console\\BaseCommand', + 'afterExecute', + 2, + new ObjectType('Cake\\Console\\ConsoleIoInterface'), + ), ]); // ===== Remove underscores from method names ===== @@ -1223,6 +1312,8 @@ new VoidMethod('Cake\Http\Session', 'close'), // Table::deleteOrFail() - returns void or throws PersistenceFailedException new VoidMethod('Cake\ORM\Table', 'deleteOrFail'), + // Table::deleteManyOrFail() - returns void or throws PersistenceFailedException + new VoidMethod('Cake\ORM\Table', 'deleteManyOrFail'), // FixtureInterface::insert() - returns void new VoidMethod('Cake\Datasource\FixtureInterface', 'insert'), new VoidMethod('Cake\TestSuite\Fixture\TestFixture', 'insert'), diff --git a/src/Rector/Cake6/ReplaceCommandArgsIoWithPropertiesRector.php b/src/Rector/Cake6/ReplaceCommandArgsIoWithPropertiesRector.php index 60834cad..6305e342 100644 --- a/src/Rector/Cake6/ReplaceCommandArgsIoWithPropertiesRector.php +++ b/src/Rector/Cake6/ReplaceCommandArgsIoWithPropertiesRector.php @@ -92,6 +92,11 @@ public function refactor(Node $node): ?Node return null; } + // The command event hooks still receive $args and $io in 6.x + if ($this->isNames($node, ['beforeExecute', 'afterExecute'])) { + return null; + } + // Find if params are $args and/or $io $argsParam = $this->findParam($node, 'args'); $ioParam = $this->findParam($node, 'io'); diff --git a/tests/TestCase/Rector/MethodCall/ReplaceCommandArgsIoWithPorpertiesRector/Fixture/skip_event_hooks.php.inc b/tests/TestCase/Rector/MethodCall/ReplaceCommandArgsIoWithPorpertiesRector/Fixture/skip_event_hooks.php.inc new file mode 100644 index 00000000..a4a4d054 --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/ReplaceCommandArgsIoWithPorpertiesRector/Fixture/skip_event_hooks.php.inc @@ -0,0 +1,22 @@ +out('before'); + } + + public function afterExecute(EventInterface $event, Arguments $args, ConsoleIo $io, ?int $result): void + { + $io->out('after'); + } +} +?> diff --git a/tests/test_apps/original/RectorCommand-testApply60/src/Command/TestCommand.php b/tests/test_apps/original/RectorCommand-testApply60/src/Command/TestCommand.php index d4708c52..6da3f620 100644 --- a/tests/test_apps/original/RectorCommand-testApply60/src/Command/TestCommand.php +++ b/tests/test_apps/original/RectorCommand-testApply60/src/Command/TestCommand.php @@ -6,6 +6,7 @@ use Cake\Command\Command; use Cake\Console\Arguments; use Cake\Console\ConsoleIo; +use Cake\Event\EventInterface; class TestCommand extends Command { @@ -16,6 +17,14 @@ public function execute(Arguments $args, ConsoleIo $io) return static::CODE_SUCCESS; } + public function beforeExecute(EventInterface $event, Arguments $args, ConsoleIo $io): void + { + } + + public function afterExecute(EventInterface $event, Arguments $args, ConsoleIo $io, ?int $result): void + { + } + protected function someMethod(Arguments $args, ConsoleIo $io): void { $someArg = $args->getArgument('some'); diff --git a/tests/test_apps/original/RectorCommand-testApply60/src/Container/AppServiceProvider.php b/tests/test_apps/original/RectorCommand-testApply60/src/Container/AppServiceProvider.php new file mode 100644 index 00000000..099ff416 --- /dev/null +++ b/tests/test_apps/original/RectorCommand-testApply60/src/Container/AppServiceProvider.php @@ -0,0 +1,23 @@ +add('app.name', new StringArgument('app')); + } + + public function configure(DefinitionContainerInterface $container): void + { + } +} diff --git a/tests/test_apps/original/RectorCommand-testApply60/src/Container/ContainerUsage.php b/tests/test_apps/original/RectorCommand-testApply60/src/Container/ContainerUsage.php new file mode 100644 index 00000000..770a8f09 --- /dev/null +++ b/tests/test_apps/original/RectorCommand-testApply60/src/Container/ContainerUsage.php @@ -0,0 +1,18 @@ +delegate(new ReflectionContainer()); + + return $container; + } +} diff --git a/tests/test_apps/original/RectorCommand-testApply60/src/Controller/ArticlesController.php b/tests/test_apps/original/RectorCommand-testApply60/src/Controller/ArticlesController.php new file mode 100644 index 00000000..7aadeae5 --- /dev/null +++ b/tests/test_apps/original/RectorCommand-testApply60/src/Controller/ArticlesController.php @@ -0,0 +1,30 @@ +_fields); + } + public function setSource(string $name) { return parent::setSource($name); } diff --git a/tests/test_apps/original/RectorCommand-testApply60/src/VoidMethods.php b/tests/test_apps/original/RectorCommand-testApply60/src/VoidMethods.php index 5311831c..28b35235 100644 --- a/tests/test_apps/original/RectorCommand-testApply60/src/VoidMethods.php +++ b/tests/test_apps/original/RectorCommand-testApply60/src/VoidMethods.php @@ -5,6 +5,7 @@ use Cake\Core\Configure; use Cake\Http\ServerRequest; +use Cake\ORM\Table; class VoidMethods { @@ -20,4 +21,10 @@ public function test(ServerRequest $request): void $request->allowMethod(['GET', 'POST']); Configure::load('other'); } + + public function deleteMany(Table $table, array $entities): void + { + // Assignment should be removed + $result = $table->deleteManyOrFail($entities); + } } diff --git a/tests/test_apps/upgraded/RectorCommand-testApply60/src/Command/TestCommand.php b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Command/TestCommand.php index d29ffb01..cbf63f46 100644 --- a/tests/test_apps/upgraded/RectorCommand-testApply60/src/Command/TestCommand.php +++ b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Command/TestCommand.php @@ -6,6 +6,7 @@ use Cake\Command\Command; use Cake\Console\Arguments; use Cake\Console\ConsoleIo; +use Cake\Event\EventInterface; class TestCommand extends Command { @@ -16,6 +17,14 @@ public function execute() return static::CODE_SUCCESS; } + public function beforeExecute(EventInterface $event, Arguments $args, \Cake\Console\ConsoleIoInterface $io): void + { + } + + public function afterExecute(EventInterface $event, Arguments $args, \Cake\Console\ConsoleIoInterface $io, ?int $result): void + { + } + protected function someMethod(): void { $someArg = $this->args->getArgument('some'); diff --git a/tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/AppServiceProvider.php b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/AppServiceProvider.php new file mode 100644 index 00000000..895ba944 --- /dev/null +++ b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/AppServiceProvider.php @@ -0,0 +1,23 @@ +add('app.name', new \Cake\Container\Argument\Literal\StringArgument('app')); + } + + public function configure(\Cake\Container\ContainerInterface $container): void + { + } +} diff --git a/tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/ContainerUsage.php b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/ContainerUsage.php new file mode 100644 index 00000000..3f034482 --- /dev/null +++ b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Container/ContainerUsage.php @@ -0,0 +1,18 @@ +delegate(new \Cake\Container\ReflectionContainer()); + + return $container; + } +} diff --git a/tests/test_apps/upgraded/RectorCommand-testApply60/src/Controller/ArticlesController.php b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Controller/ArticlesController.php new file mode 100644 index 00000000..62b89139 --- /dev/null +++ b/tests/test_apps/upgraded/RectorCommand-testApply60/src/Controller/ArticlesController.php @@ -0,0 +1,30 @@ +dynamicFields); + } + public function setSource(string $name): static { return parent::setSource($name); } diff --git a/tests/test_apps/upgraded/RectorCommand-testApply60/src/VoidMethods.php b/tests/test_apps/upgraded/RectorCommand-testApply60/src/VoidMethods.php index 6180b011..8dd46309 100644 --- a/tests/test_apps/upgraded/RectorCommand-testApply60/src/VoidMethods.php +++ b/tests/test_apps/upgraded/RectorCommand-testApply60/src/VoidMethods.php @@ -5,6 +5,7 @@ use Cake\Core\Configure; use Cake\Http\ServerRequest; +use Cake\ORM\Table; class VoidMethods { @@ -18,4 +19,9 @@ public function test(ServerRequest $request): void $request->allowMethod(['GET', 'POST']); Configure::load('other'); } + + public function deleteMany(Table $table, array $entities): void + { + $table->deleteManyOrFail($entities); + } }