refactor(scope): rename currentClassLikeVariable to currentSelf
parent
a8829a9b44
commit
77345799f5
|
@ -277,10 +277,10 @@ class DefinitionResolver
|
||||||
$name = $node->getName();
|
$name = $node->getName();
|
||||||
// Resolve $this to the containing class definition.
|
// Resolve $this to the containing class definition.
|
||||||
if ($name === 'this') {
|
if ($name === 'this') {
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$fqn = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$fqn = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
return $this->index->getDefinition($fqn, false);
|
return $this->index->getDefinition($fqn, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,16 +297,16 @@ class DefinitionResolver
|
||||||
if ($fqn === 'self' || $fqn === 'static') {
|
if ($fqn === 'self' || $fqn === 'static') {
|
||||||
// Resolve self and static keywords to the containing class
|
// Resolve self and static keywords to the containing class
|
||||||
// (This is not 100% correct for static but better than nothing)
|
// (This is not 100% correct for static but better than nothing)
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$fqn = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$fqn = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
} else if ($fqn === 'parent') {
|
} else if ($fqn === 'parent') {
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Resolve parent keyword to the base class FQN
|
// Resolve parent keyword to the base class FQN
|
||||||
$classNode = $scope->currentClassLikeVariable->definitionNode;
|
$classNode = $scope->currentSelf->definitionNode;
|
||||||
if (!$classNode->classBaseClause || !$classNode->classBaseClause->baseClass) {
|
if (!$classNode->classBaseClause || !$classNode->classBaseClause->baseClass) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -439,10 +439,10 @@ class DefinitionResolver
|
||||||
|| $varType instanceof Types\Self_
|
|| $varType instanceof Types\Self_
|
||||||
) {
|
) {
|
||||||
// $this/static/self is resolved to the containing class
|
// $this/static/self is resolved to the containing class
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$classFqn = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$classFqn = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
} else if (!($varType instanceof Types\Object_) || $varType->getFqsen() === null) {
|
} else if (!($varType instanceof Types\Object_) || $varType->getFqsen() === null) {
|
||||||
// Left-hand expression could not be resolved to a class
|
// Left-hand expression could not be resolved to a class
|
||||||
return null;
|
return null;
|
||||||
|
@ -498,7 +498,7 @@ class DefinitionResolver
|
||||||
|
|
||||||
if ($className === 'self' || $className === 'static' || $className === 'parent') {
|
if ($className === 'self' || $className === 'static' || $className === 'parent') {
|
||||||
// self and static are resolved to the containing class
|
// self and static are resolved to the containing class
|
||||||
$classNode = $scope->currentClassLikeVariable->definitionNode ?? null;
|
$classNode = $scope->currentSelf->definitionNode ?? null;
|
||||||
if ($classNode === null) {
|
if ($classNode === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -509,7 +509,7 @@ class DefinitionResolver
|
||||||
}
|
}
|
||||||
$className = $scope->getResolvedName($classNode->extends);
|
$className = $scope->getResolvedName($classNode->extends);
|
||||||
} else {
|
} else {
|
||||||
$className = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$className = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
}
|
}
|
||||||
} elseif ($scoped->scopeResolutionQualifier instanceof Node\QualifiedName) {
|
} elseif ($scoped->scopeResolutionQualifier instanceof Node\QualifiedName) {
|
||||||
$className = $scope->getResolvedName($scoped->scopeResolutionQualifier);
|
$className = $scope->getResolvedName($scoped->scopeResolutionQualifier);
|
||||||
|
@ -643,10 +643,10 @@ class DefinitionResolver
|
||||||
}
|
}
|
||||||
for ($i = 0; $t = $objType->get($i); $i++) {
|
for ($i = 0; $t = $objType->get($i); $i++) {
|
||||||
if ($t instanceof Types\This) {
|
if ($t instanceof Types\This) {
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return new Types\Mixed_;
|
return new Types\Mixed_;
|
||||||
}
|
}
|
||||||
$classFqn = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$classFqn = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
} else if (!($t instanceof Types\Object_) || $t->getFqsen() === null) {
|
} else if (!($t instanceof Types\Object_) || $t->getFqsen() === null) {
|
||||||
return new Types\Mixed_;
|
return new Types\Mixed_;
|
||||||
} else {
|
} else {
|
||||||
|
@ -920,15 +920,15 @@ class DefinitionResolver
|
||||||
$className = $scope->getResolvedName($class);
|
$className = $scope->getResolvedName($class);
|
||||||
|
|
||||||
if ($className === 'self') {
|
if ($className === 'self') {
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return new Types\Self_;
|
return new Types\Self_;
|
||||||
}
|
}
|
||||||
return $scope->currentClassLikeVariable->type;
|
return $scope->currentSelf->type;
|
||||||
} else if ($className === 'parent') {
|
} else if ($className === 'parent') {
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return new Types\Object_;
|
return new Types\Object_;
|
||||||
}
|
}
|
||||||
$classNode = $scope->currentClassLikeVariable->definitionNode;
|
$classNode = $scope->currentSelf->definitionNode;
|
||||||
if (empty($classNode->classBaseClause)
|
if (empty($classNode->classBaseClause)
|
||||||
|| !$classNode->classBaseClause->baseClass instanceof Node\QualifiedName
|
|| !$classNode->classBaseClause->baseClass instanceof Node\QualifiedName
|
||||||
) {
|
) {
|
||||||
|
@ -1027,8 +1027,8 @@ class DefinitionResolver
|
||||||
&& ($returnType = $returnTags[0]->getType()) !== null
|
&& ($returnType = $returnTags[0]->getType()) !== null
|
||||||
) {
|
) {
|
||||||
// Use @return tag
|
// Use @return tag
|
||||||
if ($returnType instanceof Types\Self_ && null !== $scope->currentClassLikeVariable) {
|
if ($returnType instanceof Types\Self_ && null !== $scope->currentSelf) {
|
||||||
return $scope->currentClassLikeVariable->type;
|
return $scope->currentSelf->type;
|
||||||
}
|
}
|
||||||
return $returnType;
|
return $returnType;
|
||||||
}
|
}
|
||||||
|
@ -1037,8 +1037,8 @@ class DefinitionResolver
|
||||||
if ($node->returnType instanceof PhpParser\Token) {
|
if ($node->returnType instanceof PhpParser\Token) {
|
||||||
// Resolve a string like "bool" to a type object
|
// Resolve a string like "bool" to a type object
|
||||||
return $this->typeResolver->resolve($node->returnType->getText($node->getFileContents()));
|
return $this->typeResolver->resolve($node->returnType->getText($node->getFileContents()));
|
||||||
} else if ($scope->currentClassLikeVariable !== null && $scope->getResolvedName($node->returnType) === 'self') {
|
} else if ($scope->currentSelf !== null && $scope->getResolvedName($node->returnType) === 'self') {
|
||||||
return $scope->currentClassLikeVariable->type;
|
return $scope->currentSelf->type;
|
||||||
}
|
}
|
||||||
return new Types\Object_(new Fqsen('\\' . $scope->getResolvedName($node->returnType)));
|
return new Types\Object_(new Fqsen('\\' . $scope->getResolvedName($node->returnType)));
|
||||||
}
|
}
|
||||||
|
@ -1160,10 +1160,10 @@ class DefinitionResolver
|
||||||
// }
|
// }
|
||||||
if ($node instanceof Node\MethodDeclaration) {
|
if ($node instanceof Node\MethodDeclaration) {
|
||||||
// Class method: use ClassName->methodName() as name
|
// Class method: use ClassName->methodName() as name
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$className = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$className = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
if (!$className) {
|
if (!$className) {
|
||||||
// Ignore anonymous classes
|
// Ignore anonymous classes
|
||||||
return null;
|
return null;
|
||||||
|
@ -1183,10 +1183,10 @@ class DefinitionResolver
|
||||||
// }
|
// }
|
||||||
if (
|
if (
|
||||||
($propertyDeclaration = ParserHelpers\tryGetPropertyDeclaration($node)) !== null &&
|
($propertyDeclaration = ParserHelpers\tryGetPropertyDeclaration($node)) !== null &&
|
||||||
$scope->currentClassLikeVariable !== null &&
|
$scope->currentSelf !== null &&
|
||||||
isset($scope->currentClassLikeVariable->definitionNode->name)
|
isset($scope->currentSelf->definitionNode->name)
|
||||||
) {
|
) {
|
||||||
$className = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$className = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
$name = $node->getName();
|
$name = $node->getName();
|
||||||
if ($propertyDeclaration->isStatic()) {
|
if ($propertyDeclaration->isStatic()) {
|
||||||
// Static Property: use ClassName::$propertyName as name
|
// Static Property: use ClassName::$propertyName as name
|
||||||
|
@ -1209,13 +1209,12 @@ class DefinitionResolver
|
||||||
return (string)$node->getNamespacedName();
|
return (string)$node->getNamespacedName();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($scope->currentClassLikeVariable === null
|
if ($scope->currentSelf === null || !isset($scope->currentSelf->definitionNode->name)
|
||||||
|| !isset($scope->currentClassLikeVariable->definitionNode->name)
|
|
||||||
) {
|
) {
|
||||||
// Class constant: use ClassName::CONSTANT_NAME as name
|
// Class constant: use ClassName::CONSTANT_NAME as name
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$className = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$className = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
return $className . '::' . $node->getName();
|
return $className . '::' . $node->getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@ use Microsoft\PhpParser\Node\QualifiedName;
|
||||||
class Scope
|
class Scope
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Variable|null $this, except also set in static contexts.
|
* @var Variable|null "Variable" representing this/self
|
||||||
*/
|
*/
|
||||||
public $currentClassLikeVariable;
|
public $currentSelf;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Variable[] Variables in the scope, indexed by their names (without the dollar) and excluding $this.
|
* @var Variable[] Variables in the scope, indexed by their names (without the dollar) and excluding $this.
|
||||||
|
|
|
@ -99,7 +99,7 @@ class TreeTraverser
|
||||||
&& $node->compoundStatementOrSemicolon instanceof Node\Statement\CompoundStatementNode
|
&& $node->compoundStatementOrSemicolon instanceof Node\Statement\CompoundStatementNode
|
||||||
) {
|
) {
|
||||||
$childScope = new Scope;
|
$childScope = new Scope;
|
||||||
$childScope->currentClassLikeVariable = $scope->currentClassLikeVariable;
|
$childScope->currentSelf = $scope->currentSelf;
|
||||||
$childScope->resolvedNameCache = $scope->resolvedNameCache;
|
$childScope->resolvedNameCache = $scope->resolvedNameCache;
|
||||||
$isStatic = $node instanceof Node\MethodDeclaration ? $node->isStatic() : !empty($node->staticModifier);
|
$isStatic = $node instanceof Node\MethodDeclaration ? $node->isStatic() : !empty($node->staticModifier);
|
||||||
if (!$isStatic && isset($scope->variables['this'])) {
|
if (!$isStatic && isset($scope->variables['this'])) {
|
||||||
|
@ -143,7 +143,7 @@ class TreeTraverser
|
||||||
$node
|
$node
|
||||||
);
|
);
|
||||||
$childScope->variables['this'] = $thisVar;
|
$childScope->variables['this'] = $thisVar;
|
||||||
$childScope->currentClassLikeVariable = $thisVar;
|
$childScope->currentSelf = $thisVar;
|
||||||
return $childScope;
|
return $childScope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,16 +157,16 @@ class TreeAnalyzer
|
||||||
if ($fqn === 'self' || $fqn === 'static') {
|
if ($fqn === 'self' || $fqn === 'static') {
|
||||||
// Resolve self and static keywords to the containing class
|
// Resolve self and static keywords to the containing class
|
||||||
// (This is not 100% correct for static but better than nothing)
|
// (This is not 100% correct for static but better than nothing)
|
||||||
if (!$scope->currentClassLikeVariable) {
|
if (!$scope->currentSelf) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$fqn = substr((string)$scope->currentClassLikeVariable->type->getFqsen(), 1);
|
$fqn = substr((string)$scope->currentSelf->type->getFqsen(), 1);
|
||||||
} else if ($fqn === 'parent') {
|
} else if ($fqn === 'parent') {
|
||||||
// Resolve parent keyword to the base class FQN
|
// Resolve parent keyword to the base class FQN
|
||||||
if ($scope->currentClassLikeVariable === null) {
|
if ($scope->currentSelf === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$classNode = $scope->currentClassLikeVariable->definitionNode;
|
$classNode = $scope->currentSelf->definitionNode;
|
||||||
if (empty($classNode->classBaseClause)
|
if (empty($classNode->classBaseClause)
|
||||||
|| !$classNode->classBaseClause->baseClass instanceof Node\QualifiedName
|
|| !$classNode->classBaseClause->baseClass instanceof Node\QualifiedName
|
||||||
) {
|
) {
|
||||||
|
|
Loading…
Reference in New Issue