diff --git a/config/rector/sets/cakephp53.php b/config/rector/sets/cakephp53.php index 55bb177f..83c1c5d5 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); $rectorConfig->rule(QueryParamAccessRector::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); +};