From 54d6bf42b1bbcd7f2bd69a2eb557352a17db9451 Mon Sep 17 00:00:00 2001 From: Max Hoogenbosch Date: Fri, 3 Apr 2026 12:34:32 +0200 Subject: [PATCH 1/2] Add rule to convert phpunit methods to snake case --- .../Fixture/rename.php.inc | 37 +++++++ .../PreferTestsWithSnakeCaseRectorTest.php | 28 ++++++ .../config/configured_rule.php | 10 ++ .../PreferTestsWithSnakeCaseRector.php | 97 +++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector.php diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc new file mode 100644 index 00000000..88f2c48c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.php b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.php new file mode 100644 index 00000000..686af464 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.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/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/config/configured_rule.php new file mode 100644 index 00000000..e7faaf99 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(PreferTestsWithSnakeCaseRector::class); +}; diff --git a/rules/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector.php b/rules/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector.php new file mode 100644 index 00000000..2be20c1a --- /dev/null +++ b/rules/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector.php @@ -0,0 +1,97 @@ +> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class]; + } + + /** + * @param ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) { + return null; + } + + $currentName = $node->name->toString(); + $newName = $this->toSnakeCase($currentName); + + if ($currentName === $newName) { + return null; + } + + $node->name = new Node\Identifier($newName); + + return $node; + } + + public function toSnakeCase(string $value): string + { + if (ctype_lower($value)) { + return $value; + } + + $value = (string) preg_replace('/\s+/u', '', ucwords($value)); + $value = (string) preg_replace('/(.)(?=[A-Z])/u', '$1_', $value); + + return strtolower($value); + } +} From 40f12c63bb154a02abb006ce9561774af9e9fad2 Mon Sep 17 00:00:00 2001 From: Max Hoogenbosch Date: Fri, 3 Apr 2026 12:48:10 +0200 Subject: [PATCH 2/2] Add rule to convert phpunit methods to camel case --- .../Fixture/rename.php.inc | 37 ++++++++ .../PreferTestsWithCamelCaseRectorTest.php | 28 ++++++ .../config/configured_rule.php | 10 ++ .../PreferTestsWithCamelCaseRector.php | 93 +++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector.php diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc new file mode 100644 index 00000000..2151c12c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.php b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.php new file mode 100644 index 00000000..507409a0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.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/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/config/configured_rule.php new file mode 100644 index 00000000..ac1d7c14 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(PreferTestsWithCamelCaseRector::class); +}; diff --git a/rules/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector.php b/rules/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector.php new file mode 100644 index 00000000..a70e5062 --- /dev/null +++ b/rules/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector.php @@ -0,0 +1,93 @@ +> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class]; + } + + /** + * @param ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) { + return null; + } + + $currentName = $node->name->toString(); + $newName = $this->toCamelCase($currentName); + + if ($currentName === $newName) { + return null; + } + + $node->name = new Node\Identifier($newName); + + return $node; + } + + public function toCamelCase(string $value): string + { + $words = explode(' ', str_replace(['-', '_'], ' ', $value)); + $words = array_map(fn (string $word) => ucfirst($word), $words); + + return lcfirst(implode($words)); + } +}