diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index 5ec9c97..8c39c77 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -201,9 +201,6 @@ class CompletionProvider $prefixLen = strlen($namePrefix); $scope = getScopeAtNode($this->definitionResolver, $node); $variables = $scope->variables; - if ($scope->thisVariable !== null) { - $variables['this'] = $scope->thisVariable; - } foreach ($variables as $name => $var) { if (substr($name, 0, $prefixLen) !== $namePrefix) { continue; diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 812e664..52d85a3 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -563,9 +563,6 @@ class DefinitionResolver // $myVariable -> type of corresponding assignment expression if ($expr instanceof Node\Expression\Variable || $expr instanceof Node\UseVariableName) { $name = $expr->getName(); - if ($name === 'this') { - return $scope->thisVariable === null ? new Types\Mixed_ : $scope->thisVariable->type; - } return isset($scope->variables[$name]) ? $scope->variables[$name]->type : new Types\Mixed_; diff --git a/src/Scope/Scope.php b/src/Scope/Scope.php index d43abcf..5c137af 100644 --- a/src/Scope/Scope.php +++ b/src/Scope/Scope.php @@ -9,13 +9,6 @@ use Microsoft\PhpParser\Node\QualifiedName; */ class Scope { - /** - * @var Variable|null $this - * - * Note that this will be set when a class is entered. It is unset again when entering a static function. - */ - public $thisVariable; - /** * @var Variable|null $this, except also set in static contexts. */ diff --git a/src/Scope/TreeTraverser.php b/src/Scope/TreeTraverser.php index 4413f47..7935871 100644 --- a/src/Scope/TreeTraverser.php +++ b/src/Scope/TreeTraverser.php @@ -102,8 +102,8 @@ class TreeTraverser $childScope->currentClassLikeVariable = $scope->currentClassLikeVariable; $childScope->resolvedNameCache = $scope->resolvedNameCache; $isStatic = $node instanceof Node\MethodDeclaration ? $node->isStatic() : !empty($node->staticModifier); - if (!$isStatic) { - $childScope->thisVariable = $scope->thisVariable; + if (!$isStatic && isset($scope->variables['this'])) { + $childScope->variables['this'] = $scope->variables['this']; } if ($node->parameters !== null) { @@ -138,11 +138,12 @@ class TreeTraverser ) { $childScope = new Scope; $childScope->resolvedNameCache = $scope->resolvedNameCache; - $childScope->thisVariable = new Variable( + $thisVar = new Variable( new Types\Object_(new Fqsen('\\' . (string)$node->getNamespacedName())), $node ); - $childScope->currentClassLikeVariable = $childScope->thisVariable; + $childScope->variables['this'] = $thisVar; + $childScope->currentClassLikeVariable = $thisVar; return $childScope; } diff --git a/src/TreeAnalyzer.php b/src/TreeAnalyzer.php index c7c6e9f..176dc5e 100644 --- a/src/TreeAnalyzer.php +++ b/src/TreeAnalyzer.php @@ -106,8 +106,8 @@ class TreeAnalyzer } // Check for invalid usage of $this. - if ($scope->thisVariable === null && - $node instanceof Node\Expression\Variable && + if ($node instanceof Node\Expression\Variable && + !isset($scope->variables['this']) && $node->getName() === 'this' ) { $this->diagnostics[] = new Diagnostic(