From 92af5a38c51fd14a8742197a6e258ee2b74e5d6f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 23 Nov 2026 14:01:39 +0100 Subject: [PATCH] [Php72] Skip while each() loop without reset pointer in WhileEachToForeachRector --- .../Fixture/dont_remove_comment.php.inc | 4 + .../Fixture/fixture.php.inc | 6 +- .../Fixture/include_suppress.php.inc | 2 + .../Fixture/reset_inside_nested_block.php.inc | 37 ++++ .../skip_assigned_from_variable.php.inc | 15 ++ .../Fixture/skip_by_reference_param.php.inc | 15 ++ .../Fixture/skip_no_reset_before.php.inc | 14 ++ .../Fixture/skip_pointer_moved_before.php.inc | 15 ++ .../skip_pointer_moved_in_loop.php.inc | 16 ++ .../Fixture/skip_pointer_used_after.php.inc | 16 ++ .../Fixture/skip_second_loop.php.inc | 19 ++ .../Fixture/trailing_comma_last.php.inc | 8 +- .../While_/WhileEachToForeachRector.php | 203 +++++++++++++++++- 13 files changed, 359 insertions(+), 11 deletions(-) create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/reset_inside_nested_block.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_assigned_from_variable.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_by_reference_param.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_no_reset_before.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_pointer_moved_before.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_pointer_moved_in_loop.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_pointer_used_after.php.inc create mode 100644 rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_second_loop.php.inc diff --git a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/dont_remove_comment.php.inc b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/dont_remove_comment.php.inc index b7af2d38376..cbacf85a5f5 100644 --- a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/dont_remove_comment.php.inc +++ b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/dont_remove_comment.php.inc @@ -4,6 +4,8 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture; function dontremovecomment() { + reset($callbacks); + // a comment before while (list($key, $callback) = each($callbacks)) { // comment inside @@ -18,6 +20,8 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture; function dontremovecomment() { + reset($callbacks); + // a comment before foreach ($callbacks as $key => $callback) { // comment inside diff --git a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/fixture.php.inc b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/fixture.php.inc index 6d9b8e92fab..051c6c44c65 100644 --- a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/fixture.php.inc +++ b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/fixture.php.inc @@ -2,8 +2,9 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture; -function each1() +function each1(array $callbacks) { + reset($callbacks); while (list($key, $callback) = each($callbacks)) { // comment } @@ -21,8 +22,9 @@ function each1() namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture; -function each1() +function each1(array $callbacks) { + reset($callbacks); foreach ($callbacks as $key => $callback) { // comment } diff --git a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/include_suppress.php.inc b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/include_suppress.php.inc index 8965afe0810..1778e690716 100644 --- a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/include_suppress.php.inc +++ b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/include_suppress.php.inc @@ -6,6 +6,7 @@ final class IncludeSuppress { public function run(array $items) { + reset($items); while (list(, $item) = @each($items)) { // some code } @@ -22,6 +23,7 @@ final class IncludeSuppress { public function run(array $items) { + reset($items); foreach ($items as $item) { // some code } diff --git a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/reset_inside_nested_block.php.inc b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/reset_inside_nested_block.php.inc new file mode 100644 index 00000000000..cb1bf57ddb0 --- /dev/null +++ b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/reset_inside_nested_block.php.inc @@ -0,0 +1,37 @@ + +----- + $item) { + echo $key . $item; + } + } + } +} + +?> diff --git a/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_assigned_from_variable.php.inc b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_assigned_from_variable.php.inc new file mode 100644 index 00000000000..7b006789b57 --- /dev/null +++ b/rules-tests/Php72/Rector/While_/WhileEachToForeachRector/Fixture/skip_assigned_from_variable.php.inc @@ -0,0 +1,15 @@ + $callback) { // ... } @@ -52,12 +75,14 @@ public function getRuleDefinition(): RuleDefinition ), new CodeSample( <<<'CODE_SAMPLE' +reset($callbacks); while (list($key) = each($callbacks)) { // ... } CODE_SAMPLE , <<<'CODE_SAMPLE' +reset($callbacks); foreach (array_keys($callbacks) as $key) { // ... } @@ -72,19 +97,75 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [While_::class]; + return [FileNode::class, Namespace_::class, ClassMethod::class, Function_::class, Closure::class]; } /** - * @param While_ $node + * @param FileNode|Namespace_|ClassMethod|Function_|Closure $node */ public function refactor(Node $node): ?Node { - if (! $node->cond instanceof Assign) { + if ($node->stmts === null) { + return null; + } + + if ($node instanceof FileNode && $node->isNamespaced()) { + // handled in Namespace_ + return null; + } + + $stmtsAwareNodes = [$node]; + $this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (&$stmtsAwareNodes): ?int { + if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + if (NodeGroup::isStmtAwareNode($subNode)) { + $stmtsAwareNodes[] = $subNode; + } + + return null; + }); + + $hasChanged = false; + // inner blocks first, so a replaced outer loop carries their changes + foreach (array_reverse($stmtsAwareNodes) as $stmtsAwareNode) { + Assert::propertyExists($stmtsAwareNode, 'stmts'); + if ($stmtsAwareNode->stmts === null) { + continue; + } + + $previousStmt = null; + foreach ($stmtsAwareNode->stmts as $key => $stmt) { + if ($stmt instanceof While_ && $previousStmt instanceof Stmt) { + $foreach = $this->refactorWhile($stmt, $previousStmt, $node); + if ($foreach instanceof Foreach_) { + $stmtsAwareNode->stmts[$key] = $foreach; + $hasChanged = true; + } + } + + $previousStmt = $stmt; + } + } + + if ($hasChanged) { + return $node; + } + + return null; + } + + private function refactorWhile( + While_ $while, + Stmt $previousStmt, + FileNode|Namespace_|ClassMethod|Function_|Closure $scopeNode + ): ?Foreach_ { + if (! $while->cond instanceof Assign) { return null; } - $listAndEach = $this->assignManipulator->matchListAndEach($node->cond); + $listAndEach = $this->assignManipulator->matchListAndEach($while->cond); if (! $listAndEach instanceof ListAndEach) { return null; } @@ -98,6 +179,11 @@ public function refactor(Node $node): ?Node $firstArg = $eachFuncCall->getArgs()[0]; + // each() continues from the internal pointer, foreach always starts at the first item + if (! $this->isPointerAtStart($firstArg->value, $previousStmt, $scopeNode, $eachFuncCall)) { + return null; + } + $foreachedExpr = count($list->items) === 1 ? $this->nodeFactory->createFuncCall( 'array_keys', [$firstArg] @@ -115,10 +201,10 @@ public function refactor(Node $node): ?Node } $foreach = new Foreach_($foreachedExpr, $arrayItem->value, [ - 'stmts' => $node->stmts, + 'stmts' => $while->stmts, ]); - $this->mirrorComments($foreach, $node); + $this->mirrorComments($foreach, $while); // is key included? add it to foreach if ($list->items !== []) { @@ -132,4 +218,107 @@ public function refactor(Node $node): ?Node return $foreach; } + + private function isPointerAtStart( + Expr $expr, + Stmt $previousStmt, + FileNode|Namespace_|ClassMethod|Function_|Closure $scopeNode, + FuncCall $eachFuncCall + ): bool { + if (! $expr instanceof Variable || ! is_string($expr->name)) { + return false; + } + + $variableName = $expr->name; + if (! $previousStmt instanceof Expression) { + return false; + } + + $resetFuncCall = $this->matchResetFuncCall($previousStmt->expr, $variableName); + if (! $resetFuncCall instanceof FuncCall && ! $this->isArrayLiteralAssign($previousStmt->expr, $variableName)) { + return false; + } + + // the caller sees the pointer of a by-reference variable + if (! $scopeNode instanceof FileNode && ! $scopeNode instanceof Namespace_) { + foreach ($scopeNode->params as $param) { + if ($param->byRef && $this->isName($param->var, $variableName)) { + return false; + } + } + } + + if ($scopeNode instanceof Closure) { + foreach ($scopeNode->uses as $closureUse) { + if ($closureUse->byRef && $this->isName($closureUse->var, $variableName)) { + return false; + } + } + } + + // any other pointer call on the variable would see a different position + $hasOtherPointerCall = false; + $this->traverseNodesWithCallable((array) $scopeNode->stmts, function (Node $node) use ( + $variableName, + $resetFuncCall, + $eachFuncCall, + &$hasOtherPointerCall + ): ?int { + if ($node instanceof Class_ || $node instanceof Function_) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + if (! $node instanceof FuncCall || $node === $resetFuncCall || $node === $eachFuncCall) { + return null; + } + + if (! $this->isPointerFuncCallOnVariable($node, self::POINTER_FUNCTION_NAMES, $variableName)) { + return null; + } + + $hasOtherPointerCall = true; + return NodeVisitor::STOP_TRAVERSAL; + }); + + return ! $hasOtherPointerCall; + } + + private function matchResetFuncCall(Expr $expr, string $variableName): ?FuncCall + { + if (! $expr instanceof FuncCall) { + return null; + } + + if (! $this->isPointerFuncCallOnVariable($expr, ['reset'], $variableName)) { + return null; + } + + return $expr; + } + + private function isArrayLiteralAssign(Expr $expr, string $variableName): bool + { + if (! $expr instanceof Assign || ! $expr->expr instanceof Array_) { + return false; + } + + return $expr->var instanceof Variable && $this->isName($expr->var, $variableName); + } + + /** + * @param string[] $functionNames + */ + private function isPointerFuncCallOnVariable(FuncCall $funcCall, array $functionNames, string $variableName): bool + { + if (! $this->isNames($funcCall, $functionNames) || $funcCall->isFirstClassCallable()) { + return false; + } + + $firstArg = $funcCall->getArgs()[0] ?? null; + if (! $firstArg instanceof Arg || ! $firstArg->value instanceof Variable) { + return false; + } + + return $this->isName($firstArg->value, $variableName); + } }