Skip to content

Commit

Permalink
[Renaming] Remove Scope filling from Name node on PHPStanNodeScopeRes…
Browse files Browse the repository at this point in the history
…olver for RenameClassRector (#4422)

* [Renaming] Remove Scope filling from Name node on PHPStanNodeScopeResolver for RenameClassRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* clean up

* [ci-review] Rector Rectify

* avoid too deep scope filling

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 5, 2023
1 parent b4ff32e commit 3fa4bec
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 106 deletions.
5 changes: 5 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Expand Up @@ -222,6 +222,11 @@ final class AttributeKey
*/
public const STATEMENT_DEPTH = 'statementDepth';

/**
* @var string
*/
public const EXPRESSION_DEPTH = 'expressionDepth';

/**
* @var string
*/
Expand Down
102 changes: 0 additions & 102 deletions packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
Expand All @@ -18,7 +17,6 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
Expand All @@ -30,7 +28,6 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -39,17 +36,12 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\Node\Stmt\TraitUseAdaptation\Precedence;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\UnionType;
use PhpParser\NodeTraverser;
use PHPStan\AnalysedCodeException;
Expand Down Expand Up @@ -143,48 +135,6 @@ public function processNodes(
return;
}

if ($node instanceof Namespace_ && $node->name instanceof Name) {
$node->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ($node instanceof Instanceof_) {
$node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$node->class->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ($node instanceof UseUse) {
$node->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ($node instanceof GroupUse) {
$node->prefix->setAttribute(AttributeKey::SCOPE, $mutatingScope);
foreach ($node->uses as $use) {
$use->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}

if ($node instanceof TraitUse) {
foreach ($node->traits as $traitName) {
$traitName->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

foreach ($node->adaptations as $precedence) {
if ($precedence instanceof Precedence) {
foreach ($precedence->insteadof as $insteadof) {
$insteadof->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ($precedence->trait instanceof Name) {
$precedence->trait->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}
}
}

if ($node instanceof Attribute) {
$node->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ((
$node instanceof Expression ||
$node instanceof Return_ ||
Expand Down Expand Up @@ -316,21 +266,6 @@ public function processNodes(
if ($node instanceof Class_ || $node instanceof Interface_ || $node instanceof Enum_) {
/** @var MutatingScope $mutatingScope */
$mutatingScope = $this->resolveClassOrInterfaceScope($node, $mutatingScope, $isScopeRefreshing);
if ($node instanceof Class_ && $node->extends instanceof FullyQualified) {
$node->extends->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ($node instanceof Interface_) {
foreach ($node->extends as $extend) {
$extend->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}

if ($node instanceof Class_ || $node instanceof Enum_) {
foreach ($node->implements as $implement) {
$implement->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}
}

// special case for unreachable nodes
Expand All @@ -346,8 +281,6 @@ public function processNodes(

private function processCallike(CallLike $callLike, MutatingScope $mutatingScope): void
{
$this->processArgsForCallike($callLike, $mutatingScope);

if ($callLike instanceof StaticCall) {
$callLike->class->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$callLike->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
Expand All @@ -372,45 +305,10 @@ private function processCallike(CallLike $callLike, MutatingScope $mutatingScope
}
}

private function processArgsForCallike(Expr $expr, MutatingScope $mutatingScope): void
{
if (! $expr instanceof CallLike) {
return;
}

if (! $expr->isFirstClassCallable()) {
foreach ($expr->getArgs() as $arg) {
$arg->value->setAttribute(AttributeKey::SCOPE, $mutatingScope);
if ($arg->value instanceof PropertyFetch) {
$arg->value->var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}
}
}

private function processAssign(Assign|AssignOp $assign, MutatingScope $mutatingScope): void
{
$assign->var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
if ($assign->var instanceof Variable && $assign->var->name instanceof Expr) {
$assign->var->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

$assign->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);

$expr = $assign;

while ($expr instanceof Assign || $expr instanceof AssignOp) {
$this->processArgsForCallike($expr->expr, $mutatingScope);

$expr->var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
if ($expr->var instanceof Variable && $expr->var->name instanceof Expr) {
$expr->var->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

$expr->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);

$expr = $expr->expr;
}
}

private function processArray(Array_ $array, MutatingScope $mutatingScope): void
Expand Down
@@ -0,0 +1,9 @@
<?php

abstract class SkipVariableVariable3
{
private static function loadCurveByParam(\DOMXPath $xpath)
{
$$param = $$bar = self::decodeValue($result::$item(0)::$textContent);
}
}
@@ -0,0 +1,9 @@
<?php

abstract class SkipVariableVariable4
{
private static function loadCurveByParam(\DOMXPath $xpath)
{
$$param = $$bar = self::decodeValue($result->getItem()->getTextContent());
}
}
10 changes: 6 additions & 4 deletions rules/Renaming/Rector/Name/RenameClassRector.php
Expand Up @@ -11,11 +11,11 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Renaming\Helper\RenameClassCallbackHandler;
use Rector\Renaming\NodeManipulator\ClassRenamer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -25,7 +25,7 @@
/**
* @see \Rector\Tests\Renaming\Rector\Name\RenameClassRector\RenameClassRectorTest
*/
final class RenameClassRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
final class RenameClassRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
Expand Down Expand Up @@ -94,14 +94,16 @@ public function getNodeTypes(): array
/**
* @param FunctionLike|Name|ClassLike|Expression|Namespace_|Property $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
if ($oldToNewClasses !== []) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
return $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
}

if ($this->renameClassCallbackHandler->hasOldToNewClassCallbacks()) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
return $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
}

Expand Down
4 changes: 4 additions & 0 deletions src/NodeAnalyzer/ScopeAnalyzer.php
Expand Up @@ -49,6 +49,10 @@ public function resolveScope(
return $this->scopeFactory->createFromFile($filePath);
}

if ($node->getAttribute(AttributeKey::EXPRESSION_DEPTH) >= 2) {
return $this->scopeFactory->createFromFile($filePath);
}

/**
* Node and parent Node doesn't has Scope, and Node Start token pos is < 0,
* it means the node and parent node just re-printed, the Scope need to be resolved from file
Expand Down

0 comments on commit 3fa4bec

Please sign in to comment.