1
0
Fork 0

Fix variable type from method return value, add tests (#393)

pull/392/head
Rob Lourens 2017-06-10 02:10:15 -07:00 committed by Felix Becker
parent 7b72b38fd9
commit f10680e441
53 changed files with 264 additions and 61 deletions

View File

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

View File

@ -0,0 +1,12 @@
<?php
class FooClass {
public static function staticFoo(): FooClass {
return new FooClass();
}
public function bar() { }
}
$foo = FooClass::staticFoo();
$foo->

View File

@ -623,6 +623,16 @@ class DefinitionResolver
}
}
// MEMBER CALL EXPRESSION/SCOPED PROPERTY CALL EXPRESSION
// The type of the member/scoped property call expression is the type of the method, so resolve the
// type of the callable expression.
if ($expr instanceof Node\Expression\CallExpression && (
$expr->callableExpression instanceof Node\Expression\MemberAccessExpression ||
$expr->callableExpression instanceof Node\Expression\ScopedPropertyAccessExpression)
) {
return $this->resolveExpressionNodeToType($expr->callableExpression);
}
// MEMBER ACCESS EXPRESSION
if ($expr instanceof Node\Expression\MemberAccessExpression) {
if ($expr->memberName instanceof Node\Expression) {

View File

@ -57,7 +57,7 @@ class LanguageServerTest extends TestCase
if ($msg->body->method === 'window/logMessage' && $promise->state === Promise::PENDING) {
if ($msg->body->params->type === MessageType::ERROR) {
$promise->reject(new Exception($msg->body->params->message));
} else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) {
} else if (preg_match('/All \d+ PHP files parsed/', $msg->body->params->message)) {
$promise->fulfill();
}
}
@ -103,7 +103,7 @@ class LanguageServerTest extends TestCase
if ($promise->state === Promise::PENDING) {
$promise->reject(new Exception($msg->body->params->message));
}
} else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) {
} else if (preg_match('/All \d+ PHP files parsed/', $msg->body->params->message)) {
$promise->fulfill();
}
}

View File

@ -499,6 +499,50 @@ class CompletionTest extends TestCase
], true), $items);
}
public function testMethodReturnType()
{
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/method_return_type.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,
'\FooClass',
null,
null,
null,
null,
null
)
], true), $items);
}
public function testStaticMethodReturnType()
{
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static_method_return_type.php');
$this->loader->open($completionUri, file_get_contents($completionUri));
$items = $this->textDocument->completion(
new TextDocumentIdentifier($completionUri),
new Position(11, 6)
)->wait();
$this->assertCompletionsListSubset(new CompletionList([
new CompletionItem(
'bar',
CompletionItemKind::METHOD,
'mixed',
null,
null,
null,
null,
null
)
], true), $items);
}
private function assertCompletionsListSubset(CompletionList $subsetList, CompletionList $list)
{
foreach ($subsetList->items as $expectedItem) {

View File

@ -132,8 +132,7 @@ class ValidationTest extends TestCase
} elseif ($propertyName === 'extends') {
$definition->$propertyName = $definition->$propertyName ?? [];
} elseif ($propertyName === 'type' && $definition->type !== null) {
// Class info is not captured by json_encode. It's important for 'type'.
$defsForAssert[$fqn]['type__class'] = get_class($definition->type);
$defsForAssert[$fqn]['type__tostring'] = (string)$definition->type;
}
$defsForAssert[$fqn][$propertyName] = $definition->$propertyName;

View File

@ -69,7 +69,7 @@
},
"containerName": "Fixtures\\Prophecy\\WithReturnTypehints"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Object_",
"type__tostring": "\\self",
"type": {},
"declarationLine": "public function getSelf(): self {",
"documentation": null
@ -88,7 +88,7 @@
},
"containerName": "Fixtures\\Prophecy\\WithReturnTypehints"
},
"type__class": "phpDocumentor\\Reflection\\Types\\String_",
"type__tostring": "string",
"type": {},
"declarationLine": "public function getName(): string {",
"documentation": null
@ -107,7 +107,7 @@
},
"containerName": "Fixtures\\Prophecy\\WithReturnTypehints"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Object_",
"type__tostring": "\\parent",
"type": {},
"declarationLine": "public function getParent(): parent {",
"documentation": null

View File

@ -33,7 +33,7 @@
},
"containerName": "A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Array_",
"type__tostring": "string[]",
"type": {},
"declarationLine": "protected $foo;",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "TestNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Integer",
"type__tostring": "int",
"type": {},
"declarationLine": "public $a;",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "TestNamespace\\TestClass"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public $testProperty;",
"documentation": null
@ -77,7 +77,7 @@
},
"containerName": "TestNamespace\\TestClass"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function testMethod($testParameter)",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function suite()",
"documentation": null

View File

@ -55,7 +55,7 @@
},
"containerName": "MyNamespace\\Mbstring"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Object_",
"type__tostring": "\\MyNamespace\\PHP_INT_MAX",
"type": {},
"declarationLine": "const MB_CASE_FOLD = PHP_INT_MAX;",
"documentation": null

View File

@ -37,7 +37,7 @@
},
"containerName": "A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b ($a = MY_CONSTANT);",
"documentation": null

View File

@ -37,7 +37,7 @@
},
"containerName": "A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Array_",
"type__tostring": "\\__CLASS__[]",
"type": {},
"declarationLine": "private static $deprecationsTriggered;",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "static function a() {",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "static function a() {",
"documentation": null

View File

@ -73,7 +73,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function getInitializer(ClassLoader $loader)",
"documentation": null

View File

@ -64,7 +64,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function testRequest()",
"documentation": null

View File

@ -55,7 +55,7 @@
},
"containerName": "MyNamespace\\ParseErrorsTest"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function setUp()",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\ParseErrorsTest"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function setAccount(AccountInterface $account)",
"documentation": null

View File

@ -0,0 +1,7 @@
<?php
class FooClass {
public function foo(): FooClass {
return $this;
}
}

View File

@ -0,0 +1,46 @@
{
"references": {
"FooClass": [
"./methodReturnType.php"
]
},
"definitions": {
"FooClass": {
"fqn": "FooClass",
"extends": [],
"isGlobal": true,
"isStatic": false,
"canBeInstantiated": true,
"symbolInformation": {
"name": "FooClass",
"kind": 5,
"location": {
"uri": "./methodReturnType.php"
},
"containerName": ""
},
"type": null,
"declarationLine": "class FooClass {",
"documentation": null
},
"FooClass->foo()": {
"fqn": "FooClass->foo()",
"extends": [],
"isGlobal": false,
"isStatic": false,
"canBeInstantiated": false,
"symbolInformation": {
"name": "foo",
"kind": 6,
"location": {
"uri": "./methodReturnType.php"
},
"containerName": "FooClass"
},
"type__tostring": "\\FooClass",
"type": {},
"declarationLine": "public function foo(): FooClass {",
"documentation": null
}
}
}

View File

@ -64,7 +64,7 @@
},
"containerName": "MyNamespace1\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -121,7 +121,7 @@
},
"containerName": "MyNamespace2\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -33,7 +33,7 @@
},
"containerName": "Foo"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Object_",
"type__tostring": "\\Iterator",
"type": {},
"declarationLine": "public function fn()",
"documentation": "Foo"

View File

@ -33,7 +33,7 @@
},
"containerName": "A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null

View File

@ -55,7 +55,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -76,7 +76,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -37,7 +37,7 @@
},
"containerName": "A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -37,7 +37,7 @@
},
"containerName": "MyNamespace"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function init(Hi $view)",
"documentation": null

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -97,7 +97,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -33,7 +33,7 @@
},
"containerName": "MyClass"
},
"type__class": "phpDocumentor\\Reflection\\Types\\String_",
"type__tostring": "string",
"type": {},
"declarationLine": "protected $mainPropertyName;",
"documentation": "The name of the main property, or NULL if there is none."

View File

@ -33,7 +33,7 @@
},
"containerName": "MyClass"
},
"type__class": "phpDocumentor\\Reflection\\Types\\String_",
"type__tostring": "string",
"type": {},
"declarationLine": "protected $mainPropertyName;",
"documentation": "The name of the main property, or NULL if there is none."

View File

@ -40,7 +40,7 @@
},
"containerName": "TestNamespace"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Object_",
"type__tostring": "\\TestNamespace\\TestClass",
"type": {},
"declarationLine": "function whatever(TestClass $param): TestClass2 {",
"documentation": "Aute duis elit reprehenderit tempor cillum proident anim laborum eu laboris reprehenderit ea incididunt."

View File

@ -58,7 +58,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "static function a() {",
"documentation": null

View File

@ -40,7 +40,7 @@
},
"containerName": "A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\String_",
"type__tostring": "string",
"type": {},
"declarationLine": "static $a;",
"documentation": null

View File

@ -46,7 +46,7 @@
},
"containerName": "TestClass"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Array_",
"type__tostring": "\\TestClass[]",
"type": {},
"declarationLine": "public static $testProperty;",
"documentation": "Lorem excepteur officia sit anim velit veniam enim."

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -70,7 +70,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"documentation": null

View File

@ -55,7 +55,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function typesProvider()",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -61,7 +61,7 @@
},
"containerName": "MyNamespace\\B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b() {",
"documentation": null
@ -100,7 +100,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -60,7 +60,7 @@
},
"containerName": "MyNamespace\\A"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

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

View File

@ -0,0 +1,65 @@
{
"references": {
"FooClass": [
"./staticMethodReturnType.php"
]
},
"definitions": {
"FooClass": {
"fqn": "FooClass",
"extends": [],
"isGlobal": true,
"isStatic": false,
"canBeInstantiated": true,
"symbolInformation": {
"name": "FooClass",
"kind": 5,
"location": {
"uri": "./staticMethodReturnType.php"
},
"containerName": ""
},
"type": null,
"declarationLine": "class FooClass {",
"documentation": null
},
"FooClass::staticFoo()": {
"fqn": "FooClass::staticFoo()",
"extends": [],
"isGlobal": false,
"isStatic": true,
"canBeInstantiated": false,
"symbolInformation": {
"name": "staticFoo",
"kind": 6,
"location": {
"uri": "./staticMethodReturnType.php"
},
"containerName": "FooClass"
},
"type__tostring": "\\FooClass",
"type": {},
"declarationLine": "public static function staticFoo(): FooClass {",
"documentation": null
},
"FooClass->bar()": {
"fqn": "FooClass->bar()",
"extends": [],
"isGlobal": false,
"isStatic": false,
"canBeInstantiated": false,
"symbolInformation": {
"name": "bar",
"kind": 6,
"location": {
"uri": "./staticMethodReturnType.php"
},
"containerName": "FooClass"
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function bar() { }",
"documentation": null
}
}
}

View File

@ -33,7 +33,7 @@
},
"containerName": "B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Integer",
"type__tostring": "int",
"type": {},
"declarationLine": "public $hi;",
"documentation": null
@ -52,7 +52,7 @@
},
"containerName": "B"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "function a () {",
"documentation": null

View File

@ -40,7 +40,7 @@
},
"containerName": "Foo"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Object_",
"type__tostring": "\\",
"type": {},
"declarationLine": "protected $bar;",
"documentation": null
@ -59,7 +59,7 @@
},
"containerName": "Foo"
},
"type__class": "phpDocumentor\\Reflection\\Types\\Mixed",
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function foo () {",
"documentation": null