forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInArgFluentChainMethodCallToStandaloneMethodCallRector.php
184 lines (154 loc) · 5.82 KB
/
InArgFluentChainMethodCallToStandaloneMethodCallRector.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
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace Rector\Defluent\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use Rector\Core\Rector\AbstractRector;
use Rector\Defluent\Matcher\AssignAndRootExprAndNodesToAddMatcher;
use Rector\Defluent\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer;
use Rector\Defluent\NodeAnalyzer\NewFluentChainMethodCallNodeAnalyzer;
use Rector\Defluent\NodeFactory\FluentMethodCallAsArgFactory;
use Rector\Defluent\NodeFactory\NonFluentChainMethodCallFactory;
use Rector\Defluent\NodeFactory\VariableFromNewFactory;
use Rector\Defluent\ValueObject\AssignAndRootExprAndNodesToAdd;
use Rector\Defluent\ValueObject\FluentCallsKind;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Defluent\Rector\MethodCall\InArgChainFluentMethodCallToStandaloneMethodCallRectorTest\InArgChainFluentMethodCallToStandaloneMethodCallRectorTest
*/
final class InArgFluentChainMethodCallToStandaloneMethodCallRector extends AbstractRector
{
/**
* @var NewFluentChainMethodCallNodeAnalyzer
*/
private $newFluentChainMethodCallNodeAnalyzer;
/**
* @var VariableFromNewFactory
*/
private $variableFromNewFactory;
/**
* @var FluentMethodCallAsArgFactory
*/
private $fluentMethodCallAsArgFactory;
/**
* @var AssignAndRootExprAndNodesToAddMatcher
*/
private $assignAndRootExprAndNodesToAddMatcher;
/**
* @var FluentChainMethodCallNodeAnalyzer
*/
private $fluentChainMethodCallNodeAnalyzer;
/**
* @var NonFluentChainMethodCallFactory
*/
private $nonFluentChainMethodCallFactory;
public function __construct(
NewFluentChainMethodCallNodeAnalyzer $newFluentChainMethodCallNodeAnalyzer,
VariableFromNewFactory $variableFromNewFactory,
FluentMethodCallAsArgFactory $fluentMethodCallAsArgFactory,
AssignAndRootExprAndNodesToAddMatcher $assignAndRootExprAndNodesToAddMatcher,
FluentChainMethodCallNodeAnalyzer $fluentChainMethodCallNodeAnalyzer,
NonFluentChainMethodCallFactory $nonFluentChainMethodCallFactory
) {
$this->newFluentChainMethodCallNodeAnalyzer = $newFluentChainMethodCallNodeAnalyzer;
$this->variableFromNewFactory = $variableFromNewFactory;
$this->fluentMethodCallAsArgFactory = $fluentMethodCallAsArgFactory;
$this->assignAndRootExprAndNodesToAddMatcher = $assignAndRootExprAndNodesToAddMatcher;
$this->fluentChainMethodCallNodeAnalyzer = $fluentChainMethodCallNodeAnalyzer;
$this->nonFluentChainMethodCallFactory = $nonFluentChainMethodCallFactory;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turns fluent interface calls to classic ones.',
[
new CodeSample(<<<'CODE_SAMPLE'
class UsedAsParameter
{
public function someFunction(FluentClass $someClass)
{
$this->processFluentClass($someClass->someFunction()->otherFunction());
}
public function processFluentClass(FluentClass $someClass)
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class UsedAsParameter
{
public function someFunction(FluentClass $someClass)
{
$someClass->someFunction();
$someClass->otherFunction();
$this->processFluentClass($someClass);
}
public function processFluentClass(FluentClass $someClass)
{
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Arg) {
return null;
}
$parentMethodCall = $parent->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentMethodCall instanceof MethodCall) {
return null;
}
if (! $this->fluentChainMethodCallNodeAnalyzer->isLastChainMethodCall($node)) {
return null;
}
// create instances from (new ...)->call, re-use from
if ($node->var instanceof New_) {
$this->refactorNew($node, $node->var);
return null;
}
$assignAndRootExprAndNodesToAdd = $this->assignAndRootExprAndNodesToAddMatcher->match(
$node, FluentCallsKind::IN_ARGS
);
if (! $assignAndRootExprAndNodesToAdd instanceof AssignAndRootExprAndNodesToAdd) {
return null;
}
$this->addNodesBeforeNode($assignAndRootExprAndNodesToAdd->getNodesToAdd(), $node);
return $assignAndRootExprAndNodesToAdd->getRootCallerExpr();
}
private function refactorNew(MethodCall $methodCall, New_ $new): void
{
if (! $this->newFluentChainMethodCallNodeAnalyzer->isNewMethodCallReturningSelf($methodCall)) {
return;
}
$nodesToAdd = $this->nonFluentChainMethodCallFactory->createFromNewAndRootMethodCall($new, $methodCall);
$newVariable = $this->variableFromNewFactory->create($new);
$nodesToAdd[] = $this->fluentMethodCallAsArgFactory->createFluentAsArg($methodCall, $newVariable);
$this->addNodesBeforeNode($nodesToAdd, $methodCall);
$this->removeParentParent($methodCall);
}
private function removeParentParent(MethodCall $methodCall): void
{
/** @var Arg $parent */
$parent = $methodCall->getAttribute(AttributeKey::PARENT_NODE);
/** @var MethodCall $parentParent */
$parentParent = $parent->getAttribute(AttributeKey::PARENT_NODE);
$this->removeNode($parentParent);
}
}