3434final class NestedToPipeOperatorRector extends AbstractRector implements MinPhpVersionInterface
3535{
3636 public function __construct (
37- private ExprAnalyzer $ exprAnalyzer
37+ private readonly ExprAnalyzer $ exprAnalyzer
3838 ) {
3939 }
4040
@@ -164,6 +164,7 @@ private function findAssignmentChain(array $statements, int $startIndex): ?array
164164 if (! $ arg ->value instanceof Variable && ! $ this ->isSimpleValue ($ arg ->value )) {
165165 return null ;
166166 }
167+
167168 $ chain [] = [
168169 'stmt ' => $ stmt ,
169170 'assign ' => $ expr ,
@@ -177,6 +178,7 @@ private function findAssignmentChain(array $statements, int $startIndex): ?array
177178 if (! $ arg ->value instanceof Variable || $ this ->getName ($ arg ->value ) !== $ previousVarName ) {
178179 break ;
179180 }
181+
180182 $ chain [] = [
181183 'stmt ' => $ stmt ,
182184 'assign ' => $ expr ,
@@ -196,8 +198,7 @@ private function isSimpleValue(Expr $expr): bool
196198 return ! $ this ->exprAnalyzer ->isDynamicExpr ($ expr );
197199 }
198200
199- return $ expr instanceof Variable
200- || $ expr instanceof ConstFetch
201+ return $ expr instanceof ConstFetch
201202 || $ expr instanceof String_
202203 || $ expr instanceof Float_
203204 || $ expr instanceof Int_;
@@ -206,9 +207,8 @@ private function isSimpleValue(Expr $expr): bool
206207 /**
207208 * @param array<int, array{stmt: Stmt, assign: Expr, funcCall: Expr\FuncCall}> $chain
208209 */
209- private function processAssignmentChain (StmtsAwareInterface $ node , array $ chain , int $ startIndex ): void
210+ private function processAssignmentChain (StmtsAwareInterface $ stmtsAware , array $ chain , int $ startIndex ): void
210211 {
211- $ firstAssignment = $ chain [0 ]['assign ' ];
212212 $ lastAssignment = $ chain [count ($ chain ) - 1 ]['assign ' ];
213213
214214 // Get the initial value from the first function call's argument
@@ -237,33 +237,34 @@ private function processAssignmentChain(StmtsAwareInterface $node, array $chain,
237237 if (! $ lastAssignment instanceof Assign) {
238238 return ;
239239 }
240+
240241 // Create the final assignment
241- $ finalAssignment = new Assign ($ lastAssignment ->var , $ pipeExpression );
242- $ finalExpression = new Expression ($ finalAssignment );
242+ $ assign = new Assign ($ lastAssignment ->var , $ pipeExpression );
243+ $ finalExpression = new Expression ($ assign );
243244
244245 // Replace the statements
245246 $ endIndex = $ startIndex + count ($ chain ) - 1 ;
246247
247248 // Remove all intermediate statements and replace with the final pipe expression
248249 for ($ i = $ startIndex ; $ i <= $ endIndex ; ++$ i ) {
249250 if ($ i === $ startIndex ) {
250- $ node ->stmts [$ i ] = $ finalExpression ;
251+ $ stmtsAware ->stmts [$ i ] = $ finalExpression ;
251252 } else {
252- unset($ node ->stmts [$ i ]);
253+ unset($ stmtsAware ->stmts [$ i ]);
253254 }
254255 }
255256
256- $ stmts = array_values ($ node ->stmts );
257+ $ stmts = array_values ($ stmtsAware ->stmts );
257258
258259 // Reindex the array
259- $ node ->stmts = $ stmts ;
260+ $ stmtsAware ->stmts = $ stmts ;
260261 }
261262
262- private function transformNestedCalls (StmtsAwareInterface $ node ): bool
263+ private function transformNestedCalls (StmtsAwareInterface $ stmtsAware ): bool
263264 {
264265 $ hasChanged = false ;
265266
266- foreach ($ node ->stmts as $ stmt ) {
267+ foreach ($ stmtsAware ->stmts as $ stmt ) {
267268 if (! $ stmt instanceof Expression) {
268269 continue ;
269270 }
@@ -274,14 +275,14 @@ private function transformNestedCalls(StmtsAwareInterface $node): bool
274275 $ assignedValue = $ expr ->expr ;
275276 $ processedValue = $ this ->processNestedCalls ($ assignedValue );
276277
277- if ($ processedValue !== null && $ processedValue !== $ assignedValue ) {
278+ if ($ processedValue instanceof Expr && $ processedValue !== $ assignedValue ) {
278279 $ expr ->expr = $ processedValue ;
279280 $ hasChanged = true ;
280281 }
281282 } elseif ($ expr instanceof FuncCall) {
282283 $ processedValue = $ this ->processNestedCalls ($ expr );
283284
284- if ($ processedValue !== null && $ processedValue !== $ expr ) {
285+ if ($ processedValue instanceof Expr && $ processedValue !== $ expr ) {
285286 $ stmt ->expr = $ processedValue ;
286287 $ hasChanged = true ;
287288 }
@@ -302,6 +303,7 @@ private function processNestedCalls(Node $node): ?Expr
302303 if (! $ arg instanceof Arg) {
303304 return null ;
304305 }
306+
305307 if ($ arg ->value instanceof FuncCall) {
306308 return $ this ->buildPipeExpression ($ node , $ arg ->value );
307309 }
@@ -315,8 +317,8 @@ private function buildPipeExpression(FuncCall $outerCall, FuncCall $innerCall):
315317 return new Pipe ($ innerCall , $ this ->createPlaceholderCall ($ outerCall ));
316318 }
317319
318- private function createPlaceholderCall (FuncCall $ originalCall ): FuncCall
320+ private function createPlaceholderCall (FuncCall $ funcCall ): FuncCall
319321 {
320- return new FuncCall ($ originalCall ->name , [new VariadicPlaceholder ()]);
322+ return new FuncCall ($ funcCall ->name , [new VariadicPlaceholder ()]);
321323 }
322324}
0 commit comments