Skip to content

Commit

Permalink
[EarlyReturn] Remove findFirstNext() on InvertedIfFactory (#4399)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jul 1, 2023
1 parent 6e90cc7 commit 66a9bcf
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions rules/EarlyReturn/NodeFactory/InvertedIfFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@

namespace Rector\EarlyReturn\NodeFactory;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
use Rector\NodeNestingScope\ContextAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class InvertedIfFactory
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ConditionInverter $conditionInverter,
private readonly ContextAnalyzer $contextAnalyzer
) {
Expand All @@ -39,9 +36,8 @@ public function createFromConditions(If_ $if, array $conditions, Return_ $return
$stmt[0]->setAttribute(AttributeKey::COMMENTS, $ifNextReturn->getAttribute(AttributeKey::COMMENTS));
}

$getNextReturnExpr = $this->getNextReturnExpr($if);
if ($getNextReturnExpr instanceof Return_) {
$return->expr = $getNextReturnExpr->expr;
if ($ifNextReturn instanceof Return_ && $ifNextReturn->expr instanceof Expr) {
$return->expr = $ifNextReturn->expr;
}

foreach ($conditions as $condition) {
Expand All @@ -53,12 +49,4 @@ public function createFromConditions(If_ $if, array $conditions, Return_ $return

return $ifs;
}

private function getNextReturnExpr(If_ $if): ?Node
{
return $this->betterNodeFinder->findFirstNext(
$if,
static fn (Node $node): bool => $node instanceof Return_ && $node->expr instanceof Expr
);
}
}

0 comments on commit 66a9bcf

Please sign in to comment.