diff --git a/rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector/Source/SomeSetterClass.php b/rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector/Source/SomeSetterClass.php index 36abfd51b98..7b0ff3a3afa 100644 --- a/rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector/Source/SomeSetterClass.php +++ b/rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector/Source/SomeSetterClass.php @@ -2,7 +2,7 @@ namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source; -class SomeSetterClass +final class SomeSetterClass { private ?string $name = null; diff --git a/rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php b/rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php index c00c7a1199e..f8f20a29626 100644 --- a/rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php +++ b/rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; +use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PHPStan\Reflection\ClassReflection; @@ -113,6 +114,9 @@ public function refactor(Node $node): ?array } $rootExpr = $currentMethodCall; + if (! $rootExpr instanceof New_) { + return null; + } if ($this->shouldSkipForVendorOrInternal($firstMethodCall)) { return null; @@ -120,28 +124,9 @@ public function refactor(Node $node): ?array $variableName = $this->resolveVariableName($rootExpr); $someVariable = new Variable($variableName); - $firstAssign = new Assign($someVariable, $rootExpr); - $stmts = [new Expression($firstAssign)]; - - // revert to normal order - $methodCalls = array_reverse($methodCalls); - foreach ($methodCalls as $methodCall) { - $methodCall->var = $someVariable; - // inlines indent and removes () around first expr - $methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null); - $stmts[] = new Expression($methodCall); - } - - if ($node instanceof Return_) { - $node->expr = $someVariable; - $stmts[] = $node; - } - - $node->expr = $someVariable; - - return $stmts; + return $this->createStmts($firstAssign, $methodCalls, $someVariable, $node); } private function resolveVariableName(Expr $expr): string @@ -174,4 +159,36 @@ private function shouldSkipForVendorOrInternal(MethodCall $firstMethodCall): boo return false; } + + /** + * @param MethodCall[] $methodCalls + * @return Stmt[] + */ + private function createStmts( + Assign $firstAssign, + array $methodCalls, + Variable $someVariable, + Expression|Return_ $firstStmt + ): array { + $stmts = [new Expression($firstAssign)]; + + // revert to normal order + $methodCalls = array_reverse($methodCalls); + + foreach ($methodCalls as $methodCall) { + $methodCall->var = $someVariable; + // inlines indent and removes () around first expr + $methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $stmts[] = new Expression($methodCall); + } + + if ($firstStmt instanceof Return_) { + $firstStmt->expr = $someVariable; + $stmts[] = $firstStmt; + } + + $firstStmt->expr = $someVariable; + + return $stmts; + } }