Skip to content

Commit

Permalink
[CodeQuality] Skip throw after foreach with return in loop on Simplif…
Browse files Browse the repository at this point in the history
…yForeachToCoalescingRector (#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 <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Mar 11, 2024
1 parent 01c460e commit a775c65
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector\Fixture;

class SkipThrowAfterForeach
{
public const SHOPCONFIGKEY_FILTER = 'MY_KEY';

/**
* @return array<string, mixed>
*
* @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!');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand All @@ -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;
}
Expand Down

0 comments on commit a775c65

Please sign in to comment.