mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-31 05:40:22 +00:00
[Php56] Clean up UndefinedVariableResolver: use existing VariableAnalyzer for check static and global variable (#1844)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
a9321717c3
commit
ce1563528f
@ -20,12 +20,10 @@ use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Global_;
|
||||
use PhpParser\Node\Stmt\Static_;
|
||||
use PhpParser\Node\Stmt\StaticVar;
|
||||
use PhpParser\Node\Stmt\Unset_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
@ -38,7 +36,8 @@ final class UndefinedVariableResolver
|
||||
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
|
||||
private readonly NodeNameResolver $nodeNameResolver,
|
||||
private readonly NodeComparator $nodeComparator,
|
||||
private readonly BetterNodeFinder $betterNodeFinder
|
||||
private readonly BetterNodeFinder $betterNodeFinder,
|
||||
private readonly VariableAnalyzer $variableAnalyzer
|
||||
) {
|
||||
}
|
||||
|
||||
@ -100,13 +99,9 @@ final class UndefinedVariableResolver
|
||||
return $parentNode instanceof Coalesce && $parentNode->left === $variable;
|
||||
}
|
||||
|
||||
private function isAssignOrStaticVariableParent(Node $parentNode): bool
|
||||
private function isAssign(Node $parentNode): bool
|
||||
{
|
||||
if (in_array($parentNode::class, [Assign::class, AssignRef::class], true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->isStaticVariable($parentNode);
|
||||
return in_array($parentNode::class, [Assign::class, AssignRef::class], true);
|
||||
}
|
||||
|
||||
private function shouldSkipVariable(Variable $variable): bool
|
||||
@ -116,11 +111,11 @@ final class UndefinedVariableResolver
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($parentNode instanceof Global_) {
|
||||
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->isAssignOrStaticVariableParent($parentNode)) {
|
||||
if ($this->isAssign($parentNode)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -204,17 +199,6 @@ final class UndefinedVariableResolver
|
||||
});
|
||||
}
|
||||
|
||||
private function isStaticVariable(Node $parentNode): bool
|
||||
{
|
||||
if (! $parentNode instanceof StaticVar) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// definition of static variable
|
||||
$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
return $parentParentNode instanceof Static_;
|
||||
}
|
||||
|
||||
private function isListAssign(Node $node): bool
|
||||
{
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
@ -11,6 +11,7 @@ use PhpParser\Node\Stmt\Static_;
|
||||
use PhpParser\Node\Stmt\StaticVar;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class VariableAnalyzer
|
||||
{
|
||||
@ -22,14 +23,21 @@ final class VariableAnalyzer
|
||||
|
||||
public function isStaticOrGlobal(Variable $variable): bool
|
||||
{
|
||||
if ($this->isParentStaticOrGlobal($variable)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $n) use (
|
||||
$variable
|
||||
): bool {
|
||||
if (! $n instanceof Static_ && ! $n instanceof Global_) {
|
||||
if (! in_array($n::class, [Static_::class, Global_::class], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var StaticVar[]|Variable[] $vars */
|
||||
/**
|
||||
* @var Static_|Global_ $n
|
||||
* @var StaticVar[]|Variable[] $vars
|
||||
*/
|
||||
$vars = $n->vars;
|
||||
foreach ($vars as $var) {
|
||||
$staticVarVariable = $var instanceof StaticVar
|
||||
@ -44,4 +52,24 @@ final class VariableAnalyzer
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private function isParentStaticOrGlobal(Variable $variable): bool
|
||||
{
|
||||
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
||||
if (! $parentNode instanceof Node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($parentNode instanceof Global_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $parentNode instanceof StaticVar) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
return $parentParentNode instanceof Static_;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user