|
19 | 19 | use Rector\Naming\Naming\PropertyNaming; |
20 | 20 | use Rector\NodeTypeResolver\Node\AttributeKey; |
21 | 21 | use Rector\Rector\AbstractRector; |
| 22 | +use Rector\Unambiguous\NodeAnalyzer\FluentMethodCallsCollector; |
22 | 23 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
23 | 24 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
24 | 25 |
|
|
30 | 31 | final class FluentSettersToStandaloneCallMethodRector extends AbstractRector |
31 | 32 | { |
32 | 33 | public function __construct( |
33 | | - private readonly PropertyNaming $propertyNaming |
| 34 | + private readonly PropertyNaming $propertyNaming, |
| 35 | + private readonly FluentMethodCallsCollector $fluentMethodCallsCollector |
34 | 36 | ) { |
35 | 37 | } |
36 | 38 |
|
@@ -87,54 +89,21 @@ public function refactor(Node $node): ?array |
87 | 89 | return null; |
88 | 90 | } |
89 | 91 |
|
90 | | - $firstMethodCall = $node->expr; |
91 | | - |
92 | | - // must be nested method call, so we avoid only single one |
93 | | - if (! $firstMethodCall->var instanceof MethodCall) { |
94 | | - return null; |
95 | | - } |
96 | | - |
97 | | - /** @var MethodCall[] $methodCalls */ |
98 | | - $methodCalls = []; |
99 | | - |
100 | | - $currentMethodCall = $firstMethodCall; |
101 | | - $classNameObjectType = null; |
102 | | - while ($currentMethodCall instanceof MethodCall) { |
103 | | - if ($currentMethodCall->isFirstClassCallable()) { |
104 | | - return null; |
105 | | - } |
106 | | - |
107 | | - // must be exactly one argument |
108 | | - if (count($currentMethodCall->getArgs()) !== 1) { |
109 | | - return null; |
110 | | - } |
111 | | - |
112 | | - $objectType = $this->getType($currentMethodCall->var); |
113 | | - if (! $objectType instanceof ObjectType) { |
114 | | - return null; |
115 | | - } |
116 | | - |
117 | | - if ($classNameObjectType === null) { |
118 | | - $classNameObjectType = $objectType->getClassName(); |
119 | | - } elseif ($classNameObjectType !== $objectType->getClassName()) { |
120 | | - return null; |
121 | | - } |
122 | | - |
123 | | - $methodCalls[] = $currentMethodCall; |
124 | | - $currentMethodCall = $currentMethodCall->var; |
125 | | - } |
| 92 | + $methodCalls = $this->fluentMethodCallsCollector->resolve($node->expr); |
126 | 93 |
|
127 | 94 | // at least 2 method calls |
128 | 95 | if (count($methodCalls) < 1) { |
129 | 96 | return null; |
130 | 97 | } |
131 | 98 |
|
132 | | - $rootExpr = $currentMethodCall; |
| 99 | + $lastMethodCall = end($methodCalls); |
| 100 | + $rootExpr = $lastMethodCall->var; |
| 101 | + |
133 | 102 | if (! $rootExpr instanceof New_) { |
134 | 103 | return null; |
135 | 104 | } |
136 | 105 |
|
137 | | - if ($this->shouldSkipForVendorOrInternal($firstMethodCall)) { |
| 106 | + if ($this->shouldSkipForVendorOrInternal($node->expr)) { |
138 | 107 | return null; |
139 | 108 | } |
140 | 109 |
|
|
0 commit comments