diff --git a/rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php b/rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php index 22a78d79a2b..aa635d2580f 100644 --- a/rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php @@ -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; @@ -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); } ); } diff --git a/src/ProcessAnalyzer/RectifiedAnalyzer.php b/src/ProcessAnalyzer/RectifiedAnalyzer.php index 8c9bc75300e..90f759129a1 100644 --- a/src/ProcessAnalyzer/RectifiedAnalyzer.php +++ b/src/ProcessAnalyzer/RectifiedAnalyzer.php @@ -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; @@ -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> + */ + private const EXCLUDE_NODES = [Assign::class, Class_::class]; + /** * @var array */ @@ -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; }