1
0
Fork 0

Resolve $this (#98)

pull/99/head
Felix Becker 2016-10-20 00:10:47 +02:00 committed by GitHub
parent 5f984e2826
commit 6bd1b10e4d
5 changed files with 52 additions and 23 deletions

View File

@ -57,7 +57,7 @@ class TestClass implements TestInterface
*/ */
public function testMethod($testParameter) public function testMethod($testParameter)
{ {
$testVariable = 123; $this->testProperty = $testParameter;
} }
} }

View File

@ -57,7 +57,7 @@ class TestClass implements TestInterface
*/ */
public function testMethod($testParameter) public function testMethod($testParameter)
{ {
$testVariable = 123; $this->testProperty = $testParameter;
} }
} }

View File

@ -395,29 +395,47 @@ class PhpDocument
return null; return null;
} }
// Need to resolve variable to a class // Need to resolve variable to a class
$varDef = $this->getVariableDefinition($node->var); if ($node->var->name === 'this') {
if (!isset($varDef)) { // $this resolved to the class it is contained in
return null; $n = $node;
} while ($n = $n->getAttribute('parentNode')) {
if ($varDef instanceof Node\Param) { if ($n instanceof Node\Stmt\Class_) {
if (!isset($varDef->type)) { if ($n->isAnonymous()) {
// Cannot resolve to class without a type hint return null;
// TODO: parse docblock }
return null; $name = (string)$n->namespacedName;
} break;
$name = (string)$varDef->type;
} else if ($varDef instanceof Node\Expr\Assign) {
if ($varDef->expr instanceof Node\Expr\New_) {
if (!($varDef->expr->class instanceof Node\Name)) {
// Cannot get definition of dynamic calls
return null;
} }
$name = (string)$varDef->expr->class; }
} else { if (!isset($name)) {
return null; return null;
} }
} else { } else {
return null; // Other variables resolve to their definition
$varDef = $this->getVariableDefinition($node->var);
if (!isset($varDef)) {
return null;
}
if ($varDef instanceof Node\Param) {
if (!isset($varDef->type)) {
// Cannot resolve to class without a type hint
// TODO: parse docblock
return null;
}
$name = (string)$varDef->type;
} else if ($varDef instanceof Node\Expr\Assign) {
if ($varDef->expr instanceof Node\Expr\New_) {
if (!($varDef->expr->class instanceof Node\Name)) {
// Cannot get definition of dynamic calls
return null;
}
$name = (string)$varDef->expr->class;
} else {
return null;
}
} else {
return null;
}
} }
$name .= '::' . (string)$node->name; $name .= '::' . (string)$node->name;
} else if ($parent instanceof Node\Expr\FuncCall) { } else if ($parent instanceof Node\Expr\FuncCall) {

View File

@ -110,7 +110,8 @@ abstract class ServerTestCase extends TestCase
0 => new Location($referencesUri, new Range(new Position( 9, 5), new Position( 9, 32))) 0 => new Location($referencesUri, new Range(new Position( 9, 5), new Position( 9, 32)))
], ],
'TestNamespace\\TestClass::testProperty' => [ 'TestNamespace\\TestClass::testProperty' => [
0 => new Location($referencesUri, new Range(new Position( 6, 5), new Position( 6, 23))) 0 => new Location($symbolsUri, new Range(new Position(59, 8), new Position(59, 27))), // $this->testProperty = $testParameter;
1 => new Location($referencesUri, new Range(new Position( 6, 5), new Position( 6, 23)))
], ],
'TestNamespace\\TestClass::staticTestProperty' => [ 'TestNamespace\\TestClass::staticTestProperty' => [
0 => new Location($referencesUri, new Range(new Position( 8, 5), new Position( 8, 35))) 0 => new Location($referencesUri, new Range(new Position( 8, 5), new Position( 8, 35)))
@ -145,7 +146,8 @@ abstract class ServerTestCase extends TestCase
0 => new Location($globalReferencesUri, new Range(new Position( 9, 5), new Position( 9, 32))) 0 => new Location($globalReferencesUri, new Range(new Position( 9, 5), new Position( 9, 32)))
], ],
'TestClass::testProperty' => [ 'TestClass::testProperty' => [
0 => new Location($globalReferencesUri, new Range(new Position( 6, 5), new Position( 6, 23))) 0 => new Location($globalSymbolsUri, new Range(new Position(59, 8), new Position(59, 27))), // $this->testProperty = $testParameter;
1 => new Location($globalReferencesUri, new Range(new Position( 6, 5), new Position( 6, 23)))
], ],
'TestClass::staticTestProperty' => [ 'TestClass::staticTestProperty' => [
0 => new Location($globalReferencesUri, new Range(new Position( 8, 5), new Position( 8, 35))) 0 => new Location($globalReferencesUri, new Range(new Position( 8, 5), new Position( 8, 35)))

View File

@ -115,6 +115,15 @@ class GlobalTest extends ServerTestCase
{ {
// echo $obj->testProperty; // echo $obj->testProperty;
// Get definition for testProperty // Get definition for testProperty
$reference = $this->getReferenceLocations('TestClass::testProperty')[1];
$result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals($this->getDefinitionLocation('TestClass::testProperty'), $result);
}
public function testDefinitionForPropertiesOnThis()
{
// $this->testProperty = $testParameter;
// Get definition for testProperty
$reference = $this->getReferenceLocations('TestClass::testProperty')[0]; $reference = $this->getReferenceLocations('TestClass::testProperty')[0];
$result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end); $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals($this->getDefinitionLocation('TestClass::testProperty'), $result); $this->assertEquals($this->getDefinitionLocation('TestClass::testProperty'), $result);