22
33declare (strict_types=1 );
44
5- namespace Rector \PHPUnit \CodeQuality \Rector \ClassMethod ;
5+ namespace Rector \PHPUnit \CodeQuality \Rector \Class_ ;
66
77use PhpParser \Node ;
8- use PhpParser \Node \Stmt \ClassMethod ;
8+ use PhpParser \Node \Stmt \Class_ ;
99use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
1010use Rector \Rector \AbstractRector ;
1111use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1212use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
1313
1414/**
15- * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod \PreferTestsWithCamelCaseRector\PreferTestsWithCamelCaseRectorTest
15+ * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_ \PreferTestsWithCamelCaseRector\PreferTestsWithCamelCaseRectorTest
1616 */
1717final class PreferTestsWithCamelCaseRector extends AbstractRector
1818{
@@ -23,7 +23,7 @@ public function __construct(
2323
2424 public function getRuleDefinition (): RuleDefinition
2525 {
26- return new RuleDefinition ('Changes PHPUnit tests methods to camel case ' , [
26+ return new RuleDefinition ('Changes PHPUnit test methods to camel case ' , [
2727 new CodeSample (
2828 <<<'CODE_SAMPLE'
2929use PHPUnit\Framework\TestCase;
@@ -55,38 +55,52 @@ public function testSomething()
5555 */
5656 public function getNodeTypes (): array
5757 {
58- return [ClassMethod ::class];
58+ return [Class_ ::class];
5959 }
6060
6161 /**
62- * @param ClassMethod $node
62+ * @param Class_ $node
6363 */
6464 public function refactor (Node $ node ): ?Node
6565 {
6666 if (! $ this ->testsNodeAnalyzer ->isInTestClass ($ node )) {
6767 return null ;
6868 }
6969
70- if (! $ this ->testsNodeAnalyzer ->isTestClassMethod ($ node )) {
71- return null ;
72- }
70+ $ hasChanged = false ;
7371
74- $ currentName = $ node ->name ->toString ();
75- $ newName = $ this ->toCamelCase ($ currentName );
72+ foreach ($ node ->getMethods () as $ classMethod ) {
73+ if (! $ this ->testsNodeAnalyzer ->isTestClassMethod ($ classMethod )) {
74+ continue ;
75+ }
7676
77- if ($ currentName === $ newName ) {
78- return null ;
77+ $ currentName = $ classMethod ->name ->toString ();
78+ $ newName = $ this ->toCamelCase ($ currentName );
79+
80+ if ($ currentName === $ newName ) {
81+ continue ;
82+ }
83+
84+ // avoid name collision with an existing method
85+ if ($ node ->getMethod ($ newName ) instanceof Node) {
86+ continue ;
87+ }
88+
89+ $ classMethod ->name = new Node \Identifier ($ newName );
90+ $ hasChanged = true ;
7991 }
8092
81- $ node ->name = new Node \Identifier ($ newName );
93+ if ($ hasChanged === false ) {
94+ return null ;
95+ }
8296
8397 return $ node ;
8498 }
8599
86- public function toCamelCase (string $ value ): string
100+ private function toCamelCase (string $ value ): string
87101 {
88102 $ words = explode (' ' , str_replace (['- ' , '_ ' ], ' ' , $ value ));
89- $ words = array_map (fn (string $ word ) => ucfirst ($ word ), $ words );
103+ $ words = array_map (fn (string $ word ): string => ucfirst ($ word ), $ words );
90104
91105 return lcfirst (implode ($ words ));
92106 }
0 commit comments