Skip to content

Commit

Permalink
[Core] [DeadCode] Refactor RectifiedAnalyzer and ExprUsedInNextNodeAn…
Browse files Browse the repository at this point in the history
…alyzer (#1533)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 20, 2021
1 parent 2cdd339 commit c51fcbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
31 changes: 6 additions & 25 deletions rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace Rector\DeadCode\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -22,37 +19,21 @@ public function __construct(
) {
}

/**
* $isCheckNameScope parameter is used to whether to check scope of Name that may be renamed
* @see https://github.com/rectorphp/rector/issues/6675
*/
public function isUsed(Expr $expr, bool $isCheckNameScope = false): bool
public function isUsed(Expr $expr): bool
{
return (bool) $this->betterNodeFinder->findFirstNext(
$expr,
function (Node $node) use ($expr, $isCheckNameScope): bool {
if ($isCheckNameScope && $node instanceof Name) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
$resolvedName = $node->getAttribute(AttributeKey::RESOLVED_NAME);
$next = $node->getAttribute(AttributeKey::NEXT_NODE);
function (Node $node) use ($expr): bool {
$isUsed = $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);

if (! $scope instanceof Scope && ! $resolvedName instanceof Name && $next instanceof Arg) {
return true;
}
}

if (! $node instanceof If_) {
return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
if ($isUsed) {
return true;
}

/**
* handle when used along with RemoveAlwaysElseRector
*/
if (! $this->hasIfChangedByRemoveAlwaysElseRector($node)) {
return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
}

return true;
return $node instanceof If_ && $this->hasIfChangedByRemoveAlwaysElseRector($node);
}
);
}
Expand Down
17 changes: 9 additions & 8 deletions src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Core\ProcessAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Class_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -19,13 +19,18 @@
*
* Some limitations:
*
* - only check against Stmt which not ClassLike.
* - only check against Node which not Assign or Class_
* - The checked node doesn't has PhpDocInfo changed.
*
* 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>
*/
Expand All @@ -38,11 +43,7 @@ public function __construct(

public function verify(RectorInterface $rector, Node $node, File $currentFile): ?RectifiedNode
{
if (! $node instanceof Stmt) {
return null;
}

if ($node instanceof ClassLike) {
if (in_array($node::class, self::EXCLUDE_NODES, true)) {
return null;
}

Expand Down

0 comments on commit c51fcbf

Please sign in to comment.