|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Unambiguous\Rector\Expression; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\Assign; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\Node\Expr\New_; |
| 12 | +use PhpParser\Node\Expr\Variable; |
| 13 | +use PhpParser\Node\Name; |
| 14 | +use PhpParser\Node\Stmt\Expression; |
| 15 | +use PhpParser\Node\Stmt\Return_; |
| 16 | +use Rector\Naming\Naming\PropertyNaming; |
| 17 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 21 | + |
| 22 | +/** |
| 23 | + * @experimental since 2025-11 |
| 24 | + * |
| 25 | + * @see \Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\FluentSettersToStandaloneCallMethodRectorTest |
| 26 | + */ |
| 27 | +final class FluentSettersToStandaloneCallMethodRector extends AbstractRector |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private readonly PropertyNaming $propertyNaming |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function getRuleDefinition(): RuleDefinition |
| 35 | + { |
| 36 | + return new RuleDefinition( |
| 37 | + 'Change fluent setter chain calls, to standalone line of setters', |
| 38 | + [ |
| 39 | + new CodeSample( |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +class SomeClass |
| 42 | +{ |
| 43 | + public function run() |
| 44 | + { |
| 45 | + return (new SomeFluentClass()) |
| 46 | + ->setName('John') |
| 47 | + ->setAge(30); |
| 48 | + } |
| 49 | +} |
| 50 | +CODE_SAMPLE |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +class SomeClass |
| 54 | +{ |
| 55 | + public function run() |
| 56 | + { |
| 57 | + $someFluentClass = new SomeFluentClass(); |
| 58 | + $someFluentClass->setName('John'); |
| 59 | + $someFluentClass->setAge(30); |
| 60 | +
|
| 61 | + return $someFluentClass; |
| 62 | + } |
| 63 | +} |
| 64 | +CODE_SAMPLE |
| 65 | + ), |
| 66 | + ] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return array<class-string<Node>> |
| 72 | + */ |
| 73 | + public function getNodeTypes(): array |
| 74 | + { |
| 75 | + return [Expression::class, Return_::class]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param Expression|Return_ $node |
| 80 | + */ |
| 81 | + public function refactor(Node $node): ?array |
| 82 | + { |
| 83 | + if (! $node->expr instanceof MethodCall) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + $firstMethodCall = $node->expr; |
| 88 | + |
| 89 | + // must be nested method call, so we avoid only single one |
| 90 | + if (! $firstMethodCall->var instanceof MethodCall) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + /** @var MethodCall[] $methodCalls */ |
| 95 | + $methodCalls = []; |
| 96 | + |
| 97 | + $currentMethodCall = $firstMethodCall; |
| 98 | + while ($currentMethodCall instanceof MethodCall) { |
| 99 | + $methodCalls[] = $currentMethodCall; |
| 100 | + $currentMethodCall = $currentMethodCall->var; |
| 101 | + } |
| 102 | + |
| 103 | + // at least 2 method calls |
| 104 | + if (count($methodCalls) < 1) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $rootExpr = $currentMethodCall; |
| 109 | + |
| 110 | + $variableName = $this->resolveVariableName($rootExpr); |
| 111 | + $someVariable = new Variable($variableName); |
| 112 | + |
| 113 | + $firstAssign = new Assign($someVariable, $rootExpr); |
| 114 | + $stmts = [new Expression($firstAssign)]; |
| 115 | + |
| 116 | + foreach ($methodCalls as $methodCall) { |
| 117 | + $methodCall->var = $someVariable; |
| 118 | + // inlines indent and removes () around first expr |
| 119 | + $methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null); |
| 120 | + $stmts[] = new Expression($methodCall); |
| 121 | + } |
| 122 | + |
| 123 | + $node->expr = $someVariable; |
| 124 | + |
| 125 | + return $stmts; |
| 126 | + } |
| 127 | + |
| 128 | + private function resolveVariableName(Expr $expr): string |
| 129 | + { |
| 130 | + if (! $expr instanceof New_) { |
| 131 | + return 'someVariable'; |
| 132 | + } |
| 133 | + |
| 134 | + if ($expr->class instanceof Name) { |
| 135 | + return $this->propertyNaming->fqnToVariableName($expr->class); |
| 136 | + } |
| 137 | + |
| 138 | + return 'someVariable'; |
| 139 | + } |
| 140 | +} |
0 commit comments