forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyFetchToMethodCallRector.php
175 lines (147 loc) · 5.26 KB
/
PropertyFetchToMethodCallRector.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\PropertyFetchToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\Assign\PropertyFetchToMethodCallRector\PropertyFetchToMethodCallRectorTest
*/
final class PropertyFetchToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const PROPERTIES_TO_METHOD_CALLS = 'properties_to_method_calls';
/**
* @var PropertyFetchToMethodCall[]
*/
private $propertiesToMethodCalls = [];
public function getRuleDefinition(): RuleDefinition
{
$firstConfiguration = [
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyFetchToMethodCall('SomeObject', 'property', 'getProperty', 'setProperty'),
],
];
$secondConfiguration = [
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyFetchToMethodCall('SomeObject', 'property', 'getConfig', null, ['someArg']),
],
];
return new RuleDefinition('Replaces properties assign calls be defined methods.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$result = $object->property;
$object->property = $value;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$result = $object->getProperty();
$object->setProperty($value);
CODE_SAMPLE
,
$firstConfiguration
),
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$result = $object->property;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$result = $object->getProperty('someArg');
CODE_SAMPLE
,
$secondConfiguration
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Assign::class];
}
/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if ($node->var instanceof PropertyFetch) {
return $this->processSetter($node);
}
if ($node->expr instanceof PropertyFetch) {
return $this->processGetter($node);
}
return null;
}
/**
* @param array<string, PropertyFetchToMethodCall[]> $configuration
*/
public function configure(array $configuration): void
{
$propertiesToMethodCalls = $configuration[self::PROPERTIES_TO_METHOD_CALLS] ?? [];
Assert::allIsInstanceOf($propertiesToMethodCalls, PropertyFetchToMethodCall::class);
$this->propertiesToMethodCalls = $propertiesToMethodCalls;
}
private function processSetter(Assign $assign): ?Node
{
/** @var PropertyFetch $propertyFetchNode */
$propertyFetchNode = $assign->var;
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
if (! $propertyToMethodCall instanceof PropertyFetchToMethodCall) {
return null;
}
if ($propertyToMethodCall->getNewSetMethod() === null) {
throw new ShouldNotHappenException();
}
$args = $this->nodeFactory->createArgs([$assign->expr]);
/** @var Variable $variable */
$variable = $propertyFetchNode->var;
return $this->nodeFactory->createMethodCall($variable, $propertyToMethodCall->getNewSetMethod(), $args);
}
private function processGetter(Assign $assign): ?Node
{
/** @var PropertyFetch $propertyFetchNode */
$propertyFetchNode = $assign->expr;
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
if (! $propertyToMethodCall instanceof PropertyFetchToMethodCall) {
return null;
}
// simple method name
if ($propertyToMethodCall->getNewGetMethod() !== '') {
$assign->expr = $this->nodeFactory->createMethodCall(
$propertyFetchNode->var,
$propertyToMethodCall->getNewGetMethod()
);
if ($propertyToMethodCall->getNewGetArguments() !== []) {
$args = $this->nodeFactory->createArgs($propertyToMethodCall->getNewGetArguments());
$assign->expr->args = $args;
}
return $assign;
}
return $assign;
}
private function matchPropertyFetchCandidate(PropertyFetch $propertyFetch): ?PropertyFetchToMethodCall
{
foreach ($this->propertiesToMethodCalls as $propertyToMethodCall) {
if (! $this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldObjectType())) {
continue;
}
if (! $this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) {
continue;
}
return $propertyToMethodCall;
}
return null;
}
}