Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

abstract class SkipVariableVariable3
{
private static function loadCurveByParam(\DOMXPath $xpath)
{
$$param = $$bar = self::decodeValue($result::$item(0)::$textContent);
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the scope passed in from rector core be the same as the one in the attribute?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still find why Scope for RenameClassRector after refactor require ORIGINAL_NODE instead of Node itself at

/** @var MutatingScope|null $currentScope */
$currentScope = $originalNode->getAttribute(AttributeKey::SCOPE);

that's possibly due to docblock is too late to get Scope, that's another PR to apply if possible ;)

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
Original file line number Diff line number Diff line change
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);
}
Comment on lines +52 to +54
Copy link
Copy Markdown
Member Author

@samsonasik samsonasik Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba this is to avoid too deep scope filing assign -> assign -> expr -> method call -> property fetch , eg:

$$param = $$bar = self::decodeValue($result->getItem()->getTextContent());

that can goes out of control,.


/**
* 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