|
11 | 11 | use PhpParser\Node\Expr\New_; |
12 | 12 | use PhpParser\Node\Expr\Variable; |
13 | 13 | use PhpParser\Node\Name; |
| 14 | +use PhpParser\Node\Stmt; |
14 | 15 | use PhpParser\Node\Stmt\Expression; |
15 | 16 | use PhpParser\Node\Stmt\Return_; |
16 | 17 | use PHPStan\Reflection\ClassReflection; |
@@ -113,35 +114,19 @@ public function refactor(Node $node): ?array |
113 | 114 | } |
114 | 115 |
|
115 | 116 | $rootExpr = $currentMethodCall; |
| 117 | + if (! $rootExpr instanceof New_) { |
| 118 | + return null; |
| 119 | + } |
116 | 120 |
|
117 | 121 | if ($this->shouldSkipForVendorOrInternal($firstMethodCall)) { |
118 | 122 | return null; |
119 | 123 | } |
120 | 124 |
|
121 | 125 | $variableName = $this->resolveVariableName($rootExpr); |
122 | 126 | $someVariable = new Variable($variableName); |
123 | | - |
124 | 127 | $firstAssign = new Assign($someVariable, $rootExpr); |
125 | | - $stmts = [new Expression($firstAssign)]; |
126 | | - |
127 | | - // revert to normal order |
128 | | - $methodCalls = array_reverse($methodCalls); |
129 | 128 |
|
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); |
145 | 130 | } |
146 | 131 |
|
147 | 132 | private function resolveVariableName(Expr $expr): string |
@@ -174,4 +159,36 @@ private function shouldSkipForVendorOrInternal(MethodCall $firstMethodCall): boo |
174 | 159 |
|
175 | 160 | return false; |
176 | 161 | } |
| 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 | + } |
177 | 194 | } |
0 commit comments