From 50de9bb28ec85520ac94551b0e3c43d4877f33d2 Mon Sep 17 00:00:00 2001 From: Philip Nelson Date: Sun, 10 Dec 2017 20:34:25 +1100 Subject: [PATCH] WIP resolve return self better --- fixtures/completion/method_return_self.php | 11 ++++++++++ .../completion/static_method_return_type.php | 4 ++-- src/DefinitionResolver.php | 7 +++++- tests/Server/TextDocument/CompletionTest.php | 22 +++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 fixtures/completion/method_return_self.php diff --git a/fixtures/completion/method_return_self.php b/fixtures/completion/method_return_self.php new file mode 100644 index 0000000..9e6ef55 --- /dev/null +++ b/fixtures/completion/method_return_self.php @@ -0,0 +1,11 @@ +foo(); +$foo-> diff --git a/fixtures/completion/static_method_return_type.php b/fixtures/completion/static_method_return_type.php index 06cafdd..bcc1264 100644 --- a/fixtures/completion/static_method_return_type.php +++ b/fixtures/completion/static_method_return_type.php @@ -1,7 +1,7 @@ \ No newline at end of file +$foo-> diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index c9d1400..5839f71 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -699,7 +699,7 @@ class DefinitionResolver foreach ($classDef->getAncestorDefinitions($this->index, true) as $fqn => $def) { $def = $this->index->getDefinition($fqn . $add); if ($def !== null) { - if ($def->type instanceof Types\This) { + if ($def->type instanceof Types\This || $def->type instanceof Types\Self_) { return new Types\Object_(new Fqsen('\\' . $classFqn)); } return $def->type; @@ -727,6 +727,9 @@ class DefinitionResolver if ($def === null) { return new Types\Mixed_; } + if ($def->type instanceof Types\Self_) { + return new Types\Object_($classType->getFqsen()); + } return $def->type; } @@ -1060,6 +1063,8 @@ class DefinitionResolver if ($node->returnType instanceof PhpParser\Token) { // Resolve a string like "bool" to a type object return $this->typeResolver->resolve($node->returnType->getText($node->getFileContents())); + } elseif ($node->returnType->getResolvedName() === 'self') { + return new Types\Self_(); } return new Types\Object_(new Fqsen('\\' . (string)$node->returnType->getResolvedName())); } diff --git a/tests/Server/TextDocument/CompletionTest.php b/tests/Server/TextDocument/CompletionTest.php index 424dc3c..4e63e75 100644 --- a/tests/Server/TextDocument/CompletionTest.php +++ b/tests/Server/TextDocument/CompletionTest.php @@ -576,6 +576,28 @@ class CompletionTest extends TestCase ], true), $items); } + public function testMethodReturnSelf() + { + $completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/method_return_self.php'); + $this->loader->open($completionUri, file_get_contents($completionUri)); + $items = $this->textDocument->completion( + new TextDocumentIdentifier($completionUri), + new Position(10, 6) + )->wait(); + $this->assertCompletionsListSubset(new CompletionList([ + new CompletionItem( + 'foo', + CompletionItemKind::METHOD, + 'self', + null, + null, + null, + null, + null + ) + ], true), $items); + } + public function testStaticMethodReturnType() { $completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static_method_return_type.php');