Skip to content

Commit

Permalink
Remove NEXT_NODE from RemoveDuplicatedIfReturnRector (#3858)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed May 15, 2023
1 parent 7a86ac2 commit 3df70bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 64 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector\Fixture;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;

final class SkipParamReference
{
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}

public function run(Node\Stmt $stmt): void
{
$isParamUsed = false;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmt, function (Node $node) use (
&$isParamUsed,
): ?int {
if ($isParamUsed) {
return NodeTraverser::STOP_TRAVERSAL;
}

// skip nested anonymous class
if ($node instanceof Class_ || $node instanceof Function_) {
$isParamUsed = true;

return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\DeadCode\Rector\FunctionLike;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
Expand All @@ -19,7 +18,6 @@
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\NodeCollector\ModifiedVariableNamesCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -99,11 +97,12 @@ public function refactor(Node $node): ?Node
return null;
}

$hasRemovedNode = false;
$hasChanged = false;

foreach ($ifWithOnlyReturnsByHash as $ifWithOnlyReturns) {
$isBool = $this->isBoolVarIfCondReturnTrueNextReturnBoolVar($ifWithOnlyReturns);
if (! $isBool && count($ifWithOnlyReturns) < 2) {

if (count($ifWithOnlyReturns) < 2) {
continue;
}

Expand All @@ -114,11 +113,11 @@ public function refactor(Node $node): ?Node

foreach ($ifWithOnlyReturns as $ifWithOnlyReturn) {
$this->removeNode($ifWithOnlyReturn);
$hasRemovedNode = true;
$hasChanged = true;
}
}

if ($hasRemovedNode) {
if ($hasChanged) {
return $node;
}

Expand All @@ -134,42 +133,17 @@ private function isBoolVarIfCondReturnTrueNextReturnBoolVar(array $ifWithOnlyRet
return false;
}

/** @var Expr $cond */
$cond = $ifWithOnlyReturns[0]->cond;
if (! in_array($cond::class, [Variable::class, PropertyFetch::class, StaticPropertyFetch::class], true)) {
return false;
}

$type = $this->nodeTypeResolver->getType($cond);
if (! $type->isBoolean()->yes()) {
return false;
}

$nextNode = $ifWithOnlyReturns[0]->getAttribute(AttributeKey::NEXT_NODE);
if (! $nextNode instanceof Return_) {
return false;
}

$expr = $nextNode->expr;
if (! $expr instanceof Expr) {
return false;
}

if (! $this->nodeComparator->areNodesEqual($expr, $cond)) {
return false;
}

/** @var Return_ $returnStmt */
$returnStmt = $ifWithOnlyReturns[0]->stmts[0];
if (! $returnStmt->expr instanceof Expr) {
return false;
}

return $this->valueResolver->isValue($returnStmt->expr, true);
return $type->isBoolean()->yes();
}

/**
* @return If_[][]
* @return array<string, If_[]>
*/
private function collectDuplicatedIfWithOnlyReturnByHash(FunctionLike $functionLike): array
{
Expand All @@ -183,6 +157,7 @@ private function collectDuplicatedIfWithOnlyReturnByHash(FunctionLike $functionL
$modifiedVariableNames,
$this->modifiedVariableNamesCollector->collectModifiedVariableNames($stmt)
);

continue;
}

Expand Down

0 comments on commit 3df70bd

Please sign in to comment.