Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture;

class MyClass
{
protected static array $_config = ['tmp'];

public static function test() {
$tmp = self::$_config;
return static::$_config;
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture;

class MyClass
{
protected static array $config = ['tmp'];

public static function test() {
$tmp = self::$config;
return static::$config;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
'oldProperty',
'newProperty'
),
new RenameProperty(
'Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture\MyClass',
'_config',
'config'
),
]);
};
14 changes: 9 additions & 5 deletions rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
Expand Down Expand Up @@ -46,11 +47,11 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [PropertyFetch::class, ClassLike::class];
return [PropertyFetch::class, StaticPropertyFetch::class, ClassLike::class];
}

/**
* @param PropertyFetch|ClassLike $node
* @param PropertyFetch|StaticPropertyFetch|ClassLike $node
*/
public function refactor(Node $node): ?Node
{
Expand Down Expand Up @@ -110,19 +111,22 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp
$property->props[0]->name = new VarLikeIdentifier($newProperty);
}

private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?PropertyFetch
private function refactorPropertyFetch(PropertyFetch|StaticPropertyFetch $propertyFetch): null|PropertyFetch|StaticPropertyFetch
{
foreach ($this->renamedProperties as $renamedProperty) {
$oldProperty = $renamedProperty->getOldProperty();
if (! $this->isName($propertyFetch, $oldProperty)) {
continue;
}

if (! $this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) {
$varPropertyFetch = $propertyFetch instanceof PropertyFetch ? $propertyFetch->var : $propertyFetch->class;
if (! $this->isObjectType($varPropertyFetch, $renamedProperty->getObjectType())) {
continue;
}

$propertyFetch->name = new Identifier($renamedProperty->getNewProperty());
$propertyFetch->name = $propertyFetch instanceof PropertyFetch
? new Identifier($renamedProperty->getNewProperty())
: new VarLikeIdentifier($renamedProperty->getNewProperty());
return $propertyFetch;
}

Expand Down