1
0
Fork 0

WIP resolve return self better

pull/550/head
Philip Nelson 2017-12-10 20:34:25 +11:00
parent 78316545a8
commit 50de9bb28e
4 changed files with 41 additions and 3 deletions

View File

@ -0,0 +1,11 @@
<?php
class FooClass {
public function foo(): self {
return $this;
}
}
$fc = new FooClass();
$foo = $fc->foo();
$foo->

View File

@ -1,7 +1,7 @@
<?php <?php
class FooClass { class FooClass {
public static function staticFoo(): FooClass { public static function staticFoo(): self {
return new FooClass(); return new FooClass();
} }

View File

@ -699,7 +699,7 @@ class DefinitionResolver
foreach ($classDef->getAncestorDefinitions($this->index, true) as $fqn => $def) { foreach ($classDef->getAncestorDefinitions($this->index, true) as $fqn => $def) {
$def = $this->index->getDefinition($fqn . $add); $def = $this->index->getDefinition($fqn . $add);
if ($def !== null) { 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 new Types\Object_(new Fqsen('\\' . $classFqn));
} }
return $def->type; return $def->type;
@ -727,6 +727,9 @@ class DefinitionResolver
if ($def === null) { if ($def === null) {
return new Types\Mixed_; return new Types\Mixed_;
} }
if ($def->type instanceof Types\Self_) {
return new Types\Object_($classType->getFqsen());
}
return $def->type; return $def->type;
} }
@ -1060,6 +1063,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()));
} elseif ($node->returnType->getResolvedName() === 'self') {
return new Types\Self_();
} }
return new Types\Object_(new Fqsen('\\' . (string)$node->returnType->getResolvedName())); return new Types\Object_(new Fqsen('\\' . (string)$node->returnType->getResolvedName()));
} }

View File

@ -576,6 +576,28 @@ class CompletionTest extends TestCase
], true), $items); ], 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() public function testStaticMethodReturnType()
{ {
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static_method_return_type.php'); $completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static_method_return_type.php');