Skip to content

Commit 2d3243a

Browse files
fix: replace getKey method for name property (#6583)
1 parent 484098b commit 2d3243a

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum;
6+
7+
final class UsageOfGetKey
8+
{
9+
public function run($value)
10+
{
11+
$name = SomeEnum::USED_TO_BE_CONST()->getKey();
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Fixture;
20+
21+
use Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum;
22+
23+
final class UsageOfGetKey
24+
{
25+
public function run($value)
26+
{
27+
$name = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST->name;
28+
}
29+
}
30+
31+
?>

rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getRuleDefinition(): RuleDefinition
4646

4747
,
4848
<<<'CODE_SAMPLE'
49-
$name = SomeEnum::VALUE;
49+
$name = SomeEnum::VALUE->name;
5050
CODE_SAMPLE
5151
),
5252
]);
@@ -110,7 +110,7 @@ private function isEnumConstant(string $className, string $constant): bool
110110
return $classReflection->hasConstant($constant);
111111
}
112112

113-
private function refactorGetKeyMethodCall(MethodCall $methodCall): ?ClassConstFetch
113+
private function refactorGetKeyMethodCall(MethodCall $methodCall): ?PropertyFetch
114114
{
115115
if (! $methodCall->var instanceof StaticCall) {
116116
return null;
@@ -131,7 +131,9 @@ private function refactorGetKeyMethodCall(MethodCall $methodCall): ?ClassConstFe
131131
return null;
132132
}
133133

134-
return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
134+
$classConstFetch = $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
135+
136+
return new PropertyFetch($classConstFetch, 'name');
135137
}
136138

137139
private function refactorGetValueMethodCall(MethodCall $methodCall): ?PropertyFetch
@@ -266,10 +268,8 @@ private function getEnumConstFetch(StaticCall $staticCall): null|ClassConstFetch
266268
return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
267269
}
268270

269-
private function refactorMethodCall(
270-
MethodCall $methodCall,
271-
string $methodName
272-
): null|ClassConstFetch|PropertyFetch|Identical {
271+
private function refactorMethodCall(MethodCall $methodCall, string $methodName): null|PropertyFetch|Identical
272+
{
273273
if (! $this->isObjectType($methodCall->var, new ObjectType('MyCLabs\Enum\Enum'))) {
274274
return null;
275275
}

0 commit comments

Comments
 (0)