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)
{
$testVariable = 123;
$this->testProperty = $testParameter;
}
}

View File

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

View File

@ -395,6 +395,23 @@ class PhpDocument
return null;
}
// Need to resolve variable to a class
if ($node->var->name === 'this') {
// $this resolved to the class it is contained in
$n = $node;
while ($n = $n->getAttribute('parentNode')) {
if ($n instanceof Node\Stmt\Class_) {
if ($n->isAnonymous()) {
return null;
}
$name = (string)$n->namespacedName;
break;
}
}
if (!isset($name)) {
return null;
}
} else {
// Other variables resolve to their definition
$varDef = $this->getVariableDefinition($node->var);
if (!isset($varDef)) {
return null;
@ -419,6 +436,7 @@ class PhpDocument
} else {
return null;
}
}
$name .= '::' . (string)$node->name;
} else if ($parent instanceof Node\Expr\FuncCall) {
if ($parent->name instanceof Node\Expr) {

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)))
],
'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' => [
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)))
],
'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' => [
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;
// 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];
$result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals($this->getDefinitionLocation('TestClass::testProperty'), $result);