Skip to content

Commit c410da9

Browse files
authored
[Renaming] Allow self/static on RenamePropertyRector (#7444)
* [Renaming] Allow self/static on RenamePropertyRector * fix * fix
1 parent 6a6047c commit c410da9

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
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\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture;
4+
5+
class MyClass
6+
{
7+
protected static array $_config = ['tmp'];
8+
9+
public static function test() {
10+
$tmp = self::$_config;
11+
return static::$_config;
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture;
20+
21+
class MyClass
22+
{
23+
protected static array $config = ['tmp'];
24+
25+
public static function test() {
26+
$tmp = self::$config;
27+
return static::$config;
28+
}
29+
}
30+
31+
?>

rules-tests/Renaming/Rector/PropertyFetch/RenamePropertyRector/config/configured_rule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@
3434
'oldProperty',
3535
'newProperty'
3636
),
37+
new RenameProperty(
38+
'Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture\MyClass',
39+
'_config',
40+
'config'
41+
),
3742
]);
3843
};

rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\PropertyFetch;
9+
use PhpParser\Node\Expr\StaticPropertyFetch;
910
use PhpParser\Node\Identifier;
1011
use PhpParser\Node\Stmt\ClassLike;
1112
use PhpParser\Node\Stmt\Property;
@@ -46,11 +47,11 @@ public function getRuleDefinition(): RuleDefinition
4647
*/
4748
public function getNodeTypes(): array
4849
{
49-
return [PropertyFetch::class, ClassLike::class];
50+
return [PropertyFetch::class, StaticPropertyFetch::class, ClassLike::class];
5051
}
5152

5253
/**
53-
* @param PropertyFetch|ClassLike $node
54+
* @param PropertyFetch|StaticPropertyFetch|ClassLike $node
5455
*/
5556
public function refactor(Node $node): ?Node
5657
{
@@ -110,19 +111,22 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp
110111
$property->props[0]->name = new VarLikeIdentifier($newProperty);
111112
}
112113

113-
private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?PropertyFetch
114+
private function refactorPropertyFetch(PropertyFetch|StaticPropertyFetch $propertyFetch): null|PropertyFetch|StaticPropertyFetch
114115
{
115116
foreach ($this->renamedProperties as $renamedProperty) {
116117
$oldProperty = $renamedProperty->getOldProperty();
117118
if (! $this->isName($propertyFetch, $oldProperty)) {
118119
continue;
119120
}
120121

121-
if (! $this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) {
122+
$varPropertyFetch = $propertyFetch instanceof PropertyFetch ? $propertyFetch->var : $propertyFetch->class;
123+
if (! $this->isObjectType($varPropertyFetch, $renamedProperty->getObjectType())) {
122124
continue;
123125
}
124126

125-
$propertyFetch->name = new Identifier($renamedProperty->getNewProperty());
127+
$propertyFetch->name = $propertyFetch instanceof PropertyFetch
128+
? new Identifier($renamedProperty->getNewProperty())
129+
: new VarLikeIdentifier($renamedProperty->getNewProperty());
126130
return $propertyFetch;
127131
}
128132

0 commit comments

Comments
 (0)