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; }