Skip to content

Commit 0e5f03c

Browse files
committed
refactor(name): Preserve enum member types during renaming
- Add condition to skip renaming for identifiers within enum classes when the type is 'int' or 'string' - Ensure renaming does not alter enum member types, maintaining code consistency and correctness
1 parent b548d7e commit 0e5f03c

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Rector/Name/RenameToConventionalCaseNameRector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,14 @@ private function shouldLcfirstCamelName(Node $node): bool
760760
private function wrapRenamer(callable $renamer, Node $node): \Closure
761761
{
762762
return function (string $name) use ($node, $renamer): string {
763+
if (
764+
$node instanceof Identifier
765+
&& $node->getAttribute('parent') instanceof Enum_
766+
&& $this->isNames($node, ['int', 'string'])
767+
) {
768+
return $name;
769+
}
770+
763771
if (str_contains($name, '::')) {
764772
[$className, $constantOrMethodName] = Str::of($name)
765773
->explode('::', 2)

tests/Rector/Name/RenameToConventionalCaseNameRector/Fixture81/fixture.php.inc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
/** @noinspection ALL */
44
// @formatter:off
55
// phpcs:ignoreFile
6-
76
enum Suit: string
87
{
98
case Heart = 'Heart';
109
case Diamond = 'Diamond';
1110
case Club = 'Club';
12-
case Spade = 'Spade';
1311
}
14-
1512
Suit::Heart;
1613

1714
?>
@@ -21,15 +18,12 @@ Suit::Heart;
2118
/** @noinspection ALL */
2219
// @formatter:off
2320
// phpcs:ignoreFile
24-
2521
enum Suit: string
2622
{
2723
case Heart = 'Heart';
2824
case Diamond = 'Diamond';
2925
case Club = 'Club';
30-
case Spade = 'Spade';
3126
}
32-
33-
Suit::Heart;
27+
Suit::HEART;
3428

3529
?>

0 commit comments

Comments
 (0)