Skip to content

Commit 035d2f2

Browse files
committed
only handle new instances
1 parent 32f3140 commit 035d2f2

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector/Source/SomeSetterClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source;
44

5-
class SomeSetterClass
5+
final class SomeSetterClass
66
{
77
private ?string $name = null;
88

rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Expr\New_;
1212
use PhpParser\Node\Expr\Variable;
1313
use PhpParser\Node\Name;
14+
use PhpParser\Node\Stmt;
1415
use PhpParser\Node\Stmt\Expression;
1516
use PhpParser\Node\Stmt\Return_;
1617
use PHPStan\Reflection\ClassReflection;
@@ -113,35 +114,19 @@ public function refactor(Node $node): ?array
113114
}
114115

115116
$rootExpr = $currentMethodCall;
117+
if (! $rootExpr instanceof New_) {
118+
return null;
119+
}
116120

117121
if ($this->shouldSkipForVendorOrInternal($firstMethodCall)) {
118122
return null;
119123
}
120124

121125
$variableName = $this->resolveVariableName($rootExpr);
122126
$someVariable = new Variable($variableName);
123-
124127
$firstAssign = new Assign($someVariable, $rootExpr);
125-
$stmts = [new Expression($firstAssign)];
126-
127-
// revert to normal order
128-
$methodCalls = array_reverse($methodCalls);
129128

130-
foreach ($methodCalls as $methodCall) {
131-
$methodCall->var = $someVariable;
132-
// inlines indent and removes () around first expr
133-
$methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null);
134-
$stmts[] = new Expression($methodCall);
135-
}
136-
137-
if ($node instanceof Return_) {
138-
$node->expr = $someVariable;
139-
$stmts[] = $node;
140-
}
141-
142-
$node->expr = $someVariable;
143-
144-
return $stmts;
129+
return $this->createStmts($firstAssign, $methodCalls, $someVariable, $node);
145130
}
146131

147132
private function resolveVariableName(Expr $expr): string
@@ -174,4 +159,36 @@ private function shouldSkipForVendorOrInternal(MethodCall $firstMethodCall): boo
174159

175160
return false;
176161
}
162+
163+
/**
164+
* @param MethodCall[] $methodCalls
165+
* @return Stmt[]
166+
*/
167+
private function createStmts(
168+
Assign $firstAssign,
169+
array $methodCalls,
170+
Variable $someVariable,
171+
Expression|Return_ $firstStmt
172+
): array {
173+
$stmts = [new Expression($firstAssign)];
174+
175+
// revert to normal order
176+
$methodCalls = array_reverse($methodCalls);
177+
178+
foreach ($methodCalls as $methodCall) {
179+
$methodCall->var = $someVariable;
180+
// inlines indent and removes () around first expr
181+
$methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null);
182+
$stmts[] = new Expression($methodCall);
183+
}
184+
185+
if ($firstStmt instanceof Return_) {
186+
$firstStmt->expr = $someVariable;
187+
$stmts[] = $firstStmt;
188+
}
189+
190+
$firstStmt->expr = $someVariable;
191+
192+
return $stmts;
193+
}
177194
}

0 commit comments

Comments
 (0)