1
0
Fork 0

added fluent interfaces support

pull/421/head
Ivan Bozhanov 2017-06-19 13:36:32 +03:00
parent dae3f2576c
commit 3d196ede54
3 changed files with 59 additions and 3 deletions

View File

@ -0,0 +1,16 @@
<?php
class Parent1 {
/** @return $this */
public function foo() {
return $this;
}
}
class Child extends Parent1 {
public function bar() {
$this->foo()->q
}
public function qux() {
}
}

View File

@ -574,7 +574,6 @@ class DefinitionResolver
// $this -> Type\this // $this -> Type\this
// $myVariable -> type of corresponding assignment expression // $myVariable -> type of corresponding assignment expression
if ($expr instanceof Node\Expression\Variable || $expr instanceof Node\UseVariableName) { if ($expr instanceof Node\Expression\Variable || $expr instanceof Node\UseVariableName) {
// TODO: this will need to change when fluent interfaces are supported
if ($expr->getName() === 'this') { if ($expr->getName() === 'this') {
return new Types\Object_(new Fqsen('\\' . $this->getContainingClassFqn($expr))); return new Types\Object_(new Fqsen('\\' . $this->getContainingClassFqn($expr)));
} }
@ -667,13 +666,27 @@ class DefinitionResolver
} else { } else {
$classFqn = substr((string)$t->getFqsen(), 1); $classFqn = substr((string)$t->getFqsen(), 1);
} }
$fqn = $classFqn . '->' . $expr->memberName->getText($expr->getFileContents()); $add = '->' . $expr->memberName->getText($expr->getFileContents());
if ($expr->parent instanceof Node\Expression\CallExpression) { if ($expr->parent instanceof Node\Expression\CallExpression) {
$fqn .= '()'; $add .= '()';
} }
$fqn = $classFqn . $add;
$def = $this->index->getDefinition($fqn); $def = $this->index->getDefinition($fqn);
if ($def !== null) { if ($def !== null) {
return $def->type; return $def->type;
} else {
$classDef = $this->index->getDefinition($classFqn);
if ($classDef !== null && is_array($classDef->extends)) {
foreach ($classDef->extends as $parent) {
$def = $this->index->getDefinition($parent . $add);
if ($def !== null) {
if ($def->type instanceof Types\This) {
return new Types\Object_(new Fqsen('\\' . $classFqn));
}
return $def->type;
}
}
}
} }
} }
} }

View File

@ -653,4 +653,31 @@ class CompletionTest extends TestCase
) )
], true), $items); ], true), $items);
} }
public function testThisReturnValue()
{
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/this_return_value.php');
$this->loader->open($completionUri, file_get_contents($completionUri));
$items = $this->textDocument->completion(
new TextDocumentIdentifier($completionUri),
new Position(10, 19)
)->wait();
$this->assertEquals(new CompletionList([
new CompletionItem(
'foo',
CompletionItemKind::METHOD,
'$this' // Return type of the method
),
new CompletionItem(
'bar',
CompletionItemKind::METHOD,
'mixed' // Return type of the method
),
new CompletionItem(
'qux',
CompletionItemKind::METHOD,
'mixed' // Return type of the method
)
], true), $items);
}
} }