From 5633c6c5af8791dd0477acfa5f3bfb2e6016dddd Mon Sep 17 00:00:00 2001 From: mscherer Date: Mon, 10 Nov 2025 16:51:11 +0100 Subject: [PATCH] Add rector rule to rename Form::_execute() to process() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Form::_execute() is deprecated in CakePHP 5.x and should be renamed to process() which accepts the same parameters and has the same return type. This rector rule automatically handles the renaming while checking that process() doesn't already exist to avoid conflicts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- config/rector/sets/cakephp53.php | 2 + .../FormExecuteToProcessRector.php | 110 ++++++++++++++++++ .../Fixture/fixture.php.inc | 33 ++++++ .../Fixture/skip_when_process_exists.php.inc | 20 ++++ .../FormExecuteToProcessRectorTest.php | 28 +++++ .../config/configured_rule.php | 9 ++ 6 files changed, 202 insertions(+) create mode 100644 src/Rector/Rector/ClassMethod/FormExecuteToProcessRector.php create mode 100644 tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/fixture.php.inc create mode 100644 tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/skip_when_process_exists.php.inc create mode 100644 tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/FormExecuteToProcessRectorTest.php create mode 100644 tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/config/configured_rule.php diff --git a/config/rector/sets/cakephp53.php b/config/rector/sets/cakephp53.php index 356eac26..ed6b9501 100644 --- a/config/rector/sets/cakephp53.php +++ b/config/rector/sets/cakephp53.php @@ -1,6 +1,7 @@ rule(EntityIsEmptyRector::class); $rectorConfig->rule(EntityPatchRector::class); + $rectorConfig->rule(FormExecuteToProcessRector::class); }; diff --git a/src/Rector/Rector/ClassMethod/FormExecuteToProcessRector.php b/src/Rector/Rector/ClassMethod/FormExecuteToProcessRector.php new file mode 100644 index 00000000..4319e197 --- /dev/null +++ b/src/Rector/Rector/ClassMethod/FormExecuteToProcessRector.php @@ -0,0 +1,110 @@ +> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param \PhpParser\Node\Stmt\Class_ $node + */ + public function refactor(Node $node): ?Node + { + if (!$this->isObjectType($node, new ObjectType('Cake\Form\Form'))) { + return null; + } + + // Check if process() already exists + $hasProcess = false; + foreach ($node->stmts as $stmt) { + if ($stmt instanceof ClassMethod && $this->isName($stmt->name, 'process')) { + $hasProcess = true; + break; + } + } + + // If process() exists, don't rename _execute() + if ($hasProcess) { + return null; + } + + // Find and rename _execute() method + $hasChanges = false; + foreach ($node->stmts as $stmt) { + if (!$stmt instanceof ClassMethod) { + continue; + } + + if (!$this->isName($stmt->name, '_execute')) { + continue; + } + + // Rename the method to process() + $stmt->name = new Identifier('process'); + + $hasChanges = true; + break; + } + + return $hasChanges ? $node : null; + } +} diff --git a/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/fixture.php.inc b/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..b4fff6dd --- /dev/null +++ b/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/skip_when_process_exists.php.inc b/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/skip_when_process_exists.php.inc new file mode 100644 index 00000000..0b680e09 --- /dev/null +++ b/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/Fixture/skip_when_process_exists.php.inc @@ -0,0 +1,20 @@ +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/ClassMethod/FormExecuteToProcessRector/config/configured_rule.php b/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/config/configured_rule.php new file mode 100644 index 00000000..dab9a779 --- /dev/null +++ b/tests/TestCase/Rector/ClassMethod/FormExecuteToProcessRector/config/configured_rule.php @@ -0,0 +1,9 @@ +rule(FormExecuteToProcessRector::class); +};