diff --git a/fixtures/completion/this.php b/fixtures/completion/this.php new file mode 100644 index 0000000..b3d684c --- /dev/null +++ b/fixtures/completion/this.php @@ -0,0 +1,15 @@ + + } +} diff --git a/fixtures/completion/this_with_prefix.php b/fixtures/completion/this_with_prefix.php new file mode 100644 index 0000000..6325b99 --- /dev/null +++ b/fixtures/completion/this_with_prefix.php @@ -0,0 +1,15 @@ +m + } +} diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 7772990..24bd28b 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -575,7 +575,7 @@ class DefinitionResolver // $myVariable -> type of corresponding assignment expression if ($expr instanceof Node\Expression\Variable || $expr instanceof Node\UseVariableName) { if ($expr->getName() === 'this') { - return new Types\This; + return new Types\Object_(new Fqsen('\\' . $this->getContainingClassFqn($expr))); } // Find variable definition (parameter or assignment expression) $defNode = $this->resolveVariableToNode($expr); diff --git a/tests/Server/TextDocument/CompletionTest.php b/tests/Server/TextDocument/CompletionTest.php index be34200..3cdb5f8 100644 --- a/tests/Server/TextDocument/CompletionTest.php +++ b/tests/Server/TextDocument/CompletionTest.php @@ -569,4 +569,88 @@ class CompletionTest extends TestCase $this->assertEquals($subsetList->isIncomplete, $list->isIncomplete); } + + public function testThisWithoutPrefix() + { + $completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/this.php'); + $this->loader->open($completionUri, file_get_contents($completionUri)); + $items = $this->textDocument->completion( + new TextDocumentIdentifier($completionUri), + new Position(12, 15) + )->wait(); + $this->assertEquals(new CompletionList([ + new CompletionItem( + 'foo', + CompletionItemKind::PROPERTY, + 'mixed', // Type of the property + null + ), + new CompletionItem( + 'bar', + CompletionItemKind::PROPERTY, + 'mixed', // Type of the property + null + ), + new CompletionItem( + 'method', + CompletionItemKind::METHOD, + 'mixed', // Return type of the method + null + ), + new CompletionItem( + 'test', + CompletionItemKind::METHOD, + 'mixed', // Return type of the method + null + ) + ], true), $items); + } + + public function testThisWithPrefix() + { + $completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/this_with_prefix.php'); + $this->loader->open($completionUri, file_get_contents($completionUri)); + $items = $this->textDocument->completion( + new TextDocumentIdentifier($completionUri), + new Position(12, 16) + )->wait(); + $this->assertEquals(new CompletionList([ + new CompletionItem( + 'testProperty', + CompletionItemKind::PROPERTY, + '\TestClass', // Type of the property + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'testMethod', + CompletionItemKind::METHOD, + '\TestClass', // Return type of the method + 'Non culpa nostrud mollit esse sunt laboris in irure ullamco cupidatat amet.' + ), + new CompletionItem( + 'foo', + CompletionItemKind::PROPERTY, + 'mixed', // Type of the property + null + ), + new CompletionItem( + 'bar', + CompletionItemKind::PROPERTY, + 'mixed', // Type of the property + null + ), + new CompletionItem( + 'method', + CompletionItemKind::METHOD, + 'mixed', // Return type of the method + null + ), + new CompletionItem( + 'test', + CompletionItemKind::METHOD, + 'mixed', // Return type of the method + null + ) + ], true), $items); + } }