Skip to content

Commit

Permalink
[Core] Apply Scope refresh for Namespace_ and FileWithoutNamespace (#…
Browse files Browse the repository at this point in the history
…2569)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 27, 2022
1 parent 0a644b1 commit 89a684f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
20 changes: 13 additions & 7 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
Expand All @@ -48,7 +49,8 @@ public function __construct(
private readonly ScopeAnalyzer $scopeAnalyzer,
private readonly UnreachableStmtAnalyzer $unreachableStmtAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly CurrentFileProvider $currentFileProvider
private readonly CurrentFileProvider $currentFileProvider,
private readonly ScopeFactory $scopeFactory
) {
}

Expand All @@ -59,6 +61,16 @@ public function refresh(Node $node, ?MutatingScope $mutatingScope, ?SmartFileInf
return;
}

if (! $smartFileInfo instanceof SmartFileInfo) {
/** @var File $file */
$file = $this->currentFileProvider->getFile();
$smartFileInfo = $file->getSmartFileInfo();
}

if ($this->scopeAnalyzer->isScopeResolvableFromFile($node, $mutatingScope)) {
$mutatingScope = $this->scopeFactory->createFromFile($smartFileInfo);
}

if (! $mutatingScope instanceof MutatingScope) {
/**
* Node does not has Scope, while:
Expand All @@ -79,12 +91,6 @@ public function refresh(Node $node, ?MutatingScope $mutatingScope, ?SmartFileInf
}
}

if (! $smartFileInfo instanceof SmartFileInfo) {
/** @var File $file */
$file = $this->currentFileProvider->getFile();
$smartFileInfo = $file->getSmartFileInfo();
}

// note from flight: when we traverse ClassMethod, the scope must be already in Class_, otherwise it crashes
// so we need to somehow get a parent scope that is already in the same place the $node is

Expand Down
25 changes: 17 additions & 8 deletions src/NodeAnalyzer/ScopeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\Analyser\MutatingScope;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;

final class ScopeAnalyzer
{
/**
* @var array<class-string<Node>>
*/
private const NO_SCOPE_NODES = [
Name::class,
Namespace_::class,
FileWithoutNamespace::class,
Identifier::class,
Param::class,
Arg::class,
];
private const NO_SCOPE_NODES = [Name::class, Identifier::class, Param::class, Arg::class];

/**
* @var array<class-string<Stmt>>
*/
private const RESOLVABLE_FROM_FILE_NODES = [Namespace_::class, FileWithoutNamespace::class];

public function hasScope(Node $node): bool
{
Expand All @@ -36,4 +36,13 @@ public function hasScope(Node $node): bool

return true;
}

public function isScopeResolvableFromFile(Node $node, ?MutatingScope $mutatingScope): bool
{
if ($mutatingScope instanceof MutatingScope) {
return false;
}

return in_array($node::class, self::RESOLVABLE_FROM_FILE_NODES, true);
}
}

0 comments on commit 89a684f

Please sign in to comment.