-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathRenamePropertyRector.php
More file actions
135 lines (112 loc) · 4.29 KB
/
RenamePropertyRector.php
File metadata and controls
135 lines (112 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Rector\Renaming\Rector\PropertyFetch;
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;
use PhpParser\Node\VarLikeIdentifier;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\Renaming\ValueObject\RenameProperty;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\RenamePropertyRectorTest
*/
final class RenamePropertyRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var RenameProperty[]
*/
private array $renamedProperties = [];
private bool $hasChanged = false;
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace defined old properties by new ones', [
new ConfiguredCodeSample(
'$someObject->someOldProperty;',
'$someObject->someNewProperty;',
[new RenameProperty('SomeClass', 'someOldProperty', 'someNewProperty')]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [PropertyFetch::class, StaticPropertyFetch::class, ClassLike::class];
}
/**
* @param PropertyFetch|StaticPropertyFetch|ClassLike $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof ClassLike) {
$this->hasChanged = false;
foreach ($this->renamedProperties as $renamedProperty) {
$this->renameProperty($node, $renamedProperty);
}
if ($this->hasChanged) {
return $node;
}
return null;
}
return $this->refactorPropertyFetch($node);
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, RenameProperty::class);
$this->renamedProperties = $configuration;
}
private function renameProperty(ClassLike $classLike, RenameProperty $renameProperty): void
{
$classLikeName = (string) $this->getName($classLike);
$renamePropertyObjectType = $renameProperty->getObjectType();
$className = $renamePropertyObjectType->getClassName();
$classLikeNameObjectType = new ObjectType($classLikeName);
$classNameObjectType = new ObjectType($className);
$isSuperType = $classNameObjectType->isSuperTypeOf($classLikeNameObjectType)
->yes();
if ($classLikeName !== $className && ! $isSuperType) {
return;
}
$property = $classLike->getProperty($renameProperty->getOldProperty());
if (! $property instanceof Property) {
return;
}
$newProperty = $renameProperty->getNewProperty();
$targetNewProperty = $classLike->getProperty($newProperty);
if ($targetNewProperty instanceof Property) {
return;
}
$this->hasChanged = true;
$property->props[0]->name = new VarLikeIdentifier($newProperty);
}
private function refactorPropertyFetch(PropertyFetch|StaticPropertyFetch $propertyFetch): null|PropertyFetch|StaticPropertyFetch
{
foreach ($this->renamedProperties as $renamedProperty) {
$oldProperty = $renamedProperty->getOldProperty();
if (! $this->isName($propertyFetch, $oldProperty)) {
continue;
}
$varPropertyFetch = $propertyFetch instanceof PropertyFetch ? $propertyFetch->var : $propertyFetch->class;
if (! $this->isObjectType($varPropertyFetch, $renamedProperty->getObjectType())) {
continue;
}
$propertyFetch->name = $propertyFetch instanceof PropertyFetch
? new Identifier($renamedProperty->getNewProperty())
: new VarLikeIdentifier($renamedProperty->getNewProperty());
return $propertyFetch;
}
return null;
}
}