44
55namespace Rector \Php85 \Rector \StmtsAwareInterface ;
66
7+ use PhpParser \Node \Expr \BinaryOp \Pipe ;
78use PhpParser \Node ;
89use PhpParser \Node \Arg ;
910use PhpParser \Node \Expr ;
@@ -90,17 +91,17 @@ public function refactor(Node $node): ?Node
9091 return $ hasChanged ? $ node : null ;
9192 }
9293
93- private function transformSequentialAssignments (StmtsAwareInterface $ node ): bool
94+ private function transformSequentialAssignments (StmtsAwareInterface $ stmtsAware ): bool
9495 {
9596 $ hasChanged = false ;
96- $ statements = $ node ->stmts ;
97+ $ statements = $ stmtsAware ->stmts ;
9798 $ totalStatements = count ($ statements ) - 1 ;
9899
99100 for ($ i = 0 ; $ i < $ totalStatements ; ++$ i ) {
100101 $ chain = $ this ->findAssignmentChain ($ statements , $ i );
101102
102103 if ($ chain && count ($ chain ) >= 2 ) {
103- $ this ->processAssignmentChain ($ node , $ chain , $ i );
104+ $ this ->processAssignmentChain ($ stmtsAware , $ chain , $ i );
104105 $ hasChanged = true ;
105106 // Skip processed statements
106107 $ i += count ($ chain ) - 1 ;
@@ -153,6 +154,7 @@ private function findAssignmentChain(array $statements, int $startIndex): ?array
153154 if (! $ arg ->value instanceof Variable && ! $ this ->isSimpleValue ($ arg ->value )) {
154155 return null ;
155156 }
157+
156158 $ chain [] = [
157159 'stmt ' => $ stmt ,
158160 'assign ' => $ expr ,
@@ -166,6 +168,7 @@ private function findAssignmentChain(array $statements, int $startIndex): ?array
166168 if (! $ arg ->value instanceof Variable || $ this ->getName ($ arg ->value ) !== $ previousVarName ) {
167169 break ;
168170 }
171+
169172 $ chain [] = [
170173 'stmt ' => $ stmt ,
171174 'assign ' => $ expr ,
@@ -192,9 +195,8 @@ private function isSimpleValue(Expr $expr): bool
192195 /**
193196 * @param array<int, array{stmt: Stmt, assign: Expr, funcCall: Expr\FuncCall}> $chain
194197 */
195- private function processAssignmentChain (StmtsAwareInterface $ node , array $ chain , int $ startIndex ): void
198+ private function processAssignmentChain (StmtsAwareInterface $ stmtsAware , array $ chain , int $ startIndex ): void
196199 {
197- $ firstAssignment = $ chain [0 ]['assign ' ];
198200 $ lastAssignment = $ chain [count ($ chain ) - 1 ]['assign ' ];
199201
200202 // Get the initial value from the first function call's argument
@@ -217,39 +219,40 @@ private function processAssignmentChain(StmtsAwareInterface $node, array $chain,
217219 foreach ($ chain as $ chainItem ) {
218220 $ funcCall = $ chainItem ['funcCall ' ];
219221 $ placeholderCall = $ this ->createPlaceholderCall ($ funcCall );
220- $ pipeExpression = new Node \ Expr \ BinaryOp \ Pipe ($ pipeExpression , $ placeholderCall );
222+ $ pipeExpression = new Pipe ($ pipeExpression , $ placeholderCall );
221223 }
222224
223225 if (! $ lastAssignment instanceof Assign) {
224226 return ;
225227 }
228+
226229 // Create the final assignment
227- $ finalAssignment = new Assign ($ lastAssignment ->var , $ pipeExpression );
228- $ finalExpression = new Expression ($ finalAssignment );
230+ $ assign = new Assign ($ lastAssignment ->var , $ pipeExpression );
231+ $ finalExpression = new Expression ($ assign );
229232
230233 // Replace the statements
231234 $ endIndex = $ startIndex + count ($ chain ) - 1 ;
232235
233236 // Remove all intermediate statements and replace with the final pipe expression
234237 for ($ i = $ startIndex ; $ i <= $ endIndex ; ++$ i ) {
235238 if ($ i === $ startIndex ) {
236- $ node ->stmts [$ i ] = $ finalExpression ;
239+ $ stmtsAware ->stmts [$ i ] = $ finalExpression ;
237240 } else {
238- unset($ node ->stmts [$ i ]);
241+ unset($ stmtsAware ->stmts [$ i ]);
239242 }
240243 }
241244
242- $ stmts = array_values ($ node ->stmts );
245+ $ stmts = array_values ($ stmtsAware ->stmts );
243246
244247 // Reindex the array
245- $ node ->stmts = $ stmts ;
248+ $ stmtsAware ->stmts = $ stmts ;
246249 }
247250
248- private function transformNestedCalls (StmtsAwareInterface $ node ): bool
251+ private function transformNestedCalls (StmtsAwareInterface $ stmtsAware ): bool
249252 {
250253 $ hasChanged = false ;
251254
252- foreach ($ node ->stmts as $ stmt ) {
255+ foreach ($ stmtsAware ->stmts as $ stmt ) {
253256 if (! $ stmt instanceof Expression) {
254257 continue ;
255258 }
@@ -260,14 +263,14 @@ private function transformNestedCalls(StmtsAwareInterface $node): bool
260263 $ assignedValue = $ expr ->expr ;
261264 $ processedValue = $ this ->processNestedCalls ($ assignedValue );
262265
263- if ($ processedValue !== null && $ processedValue !== $ assignedValue ) {
266+ if ($ processedValue instanceof Expr && $ processedValue !== $ assignedValue ) {
264267 $ expr ->expr = $ processedValue ;
265268 $ hasChanged = true ;
266269 }
267270 } elseif ($ expr instanceof FuncCall) {
268271 $ processedValue = $ this ->processNestedCalls ($ expr );
269272
270- if ($ processedValue !== null && $ processedValue !== $ expr ) {
273+ if ($ processedValue instanceof Expr && $ processedValue !== $ expr ) {
271274 $ stmt ->expr = $ processedValue ;
272275 $ hasChanged = true ;
273276 }
@@ -288,6 +291,7 @@ private function processNestedCalls(Node $node): ?Expr
288291 if (! $ arg instanceof Arg) {
289292 return null ;
290293 }
294+
291295 if ($ arg ->value instanceof FuncCall) {
292296 return $ this ->buildPipeExpression ($ node , $ arg ->value );
293297 }
@@ -296,20 +300,18 @@ private function processNestedCalls(Node $node): ?Expr
296300 return null ;
297301 }
298302
299- private function buildPipeExpression (FuncCall $ outerCall , FuncCall $ innerCall ): Node \ Expr \ BinaryOp \ Pipe
303+ private function buildPipeExpression (FuncCall $ outerCall , FuncCall $ innerCall ): Pipe
300304 {
301- $ pipe = new Node \Expr \BinaryOp \Pipe ($ innerCall , $ this ->createPlaceholderCall ($ outerCall ));
302-
303- return $ pipe ;
305+ return new Pipe ($ innerCall , $ this ->createPlaceholderCall ($ outerCall ));
304306 }
305307
306- private function createPlaceholderCall (FuncCall $ originalCall ): FuncCall
308+ private function createPlaceholderCall (FuncCall $ funcCall ): FuncCall
307309 {
308310 $ newArgs = [];
309- foreach ($ originalCall ->args as $ arg ) {
311+ foreach ($ funcCall ->args as $ arg ) {
310312 $ newArgs [] = new VariadicPlaceholder ();
311313 }
312314
313- return new FuncCall ($ originalCall ->name , $ newArgs );
315+ return new FuncCall ($ funcCall ->name , $ newArgs );
314316 }
315317}
0 commit comments