Skip to content

Commit 2fdf38b

Browse files
committed
address review: use Class_ node type, skip rename on existing method name collision
1 parent cc3684d commit 2fdf38b

10 files changed

Lines changed: 99 additions & 39 deletions

File tree

rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc renamed to rules-tests/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -19,7 +19,7 @@ final class RenameTest extends TestCase
1919
-----
2020
<?php
2121

22-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector\Fixture;
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture;
2323

2424
use PHPUnit\Framework\TestCase;
2525

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipExistingNameTest extends TestCase
8+
{
9+
public function test_me()
10+
{
11+
}
12+
13+
public function testMe()
14+
{
15+
}
16+
}

rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.php renamed to rules-tests/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector;
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;

rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector/config/configured_rule.php renamed to rules-tests/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector;
6+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector;
77

88
return static function (RectorConfig $rectorConfig): void {
99
$rectorConfig->rule(PreferTestsWithCamelCaseRector::class);

rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc renamed to rules-tests/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithSnakeCaseRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -19,7 +19,7 @@ final class RenameTest extends TestCase
1919
-----
2020
<?php
2121

22-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithSnakeCaseRector\Fixture;
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture;
2323

2424
use PHPUnit\Framework\TestCase;
2525

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipExistingNameTest extends TestCase
8+
{
9+
public function testMe()
10+
{
11+
}
12+
13+
public function test_me()
14+
{
15+
}
16+
}

rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.php renamed to rules-tests/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithSnakeCaseRector;
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;

rules-tests/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector/config/configured_rule.php renamed to rules-tests/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\PreferTestsWithSnakeCaseRector;
6+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector;
77

88
return static function (RectorConfig $rectorConfig): void {
99
$rectorConfig->rule(PreferTestsWithSnakeCaseRector::class);

rules/CodeQuality/Rector/ClassMethod/PreferTestsWithCamelCaseRector.php renamed to rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
5+
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Stmt\ClassMethod;
8+
use PhpParser\Node\Stmt\Class_;
99
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1010
use Rector\Rector\AbstractRector;
1111
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1212
use 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
*/
1717
final 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'
2929
use 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
}

rules/CodeQuality/Rector/ClassMethod/PreferTestsWithSnakeCaseRector.php renamed to rules/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
5+
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Stmt\ClassMethod;
8+
use PhpParser\Node\Stmt\Class_;
99
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1010
use Rector\Rector\AbstractRector;
1111
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1212
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1313

1414
/**
15-
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithSnakeCaseRector\PreferTestsWithSnakeCaseRectorTest
15+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\PreferTestsWithSnakeCaseRectorTest
1616
*/
1717
final class PreferTestsWithSnakeCaseRector 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 snake case', [
26+
return new RuleDefinition('Changes PHPUnit test methods to snake case', [
2727
new CodeSample(
2828
<<<'CODE_SAMPLE'
2929
use PHPUnit\Framework\TestCase;
@@ -55,35 +55,49 @@ public function test_something()
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->toSnakeCase($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->toSnakeCase($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 toSnakeCase(string $value): string
100+
private function toSnakeCase(string $value): string
87101
{
88102
if (ctype_lower($value)) {
89103
return $value;

0 commit comments

Comments
 (0)