From a775c65eeccc0c6ed106adbe836ab114f37c5da4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 11 Mar 2024 18:16:44 +0700 Subject: [PATCH] [CodeQuality] Skip throw after foreach with return in loop on SimplifyForeachToCoalescingRector (#5714) * CodeQuality] Skip throw after foreach with return in loop on SimplifyForeachToCoalescingRector * CodeQuality] Skip throw after foreach with return in loop on SimplifyForeachToCoalescingRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action --- .../Fixture/skip_throw_after_foreach.php.inc | 26 +++++++++++++++++++ .../SimplifyForeachToCoalescingRector.php | 8 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector/Fixture/skip_throw_after_foreach.php.inc diff --git a/rules-tests/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector/Fixture/skip_throw_after_foreach.php.inc b/rules-tests/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector/Fixture/skip_throw_after_foreach.php.inc new file mode 100644 index 00000000000..513371b7e13 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector/Fixture/skip_throw_after_foreach.php.inc @@ -0,0 +1,26 @@ + + * + * @throws Exception + */ + private function getFilterConfig(): array + { + $shopConfig = clxMobileNet::config('shopconfig'); + + foreach ($shopConfig as $key => $mixedValues) { + if (self::SHOPCONFIGKEY_FILTER === $key) { + return $mixedValues; + } + } + + throw new \Exception('Usage of "'.self::class.'" assumes that ShopConfig "'.self::SHOPCONFIGKEY_FILTER.'" is defined!'); + } +} \ No newline at end of file diff --git a/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php b/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php index 8dae40fb421..f2508a4d990 100644 --- a/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php +++ b/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php @@ -112,6 +112,10 @@ public function refactor(Node $node): ?Node $nextStmt = $node->stmts[$key + 1] ?? null; + if (! $nextStmt instanceof Return_) { + continue; + } + $return = $this->processForeachNodeWithReturnInside($foreach, $foreachReturnOrAssign, $nextStmt); if (! $return instanceof Return_) { @@ -121,9 +125,7 @@ public function refactor(Node $node): ?Node $node->stmts[$key] = $return; // cleanup next return - if ($nextStmt instanceof Return_) { - unset($node->stmts[$key + 1]); - } + unset($node->stmts[$key + 1]); $hasChanged = true; }