Skip to content

Commit

Permalink
[Core] Refactor RectifiedAnalyzer to only exclude Class_ (#1681)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 15, 2022
1 parent 796b097 commit 99da810
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
11 changes: 8 additions & 3 deletions packages/NodeRemoval/AssignRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ public function removeAssignNode(Assign $assign): void
$parent = $assign->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof Expression) {
$this->nodeRemover->removeNode($assign);
} else {
$this->nodesToReplaceCollector->addReplaceNodeWithAnotherNode($assign, $assign->expr);
$this->rectorChangeCollector->notifyNodeFileInfo($assign->expr);
return;
}

$this->nodesToReplaceCollector->addReplaceNodeWithAnotherNode($assign, $assign->expr);
$this->rectorChangeCollector->notifyNodeFileInfo($assign->expr);

if ($parent instanceof Assign) {
$this->removeAssignNode($parent);
}
}
}
22 changes: 1 addition & 21 deletions rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\If_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ExprUsedInNextNodeAnalyzer
{
Expand All @@ -23,24 +20,7 @@ public function isUsed(Expr $expr): bool
{
return (bool) $this->betterNodeFinder->findFirstNext(
$expr,
function (Node $node) use ($expr): bool {
$isUsed = $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);

if ($isUsed) {
return true;
}

/**
* handle when used along with RemoveAlwaysElseRector
*/
return $node instanceof If_ && $this->hasIfChangedByRemoveAlwaysElseRector($node);
}
fn (Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $expr)
);
}

private function hasIfChangedByRemoveAlwaysElseRector(If_ $if): bool
{
$createdByRule = $if->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];
return in_array(RemoveAlwaysElseRector::class, $createdByRule, true);
}
}
10 changes: 2 additions & 8 deletions src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Core\ProcessAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -18,25 +17,20 @@
*
* Limitation:
*
* It only check against Node which not Assign or Class_
* It only check against Node which not a Class_
*
* which possibly changed by other process.
*/
final class RectifiedAnalyzer
{
/**
* @var array<class-string<Node>>
*/
private const EXCLUDE_NODES = [Assign::class, Class_::class];

/**
* @var array<string, RectifiedNode|null>
*/
private array $previousFileWithNodes = [];

public function verify(RectorInterface $rector, Node $node, File $currentFile): ?RectifiedNode
{
if (in_array($node::class, self::EXCLUDE_NODES, true)) {
if ($node instanceof Class_) {
return null;
}

Expand Down

0 comments on commit 99da810

Please sign in to comment.