-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Traverser] Add GlobalVariableNodeVisitor (#3816)
* [Traverser] Add GlobalVariableNodeVariableVisitor * rename * Fix return * [ci-review] Rector Rectify * [ci-review] Rector Rectify * Fix phpstan --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
849459b
commit 0d3a9a2
Showing
12 changed files
with
105 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Global_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; | ||
|
||
final class GlobalVariableNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
public function __construct( | ||
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser | ||
) { | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if (! $node instanceof StmtsAwareInterface) { | ||
return null; | ||
} | ||
|
||
if ($node->stmts === null) { | ||
return null; | ||
} | ||
|
||
/** @var string[] $globalVariableNames */ | ||
$globalVariableNames = []; | ||
|
||
foreach ($node->stmts as $stmt) { | ||
if (! $stmt instanceof Global_) { | ||
$this->setIsGlobalVarAttribute($stmt, $globalVariableNames); | ||
continue; | ||
} | ||
|
||
foreach ($stmt->vars as $variable) { | ||
if ($variable instanceof Variable && is_string($variable->name)) { | ||
$variable->setAttribute(AttributeKey::IS_GLOBAL_VAR, true); | ||
$globalVariableNames[] = $variable->name; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param string[] $globalVariableNames | ||
*/ | ||
private function setIsGlobalVarAttribute(Stmt $stmt, array $globalVariableNames): void | ||
{ | ||
if ($globalVariableNames === []) { | ||
return; | ||
} | ||
|
||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable( | ||
$stmt, | ||
static function (Node $subNode) use ($globalVariableNames): int|null|Variable { | ||
if ($subNode instanceof Class_) { | ||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; | ||
} | ||
|
||
if (! $subNode instanceof Variable) { | ||
return null; | ||
} | ||
|
||
if (! is_string($subNode->name)) { | ||
return null; | ||
} | ||
|
||
if (! in_array($subNode->name, $globalVariableNames, true)) { | ||
return null; | ||
} | ||
|
||
$subNode->setAttribute(AttributeKey::IS_GLOBAL_VAR, true); | ||
return $subNode; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters