WIP resolve return self better
parent
78316545a8
commit
50de9bb28e
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
class FooClass {
|
||||
public function foo(): self {
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
$fc = new FooClass();
|
||||
$foo = $fc->foo();
|
||||
$foo->
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
class FooClass {
|
||||
public static function staticFoo(): FooClass {
|
||||
public static function staticFoo(): self {
|
||||
return new FooClass();
|
||||
}
|
||||
|
||||
|
@ -9,4 +9,4 @@ class FooClass {
|
|||
}
|
||||
|
||||
$foo = FooClass::staticFoo();
|
||||
$foo->
|
||||
$foo->
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in New Issue