From d81082ecd8d00054ff566795401452a2ed4e367d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Mar 2024 13:06:07 +0700 Subject: [PATCH 1/3] [DowngradePhp73] Ensure reset() after go to end() array on DowngradeArrayKeyFirstLastRector --- .../Fixture/array_key_last.php.inc | 1 + .../Fixture/cast_array_key_last_value.php.inc | 1 + .../Fixture/double_cast.php.inc | 1 + .../Fixture/keep_inside_else.php.inc | 1 + .../Fixture/keep_inside_if.php.inc | 1 + .../Fixture/method_call_as_arg.php.inc | 1 + .../Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php | 4 ++++ 7 files changed, 10 insertions(+) diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/array_key_last.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/array_key_last.php.inc index 998780ec..c9daf691 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/array_key_last.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/array_key_last.php.inc @@ -22,6 +22,7 @@ class SomeClass { end($items); $lastItemKey = key($items); + reset($items); } } diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/cast_array_key_last_value.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/cast_array_key_last_value.php.inc index 427dc4f1..b9990edb 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/cast_array_key_last_value.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/cast_array_key_last_value.php.inc @@ -27,6 +27,7 @@ class CastArrayKeyLastValue $stmts = (array) $classMethod->stmts; end($stmts); $lastItemKey = key($stmts); + reset($stmts); } } diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/double_cast.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/double_cast.php.inc index ab6492fd..747779e3 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/double_cast.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/double_cast.php.inc @@ -27,6 +27,7 @@ class DoubleCast $stmts = (array)(array) $classMethod->stmts; end($stmts); $lastItemKey = key($stmts); + reset($stmts); } } diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_else.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_else.php.inc index a7e76495..c7103545 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_else.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_else.php.inc @@ -29,6 +29,7 @@ final class KeepInsideIf } else { end($argumentValue); $this->completionValue = $argumentValue ? $argumentValue[key($argumentValue)] : null; + reset($argumentValue); } } } diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_if.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_if.php.inc index 270501d8..34773fb1 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_if.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/keep_inside_if.php.inc @@ -27,6 +27,7 @@ final class KeepInsideIf if (\is_array($argumentValue)) { end($argumentValue); $this->completionValue = $argumentValue ? $argumentValue[key($argumentValue)] : null; + reset($argumentValue); } } } diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/method_call_as_arg.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/method_call_as_arg.php.inc index c324acb7..bbfce08e 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/method_call_as_arg.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/method_call_as_arg.php.inc @@ -27,6 +27,7 @@ class MethodCallAsArg $getArgs = $node->getArgs(); end($getArgs); $lastItemKey = key($getArgs); + reset($getArgs); } } diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php index 79f37245..2ebec8bb 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php @@ -214,6 +214,10 @@ private function refactorArrayKeyLast( $newStmts[] = $stmt; + if (! $stmt instanceof StmtsAwareInterface && ! $stmt instanceof Return_) { + $newStmts[] = new Expression($this->nodeFactory->createFuncCall('reset', [$array])); + } + return $newStmts; } From 98110d8bbfffc006aab0eb1e194b900cd81c8cfb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Mar 2024 13:20:36 +0700 Subject: [PATCH 2/3] fix --- .../Fixture/inside_if_cond.php.inc | 1 + .../possible_non_array_or_object_array_key_last.php.inc | 1 + .../Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/inside_if_cond.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/inside_if_cond.php.inc index 3d6ef80c..a2fc3ba8 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/inside_if_cond.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/inside_if_cond.php.inc @@ -26,6 +26,7 @@ final class InsideIfCond { end($items); if (key($items)) { + reset($items); return true; } else { return false; diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/possible_non_array_or_object_array_key_last.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/possible_non_array_or_object_array_key_last.php.inc index 05f69ac1..74179653 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/possible_non_array_or_object_array_key_last.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/possible_non_array_or_object_array_key_last.php.inc @@ -36,6 +36,7 @@ class PossibleNonArrayOrObjectArrayKeyLast } if (!\is_array($v) || 1 !== \count($v) || 10 !== \strlen($k = (string) key($v)) || "\x9D" !== $k[0] || "\0" !== $k[5] || "\x5F" !== $k[9]) { + reset($v); return false; } diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php index 2ebec8bb..dc412082 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php @@ -214,10 +214,19 @@ private function refactorArrayKeyLast( $newStmts[] = $stmt; + $resetExpression = new Expression($this->nodeFactory->createFuncCall('reset', [$array])); + if (! $stmt instanceof StmtsAwareInterface && ! $stmt instanceof Return_) { $newStmts[] = new Expression($this->nodeFactory->createFuncCall('reset', [$array])); } + if ($stmt instanceof StmtsAwareInterface) { + $stmt->stmts = array_merge( + [$resetExpression], + $stmt->stmts + ); + } + return $newStmts; } From 6807fe6669d5d31600d7f2a9978382f00dab8109 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 8 Mar 2024 06:21:41 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- .../Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php index dc412082..6fd2b54d 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php @@ -221,10 +221,7 @@ private function refactorArrayKeyLast( } if ($stmt instanceof StmtsAwareInterface) { - $stmt->stmts = array_merge( - [$resetExpression], - $stmt->stmts - ); + $stmt->stmts = array_merge([$resetExpression], $stmt->stmts); } return $newStmts;