From 7b72b38fd9533b3daedd5af17eeab3a6b14a3e5e Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Sat, 10 Jun 2017 01:55:41 -0700 Subject: [PATCH 1/6] Assert that references array is equal, not a subset, and update expected.json files (#395) --- tests/Validation/ValidationTest.php | 7 +------ .../cases/WithReturnTypehints.php.expected.json | 6 ++++++ tests/Validation/cases/exceptions1.php.expected.json | 6 +++++- tests/Validation/cases/functionUse2.php.expected.json | 3 +++ .../cases/magicConstantsShouldBeGlobal.php.expected.json | 9 ++++++++- tests/Validation/cases/magicConsts.php.expected.json | 6 +++++- tests/Validation/cases/memberAccess5.php.expected.json | 6 +++++- tests/Validation/cases/namespaces8.php.expected.json | 6 ++++++ tests/Validation/cases/self4.php.expected.json | 6 ++++++ 9 files changed, 45 insertions(+), 10 deletions(-) diff --git a/tests/Validation/ValidationTest.php b/tests/Validation/ValidationTest.php index 649e947..8fa7442 100644 --- a/tests/Validation/ValidationTest.php +++ b/tests/Validation/ValidationTest.php @@ -64,12 +64,7 @@ class ValidationTest extends TestCase try { $this->assertEquals($expectedValues['definitions'], $actualValues['definitions']); - - try { - $this->assertArraySubset((array)$expectedValues['references'], (array)$actualValues['references'], false, 'references don\'t match.'); - } catch (\Throwable $e) { - $this->assertEquals((array)$expectedValues['references'], (array)$actualValues['references'], 'references don\'t match.'); - } + $this->assertEquals((array)$expectedValues['references'], (array)$actualValues['references'], 'references don\'t match.'); } catch (\Throwable $e) { $outputFile = getExpectedValuesFile($testCaseFile); file_put_contents($outputFile, json_encode($actualValues, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); diff --git a/tests/Validation/cases/WithReturnTypehints.php.expected.json b/tests/Validation/cases/WithReturnTypehints.php.expected.json index 50801d9..166786a 100644 --- a/tests/Validation/cases/WithReturnTypehints.php.expected.json +++ b/tests/Validation/cases/WithReturnTypehints.php.expected.json @@ -6,6 +6,12 @@ "self": [ "./WithReturnTypehints.php" ], + "Fixtures\\Prophecy\\__CLASS__": [ + "./WithReturnTypehints.php" + ], + "__CLASS__": [ + "./WithReturnTypehints.php" + ], "parent": [ "./WithReturnTypehints.php" ] diff --git a/tests/Validation/cases/exceptions1.php.expected.json b/tests/Validation/cases/exceptions1.php.expected.json index d74deff..4a13354 100644 --- a/tests/Validation/cases/exceptions1.php.expected.json +++ b/tests/Validation/cases/exceptions1.php.expected.json @@ -1,5 +1,9 @@ { - "references": [], + "references": { + "MyNamespace\\Exception": [ + "./exceptions1.php" + ] + }, "definitions": { "MyNamespace": { "fqn": "MyNamespace", diff --git a/tests/Validation/cases/functionUse2.php.expected.json b/tests/Validation/cases/functionUse2.php.expected.json index f3609bd..320cc41 100644 --- a/tests/Validation/cases/functionUse2.php.expected.json +++ b/tests/Validation/cases/functionUse2.php.expected.json @@ -3,6 +3,9 @@ "LanguageServer": [ "./functionUse2.php" ], + "LanguageServer\\pathToUri()": [ + "./functionUse2.php" + ], "LanguageServer\\timeout()": [ "./functionUse2.php" ] diff --git a/tests/Validation/cases/magicConstantsShouldBeGlobal.php.expected.json b/tests/Validation/cases/magicConstantsShouldBeGlobal.php.expected.json index 36a2942..b61333f 100644 --- a/tests/Validation/cases/magicConstantsShouldBeGlobal.php.expected.json +++ b/tests/Validation/cases/magicConstantsShouldBeGlobal.php.expected.json @@ -1,5 +1,12 @@ { - "references": [], + "references": { + "B\\__FILE__": [ + "./magicConstantsShouldBeGlobal.php" + ], + "__FILE__": [ + "./magicConstantsShouldBeGlobal.php" + ] + }, "definitions": { "B": { "fqn": "B", diff --git a/tests/Validation/cases/magicConsts.php.expected.json b/tests/Validation/cases/magicConsts.php.expected.json index c017d49..6641af3 100644 --- a/tests/Validation/cases/magicConsts.php.expected.json +++ b/tests/Validation/cases/magicConsts.php.expected.json @@ -1,5 +1,9 @@ { - "references": [], + "references": { + "__CLASS__": [ + "./magicConsts.php" + ] + }, "definitions": { "A": { "fqn": "A", diff --git a/tests/Validation/cases/memberAccess5.php.expected.json b/tests/Validation/cases/memberAccess5.php.expected.json index ae2b4ac..5023cd6 100644 --- a/tests/Validation/cases/memberAccess5.php.expected.json +++ b/tests/Validation/cases/memberAccess5.php.expected.json @@ -1,5 +1,9 @@ { - "references": [], + "references": { + "MyNamespace\\ParseErrorsTest->args": [ + "./memberAccess5.php" + ] + }, "definitions": { "MyNamespace": { "fqn": "MyNamespace", diff --git a/tests/Validation/cases/namespaces8.php.expected.json b/tests/Validation/cases/namespaces8.php.expected.json index 2fbd1fb..7a77f7c 100644 --- a/tests/Validation/cases/namespaces8.php.expected.json +++ b/tests/Validation/cases/namespaces8.php.expected.json @@ -2,6 +2,12 @@ "references": { "LanguageServer": [ "./namespaces8.php" + ], + "LanguageServer\\pathToUri()": [ + "./namespaces8.php" + ], + "LanguageServer\\uriToPath()": [ + "./namespaces8.php" ] }, "definitions": { diff --git a/tests/Validation/cases/self4.php.expected.json b/tests/Validation/cases/self4.php.expected.json index 5fac53f..9a01912 100644 --- a/tests/Validation/cases/self4.php.expected.json +++ b/tests/Validation/cases/self4.php.expected.json @@ -6,6 +6,12 @@ "MyNamespace\\A->addTestFile()": [ "./self4.php" ], + "MyNamespace\\__DIR__": [ + "./self4.php" + ], + "__DIR__": [ + "./self4.php" + ], "MyNamespace\\DS": [ "./self4.php" ], From f10680e4419758e8986393c57cc78a7d9f2cbbbe Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Sat, 10 Jun 2017 02:10:15 -0700 Subject: [PATCH 2/6] Fix variable type from method return value, add tests (#393) --- fixtures/completion/method_return_type.php | 11 ++++ .../completion/static_method_return_type.php | 12 ++++ src/DefinitionResolver.php | 10 +++ tests/LanguageServerTest.php | 4 +- tests/Server/TextDocument/CompletionTest.php | 44 +++++++++++++ tests/Validation/ValidationTest.php | 3 +- .../WithReturnTypehints.php.expected.json | 6 +- ...rrayValueShouldBeBoolean.php.expected.json | 2 +- .../cases/classDefinition1.php.expected.json | 2 +- .../cases/classProperty1.php.expected.json | 4 +- .../cases/constants.php.expected.json | 2 +- .../cases/constants2.php.expected.json | 2 +- .../cases/constants3.php.expected.json | 2 +- .../cases/constants4.php.expected.json | 2 +- .../cases/constants5.php.expected.json | 2 +- ...tsInFunctionParamDefault.php.expected.json | 2 +- .../cases/magicConsts.php.expected.json | 2 +- .../cases/memberAccess1.php.expected.json | 2 +- .../cases/memberAccess2.php.expected.json | 2 +- .../cases/memberAccess3.php.expected.json | 2 +- .../cases/memberAccess4.php.expected.json | 2 +- .../cases/memberAccess5.php.expected.json | 2 +- .../cases/memberCall1.php.expected.json | 2 +- tests/Validation/cases/methodReturnType.php | 7 ++ .../cases/methodReturnType.php.expected.json | 46 +++++++++++++ .../multipleNamespaces.php.expected.json | 4 +- ...ltiplePreceedingComments.php.expected.json | 2 +- .../cases/nameToken.php.expected.json | 2 +- .../cases/objectCreation.php.expected.json | 2 +- .../cases/objectCreation2.php.expected.json | 2 +- .../cases/objectCreation3.php.expected.json | 2 +- .../Validation/cases/param1.php.expected.json | 2 +- .../cases/parent1.php.expected.json | 4 +- .../cases/parent3.php.expected.json | 4 +- .../cases/propertyName1.php.expected.json | 2 +- .../cases/propertyName2.php.expected.json | 2 +- .../cases/returnType.php.expected.json | 2 +- .../scopedPropertyAccess.php.expected.json | 2 +- .../scopedPropertyAccess3.php.expected.json | 2 +- .../scopedPropertyAccess5.php.expected.json | 2 +- .../Validation/cases/self1.php.expected.json | 4 +- .../Validation/cases/self2.php.expected.json | 4 +- .../Validation/cases/self3.php.expected.json | 4 +- .../Validation/cases/self4.php.expected.json | 2 +- .../Validation/cases/self5.php.expected.json | 2 +- .../cases/static1.php.expected.json | 4 +- .../cases/static2.php.expected.json | 4 +- .../cases/static3.php.expected.json | 4 +- .../cases/static4.php.expected.json | 2 +- .../cases/staticMethodReturnType.php | 9 +++ .../staticMethodReturnType.php.expected.json | 65 +++++++++++++++++++ .../cases/stringVariable.php.expected.json | 4 +- ...rifyFqsenOnClassProperty.php.expected.json | 4 +- 53 files changed, 264 insertions(+), 61 deletions(-) create mode 100644 fixtures/completion/method_return_type.php create mode 100644 fixtures/completion/static_method_return_type.php create mode 100644 tests/Validation/cases/methodReturnType.php create mode 100644 tests/Validation/cases/methodReturnType.php.expected.json create mode 100644 tests/Validation/cases/staticMethodReturnType.php create mode 100644 tests/Validation/cases/staticMethodReturnType.php.expected.json diff --git a/fixtures/completion/method_return_type.php b/fixtures/completion/method_return_type.php new file mode 100644 index 0000000..b168f65 --- /dev/null +++ b/fixtures/completion/method_return_type.php @@ -0,0 +1,11 @@ +foo(); +$foo-> \ No newline at end of file diff --git a/fixtures/completion/static_method_return_type.php b/fixtures/completion/static_method_return_type.php new file mode 100644 index 0000000..06cafdd --- /dev/null +++ b/fixtures/completion/static_method_return_type.php @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index cf2f8a6..32defab 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -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) { diff --git a/tests/LanguageServerTest.php b/tests/LanguageServerTest.php index fb52ef6..5d03451 100644 --- a/tests/LanguageServerTest.php +++ b/tests/LanguageServerTest.php @@ -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(); } } diff --git a/tests/Server/TextDocument/CompletionTest.php b/tests/Server/TextDocument/CompletionTest.php index 03ba3d8..923acc1 100644 --- a/tests/Server/TextDocument/CompletionTest.php +++ b/tests/Server/TextDocument/CompletionTest.php @@ -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) { diff --git a/tests/Validation/ValidationTest.php b/tests/Validation/ValidationTest.php index 8fa7442..3fc7429 100644 --- a/tests/Validation/ValidationTest.php +++ b/tests/Validation/ValidationTest.php @@ -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; diff --git a/tests/Validation/cases/WithReturnTypehints.php.expected.json b/tests/Validation/cases/WithReturnTypehints.php.expected.json index 166786a..271c203 100644 --- a/tests/Validation/cases/WithReturnTypehints.php.expected.json +++ b/tests/Validation/cases/WithReturnTypehints.php.expected.json @@ -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 diff --git a/tests/Validation/cases/arrayValueShouldBeBoolean.php.expected.json b/tests/Validation/cases/arrayValueShouldBeBoolean.php.expected.json index c9b02d0..1bb66e1 100644 --- a/tests/Validation/cases/arrayValueShouldBeBoolean.php.expected.json +++ b/tests/Validation/cases/arrayValueShouldBeBoolean.php.expected.json @@ -33,7 +33,7 @@ }, "containerName": "A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Array_", + "type__tostring": "string[]", "type": {}, "declarationLine": "protected $foo;", "documentation": null diff --git a/tests/Validation/cases/classDefinition1.php.expected.json b/tests/Validation/cases/classDefinition1.php.expected.json index aad36bb..5167a4c 100644 --- a/tests/Validation/cases/classDefinition1.php.expected.json +++ b/tests/Validation/cases/classDefinition1.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "TestNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Integer", + "type__tostring": "int", "type": {}, "declarationLine": "public $a;", "documentation": null diff --git a/tests/Validation/cases/classProperty1.php.expected.json b/tests/Validation/cases/classProperty1.php.expected.json index 671c019..05d90c1 100644 --- a/tests/Validation/cases/classProperty1.php.expected.json +++ b/tests/Validation/cases/classProperty1.php.expected.json @@ -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 diff --git a/tests/Validation/cases/constants.php.expected.json b/tests/Validation/cases/constants.php.expected.json index eef5cdd..ba80e25 100644 --- a/tests/Validation/cases/constants.php.expected.json +++ b/tests/Validation/cases/constants.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public static function suite()", "documentation": null diff --git a/tests/Validation/cases/constants2.php.expected.json b/tests/Validation/cases/constants2.php.expected.json index 6ecb2a6..e08408e 100644 --- a/tests/Validation/cases/constants2.php.expected.json +++ b/tests/Validation/cases/constants2.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public static function suite()", "documentation": null diff --git a/tests/Validation/cases/constants3.php.expected.json b/tests/Validation/cases/constants3.php.expected.json index d49903c..dbbb959 100644 --- a/tests/Validation/cases/constants3.php.expected.json +++ b/tests/Validation/cases/constants3.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public static function suite()", "documentation": null diff --git a/tests/Validation/cases/constants4.php.expected.json b/tests/Validation/cases/constants4.php.expected.json index 2c01864..24523a8 100644 --- a/tests/Validation/cases/constants4.php.expected.json +++ b/tests/Validation/cases/constants4.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public function suite()", "documentation": null diff --git a/tests/Validation/cases/constants5.php.expected.json b/tests/Validation/cases/constants5.php.expected.json index a0876b5..1498165 100644 --- a/tests/Validation/cases/constants5.php.expected.json +++ b/tests/Validation/cases/constants5.php.expected.json @@ -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 diff --git a/tests/Validation/cases/constantsInFunctionParamDefault.php.expected.json b/tests/Validation/cases/constantsInFunctionParamDefault.php.expected.json index 98aa7cd..3ff0ca1 100644 --- a/tests/Validation/cases/constantsInFunctionParamDefault.php.expected.json +++ b/tests/Validation/cases/constantsInFunctionParamDefault.php.expected.json @@ -37,7 +37,7 @@ }, "containerName": "A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function b ($a = MY_CONSTANT);", "documentation": null diff --git a/tests/Validation/cases/magicConsts.php.expected.json b/tests/Validation/cases/magicConsts.php.expected.json index 6641af3..74cf36b 100644 --- a/tests/Validation/cases/magicConsts.php.expected.json +++ b/tests/Validation/cases/magicConsts.php.expected.json @@ -37,7 +37,7 @@ }, "containerName": "A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Array_", + "type__tostring": "\\__CLASS__[]", "type": {}, "declarationLine": "private static $deprecationsTriggered;", "documentation": null diff --git a/tests/Validation/cases/memberAccess1.php.expected.json b/tests/Validation/cases/memberAccess1.php.expected.json index ff4868b..efe4d3a 100644 --- a/tests/Validation/cases/memberAccess1.php.expected.json +++ b/tests/Validation/cases/memberAccess1.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "static function a() {", "documentation": null diff --git a/tests/Validation/cases/memberAccess2.php.expected.json b/tests/Validation/cases/memberAccess2.php.expected.json index a6707d0..1725a5b 100644 --- a/tests/Validation/cases/memberAccess2.php.expected.json +++ b/tests/Validation/cases/memberAccess2.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "static function a() {", "documentation": null diff --git a/tests/Validation/cases/memberAccess3.php.expected.json b/tests/Validation/cases/memberAccess3.php.expected.json index df58d1e..9b1b4ee 100644 --- a/tests/Validation/cases/memberAccess3.php.expected.json +++ b/tests/Validation/cases/memberAccess3.php.expected.json @@ -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 diff --git a/tests/Validation/cases/memberAccess4.php.expected.json b/tests/Validation/cases/memberAccess4.php.expected.json index 1cfabb3..3e26d72 100644 --- a/tests/Validation/cases/memberAccess4.php.expected.json +++ b/tests/Validation/cases/memberAccess4.php.expected.json @@ -64,7 +64,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public function testRequest()", "documentation": null diff --git a/tests/Validation/cases/memberAccess5.php.expected.json b/tests/Validation/cases/memberAccess5.php.expected.json index 5023cd6..c7158b6 100644 --- a/tests/Validation/cases/memberAccess5.php.expected.json +++ b/tests/Validation/cases/memberAccess5.php.expected.json @@ -55,7 +55,7 @@ }, "containerName": "MyNamespace\\ParseErrorsTest" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public function setUp()", "documentation": null diff --git a/tests/Validation/cases/memberCall1.php.expected.json b/tests/Validation/cases/memberCall1.php.expected.json index 4cf2cd8..bd31b2f 100644 --- a/tests/Validation/cases/memberCall1.php.expected.json +++ b/tests/Validation/cases/memberCall1.php.expected.json @@ -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 diff --git a/tests/Validation/cases/methodReturnType.php b/tests/Validation/cases/methodReturnType.php new file mode 100644 index 0000000..b4b937d --- /dev/null +++ b/tests/Validation/cases/methodReturnType.php @@ -0,0 +1,7 @@ +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 + } + } +} \ No newline at end of file diff --git a/tests/Validation/cases/multipleNamespaces.php.expected.json b/tests/Validation/cases/multipleNamespaces.php.expected.json index 2ed59de..3533e8c 100644 --- a/tests/Validation/cases/multipleNamespaces.php.expected.json +++ b/tests/Validation/cases/multipleNamespaces.php.expected.json @@ -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 diff --git a/tests/Validation/cases/multiplePreceedingComments.php.expected.json b/tests/Validation/cases/multiplePreceedingComments.php.expected.json index ca97671..c331b5e 100644 --- a/tests/Validation/cases/multiplePreceedingComments.php.expected.json +++ b/tests/Validation/cases/multiplePreceedingComments.php.expected.json @@ -33,7 +33,7 @@ }, "containerName": "Foo" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Object_", + "type__tostring": "\\Iterator", "type": {}, "declarationLine": "public function fn()", "documentation": "Foo" diff --git a/tests/Validation/cases/nameToken.php.expected.json b/tests/Validation/cases/nameToken.php.expected.json index 1bd944d..af73f78 100644 --- a/tests/Validation/cases/nameToken.php.expected.json +++ b/tests/Validation/cases/nameToken.php.expected.json @@ -33,7 +33,7 @@ }, "containerName": "A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function b() {", "documentation": null diff --git a/tests/Validation/cases/objectCreation.php.expected.json b/tests/Validation/cases/objectCreation.php.expected.json index cea0343..a0e8f72 100644 --- a/tests/Validation/cases/objectCreation.php.expected.json +++ b/tests/Validation/cases/objectCreation.php.expected.json @@ -55,7 +55,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function a () {", "documentation": null diff --git a/tests/Validation/cases/objectCreation2.php.expected.json b/tests/Validation/cases/objectCreation2.php.expected.json index 7f856b1..0119bf7 100644 --- a/tests/Validation/cases/objectCreation2.php.expected.json +++ b/tests/Validation/cases/objectCreation2.php.expected.json @@ -76,7 +76,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function a () {", "documentation": null diff --git a/tests/Validation/cases/objectCreation3.php.expected.json b/tests/Validation/cases/objectCreation3.php.expected.json index c6dcab9..75cc011 100644 --- a/tests/Validation/cases/objectCreation3.php.expected.json +++ b/tests/Validation/cases/objectCreation3.php.expected.json @@ -37,7 +37,7 @@ }, "containerName": "A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function a () {", "documentation": null diff --git a/tests/Validation/cases/param1.php.expected.json b/tests/Validation/cases/param1.php.expected.json index adbe002..ee952f3 100644 --- a/tests/Validation/cases/param1.php.expected.json +++ b/tests/Validation/cases/param1.php.expected.json @@ -37,7 +37,7 @@ }, "containerName": "MyNamespace" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function init(Hi $view)", "documentation": null diff --git a/tests/Validation/cases/parent1.php.expected.json b/tests/Validation/cases/parent1.php.expected.json index 3391cd4..961b35b 100644 --- a/tests/Validation/cases/parent1.php.expected.json +++ b/tests/Validation/cases/parent1.php.expected.json @@ -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 diff --git a/tests/Validation/cases/parent3.php.expected.json b/tests/Validation/cases/parent3.php.expected.json index 2c4915d..e2cf2c6 100644 --- a/tests/Validation/cases/parent3.php.expected.json +++ b/tests/Validation/cases/parent3.php.expected.json @@ -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 diff --git a/tests/Validation/cases/propertyName1.php.expected.json b/tests/Validation/cases/propertyName1.php.expected.json index 8032ecc..0a37c20 100644 --- a/tests/Validation/cases/propertyName1.php.expected.json +++ b/tests/Validation/cases/propertyName1.php.expected.json @@ -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." diff --git a/tests/Validation/cases/propertyName2.php.expected.json b/tests/Validation/cases/propertyName2.php.expected.json index a70c515..9cba945 100644 --- a/tests/Validation/cases/propertyName2.php.expected.json +++ b/tests/Validation/cases/propertyName2.php.expected.json @@ -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." diff --git a/tests/Validation/cases/returnType.php.expected.json b/tests/Validation/cases/returnType.php.expected.json index 9515a6b..6ddca7e 100644 --- a/tests/Validation/cases/returnType.php.expected.json +++ b/tests/Validation/cases/returnType.php.expected.json @@ -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." diff --git a/tests/Validation/cases/scopedPropertyAccess.php.expected.json b/tests/Validation/cases/scopedPropertyAccess.php.expected.json index 6797a0e..3eeda77 100644 --- a/tests/Validation/cases/scopedPropertyAccess.php.expected.json +++ b/tests/Validation/cases/scopedPropertyAccess.php.expected.json @@ -58,7 +58,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "static function a() {", "documentation": null diff --git a/tests/Validation/cases/scopedPropertyAccess3.php.expected.json b/tests/Validation/cases/scopedPropertyAccess3.php.expected.json index d29387a..81cbfb6 100644 --- a/tests/Validation/cases/scopedPropertyAccess3.php.expected.json +++ b/tests/Validation/cases/scopedPropertyAccess3.php.expected.json @@ -40,7 +40,7 @@ }, "containerName": "A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\String_", + "type__tostring": "string", "type": {}, "declarationLine": "static $a;", "documentation": null diff --git a/tests/Validation/cases/scopedPropertyAccess5.php.expected.json b/tests/Validation/cases/scopedPropertyAccess5.php.expected.json index ab1e214..27f0509 100644 --- a/tests/Validation/cases/scopedPropertyAccess5.php.expected.json +++ b/tests/Validation/cases/scopedPropertyAccess5.php.expected.json @@ -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." diff --git a/tests/Validation/cases/self1.php.expected.json b/tests/Validation/cases/self1.php.expected.json index 6531b53..41525d8 100644 --- a/tests/Validation/cases/self1.php.expected.json +++ b/tests/Validation/cases/self1.php.expected.json @@ -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 diff --git a/tests/Validation/cases/self2.php.expected.json b/tests/Validation/cases/self2.php.expected.json index 8300de2..eb31aba 100644 --- a/tests/Validation/cases/self2.php.expected.json +++ b/tests/Validation/cases/self2.php.expected.json @@ -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 diff --git a/tests/Validation/cases/self3.php.expected.json b/tests/Validation/cases/self3.php.expected.json index 3d5d4ad..f50b80c 100644 --- a/tests/Validation/cases/self3.php.expected.json +++ b/tests/Validation/cases/self3.php.expected.json @@ -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 diff --git a/tests/Validation/cases/self4.php.expected.json b/tests/Validation/cases/self4.php.expected.json index 9a01912..4946001 100644 --- a/tests/Validation/cases/self4.php.expected.json +++ b/tests/Validation/cases/self4.php.expected.json @@ -70,7 +70,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public static function suite()", "documentation": null diff --git a/tests/Validation/cases/self5.php.expected.json b/tests/Validation/cases/self5.php.expected.json index 781cd24..e1c99af 100644 --- a/tests/Validation/cases/self5.php.expected.json +++ b/tests/Validation/cases/self5.php.expected.json @@ -55,7 +55,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "public function typesProvider()", "documentation": null diff --git a/tests/Validation/cases/static1.php.expected.json b/tests/Validation/cases/static1.php.expected.json index b0323b2..ca49245 100644 --- a/tests/Validation/cases/static1.php.expected.json +++ b/tests/Validation/cases/static1.php.expected.json @@ -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 diff --git a/tests/Validation/cases/static2.php.expected.json b/tests/Validation/cases/static2.php.expected.json index c8d81fb..06a0627 100644 --- a/tests/Validation/cases/static2.php.expected.json +++ b/tests/Validation/cases/static2.php.expected.json @@ -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 diff --git a/tests/Validation/cases/static3.php.expected.json b/tests/Validation/cases/static3.php.expected.json index bb556bf..745bd56 100644 --- a/tests/Validation/cases/static3.php.expected.json +++ b/tests/Validation/cases/static3.php.expected.json @@ -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 diff --git a/tests/Validation/cases/static4.php.expected.json b/tests/Validation/cases/static4.php.expected.json index 6a28028..67c677a 100644 --- a/tests/Validation/cases/static4.php.expected.json +++ b/tests/Validation/cases/static4.php.expected.json @@ -60,7 +60,7 @@ }, "containerName": "MyNamespace\\A" }, - "type__class": "phpDocumentor\\Reflection\\Types\\Mixed", + "type__tostring": "mixed", "type": {}, "declarationLine": "function a () {", "documentation": null diff --git a/tests/Validation/cases/staticMethodReturnType.php b/tests/Validation/cases/staticMethodReturnType.php new file mode 100644 index 0000000..325738a --- /dev/null +++ b/tests/Validation/cases/staticMethodReturnType.php @@ -0,0 +1,9 @@ +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 + } + } +} \ No newline at end of file diff --git a/tests/Validation/cases/stringVariable.php.expected.json b/tests/Validation/cases/stringVariable.php.expected.json index 5ac910b..982dba5 100644 --- a/tests/Validation/cases/stringVariable.php.expected.json +++ b/tests/Validation/cases/stringVariable.php.expected.json @@ -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 diff --git a/tests/Validation/cases/verifyFqsenOnClassProperty.php.expected.json b/tests/Validation/cases/verifyFqsenOnClassProperty.php.expected.json index f072722..d40ef63 100644 --- a/tests/Validation/cases/verifyFqsenOnClassProperty.php.expected.json +++ b/tests/Validation/cases/verifyFqsenOnClassProperty.php.expected.json @@ -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 From cc3f0da21ae5e83402652acae417cf8aa9939dbb Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Sat, 10 Jun 2017 11:37:39 +0200 Subject: [PATCH 3/6] Fix 'find references' for unused symbols (#392) * Add tests for unused symbols * Fix tests for unused symbols --- fixtures/global_symbols.php | 12 ++++++ tests/Server/ServerTestCase.php | 3 ++ .../TextDocument/References/GlobalTest.php | 39 +++++++++++++++++++ tests/Server/Workspace/SymbolTest.php | 6 ++- 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/fixtures/global_symbols.php b/fixtures/global_symbols.php index ac93b68..25b928b 100644 --- a/fixtures/global_symbols.php +++ b/fixtures/global_symbols.php @@ -105,3 +105,15 @@ class ChildClass extends TestClass {} define('TEST_DEFINE_CONSTANT', false); print TEST_DEFINE_CONSTANT ? 'true' : 'false'; + +/** + * Neither this class nor its members are referenced anywhere + */ +class UnusedClass +{ + public $unusedProperty; + + public function unusedMethod() + { + } +} diff --git a/tests/Server/ServerTestCase.php b/tests/Server/ServerTestCase.php index d8203c3..04e5976 100644 --- a/tests/Server/ServerTestCase.php +++ b/tests/Server/ServerTestCase.php @@ -85,6 +85,9 @@ abstract class ServerTestCase extends TestCase 'TestClass::staticTestMethod()' => new Location($globalSymbolsUri, new Range(new Position(46, 4), new Position(49, 5))), 'TestClass::testMethod()' => new Location($globalSymbolsUri, new Range(new Position(57, 4), new Position(60, 5))), 'test_function()' => new Location($globalSymbolsUri, new Range(new Position(78, 0), new Position(81, 1))), + 'UnusedClass' => new Location($globalSymbolsUri, new Range(new Position(111, 0), new Position(118, 1))), + 'UnusedClass::unusedProperty' => new Location($globalSymbolsUri, new Range(new Position(113,11), new Position(113, 26))), + 'UnusedClass::unusedMethod' => new Location($globalSymbolsUri, new Range(new Position(115, 4), new Position(117, 5))), 'whatever()' => new Location($globalReferencesUri, new Range(new Position(21, 0), new Position(23, 1))), // Namespaced diff --git a/tests/Server/TextDocument/References/GlobalTest.php b/tests/Server/TextDocument/References/GlobalTest.php index 4febaf3..fcb6e71 100644 --- a/tests/Server/TextDocument/References/GlobalTest.php +++ b/tests/Server/TextDocument/References/GlobalTest.php @@ -159,4 +159,43 @@ class GlobalTest extends ServerTestCase )->wait(); $this->assertEquals($this->getReferenceLocations('TestClass'), $result); } + + public function testReferencesForUnusedClass() + { + // class UnusedClass + // Get references for UnusedClass + $symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/global_symbols.php')); + $result = $this->textDocument->references( + new ReferenceContext, + new TextDocumentIdentifier($symbolsUri), + new Position(111, 10) + )->wait(); + $this->assertEquals([], $result); + } + + public function testReferencesForUnusedProperty() + { + // public $unusedProperty + // Get references for unusedProperty + $symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/global_symbols.php')); + $result = $this->textDocument->references( + new ReferenceContext, + new TextDocumentIdentifier($symbolsUri), + new Position(113, 18) + )->wait(); + $this->assertEquals([], $result); + } + + public function testReferencesForUnusedMethod() + { + // public function unusedMethod() + // Get references for unusedMethod + $symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/global_symbols.php')); + $result = $this->textDocument->references( + new ReferenceContext, + new TextDocumentIdentifier($symbolsUri), + new Position(115, 26) + )->wait(); + $this->assertEquals([], $result); + } } diff --git a/tests/Server/Workspace/SymbolTest.php b/tests/Server/Workspace/SymbolTest.php index 8f2680d..765841b 100644 --- a/tests/Server/Workspace/SymbolTest.php +++ b/tests/Server/Workspace/SymbolTest.php @@ -27,6 +27,7 @@ class SymbolTest extends ServerTestCase // Request symbols $result = $this->workspace->symbol('')->wait(); $referencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php')); + // @codingStandardsIgnoreStart $this->assertEquals([ new SymbolInformation('TestNamespace', SymbolKind::NAMESPACE, new Location($referencesUri, new Range(new Position(2, 0), new Position(2, 24))), ''), @@ -59,9 +60,12 @@ class SymbolTest extends ServerTestCase new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('test_function()'), ''), new SymbolInformation('ChildClass', SymbolKind::CLASS_, $this->getDefinitionLocation('ChildClass'), ''), new SymbolInformation('TEST_DEFINE_CONSTANT', SymbolKind::CONSTANT, $this->getDefinitionLocation('TEST_DEFINE_CONSTANT'), ''), + new SymbolInformation('UnusedClass', SymbolKind::CLASS_, $this->getDefinitionLocation('UnusedClass'), ''), + new SymbolInformation('unusedProperty', SymbolKind::PROPERTY, $this->getDefinitionLocation('UnusedClass::unusedProperty'), 'UnusedClass'), + new SymbolInformation('unusedMethod', SymbolKind::METHOD, $this->getDefinitionLocation('UnusedClass::unusedMethod'), 'UnusedClass'), new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('whatever()'), ''), - new SymbolInformation('SecondTestNamespace', SymbolKind::NAMESPACE, $this->getDefinitionLocation('SecondTestNamespace'), '') + new SymbolInformation('SecondTestNamespace', SymbolKind::NAMESPACE, $this->getDefinitionLocation('SecondTestNamespace'), ''), ], $result); // @codingStandardsIgnoreEnd } From 4c1d7bd1bc82652a97fb9c7a1b87c24e43691d5c Mon Sep 17 00:00:00 2001 From: Jens Hausdorf Date: Sat, 10 Jun 2017 18:47:19 +0200 Subject: [PATCH 4/6] Add true, false, null to keywords (#396) --- src/CompletionProvider.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index 160798d..0c160aa 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -49,6 +49,7 @@ class CompletionProvider 'eval', 'exit', 'extends', + 'false', 'final', 'finally', 'for', @@ -67,6 +68,7 @@ class CompletionProvider 'list', 'namespace', 'new', + 'null', 'or', 'print', 'private', @@ -79,6 +81,7 @@ class CompletionProvider 'switch', 'throw', 'trait', + 'true', 'try', 'unset', 'use', From fe7e9d580041238682f1952d3429569f183afd23 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sat, 10 Jun 2017 21:36:16 +0200 Subject: [PATCH 5/6] Rename $stmts to $sourceFileNode everywhere The root node is now a SourceFileNode, not an array --- src/PhpDocument.php | 18 +++++++++--------- src/TreeAnalyzer.php | 20 ++++++++++---------- tests/DefinitionResolverTest.php | 16 ++++++++-------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/PhpDocument.php b/src/PhpDocument.php index f7b813e..7820701 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -56,9 +56,9 @@ class PhpDocument /** * The AST of the document * - * @var Node + * @var Node\SourceFileNode */ - private $stmts; + private $sourceFileNode; /** * Map from fully qualified name (FQN) to Definition @@ -172,7 +172,7 @@ class PhpDocument $this->index->addReferenceUri($fqn, $this->uri); } - $this->stmts = $treeAnalyzer->getStmts(); + $this->sourceFileNode = $treeAnalyzer->getSourceFileNode(); } /** @@ -221,11 +221,11 @@ class PhpDocument /** * Returns the AST of the document * - * @return Node | null + * @return Node\SourceFileNode|null */ - public function getStmts() + public function getSourceFileNode() { - return $this->stmts; + return $this->sourceFileNode; } /** @@ -236,12 +236,12 @@ class PhpDocument */ public function getNodeAtPosition(Position $position) { - if ($this->stmts === null) { + if ($this->sourceFileNode === null) { return null; } - $offset = $position->toOffset($this->stmts->getFileContents()); - $node = $this->stmts->getDescendantNodeAtPosition($offset); + $offset = $position->toOffset($this->sourceFileNode->getFileContents()); + $node = $this->sourceFileNode->getDescendantNodeAtPosition($offset); if ($node !== null && $node->getStart() > $offset) { return null; } diff --git a/src/TreeAnalyzer.php b/src/TreeAnalyzer.php index 08c60b0..3296ae8 100644 --- a/src/TreeAnalyzer.php +++ b/src/TreeAnalyzer.php @@ -15,8 +15,8 @@ class TreeAnalyzer /** @var PhpParser\Parser */ private $parser; - /** @var Node */ - private $stmts; + /** @var Node\SourceFileNode */ + private $sourceFileNode; /** @var Diagnostic[] */ private $diagnostics; @@ -46,17 +46,17 @@ class TreeAnalyzer $this->docBlockFactory = $docBlockFactory; $this->definitionResolver = $definitionResolver; $this->content = $content; - $this->stmts = $this->parser->parseSourceFile($content, $uri); + $this->sourceFileNode = $this->parser->parseSourceFile($content, $uri); // TODO - docblock errors - $this->collectDefinitionsAndReferences($this->stmts); + $this->collectDefinitionsAndReferences($this->sourceFileNode); } - private function collectDefinitionsAndReferences(Node $stmts) + private function collectDefinitionsAndReferences(Node $sourceFileNode) { - foreach ($stmts::CHILD_NAMES as $name) { - $node = $stmts->$name; + foreach ($sourceFileNode::CHILD_NAMES as $name) { + $node = $sourceFileNode->$name; if ($node === null) { continue; @@ -200,10 +200,10 @@ class TreeAnalyzer } /** - * @return Node[] + * @return Node\SourceFileNode */ - public function getStmts() + public function getSourceFileNode() { - return $this->stmts; + return $this->sourceFileNode; } } diff --git a/tests/DefinitionResolverTest.php b/tests/DefinitionResolverTest.php index e7d8b78..0397e71 100644 --- a/tests/DefinitionResolverTest.php +++ b/tests/DefinitionResolverTest.php @@ -14,11 +14,11 @@ class DefinitionResolverTest extends TestCase { $parser = new PhpParser\Parser; $doc = new MockPhpDocument; - $stmts = $parser->parseSourceFile("getUri()); + $sourceFileNode = $parser->parseSourceFile("getUri()); $index = new Index; $definitionResolver = new DefinitionResolver($index); - $def = $definitionResolver->createDefinitionFromNode($stmts->statementList[1]->expression, '\TEST_DEFINE'); + $def = $definitionResolver->createDefinitionFromNode($sourceFileNode->statementList[1]->expression, '\TEST_DEFINE'); $this->assertInstanceOf(\phpDocumentor\Reflection\Types\Boolean::class, $def->type); } @@ -27,11 +27,11 @@ class DefinitionResolverTest extends TestCase { $parser = new PhpParser\Parser; $doc = new MockPhpDocument; - $stmts = $parser->parseSourceFile("getUri()); + $sourceFileNode = $parser->parseSourceFile("getUri()); $index = new Index; $definitionResolver = new DefinitionResolver($index); - $type = $definitionResolver->getTypeFromNode($stmts->statementList[1]->expression); + $type = $definitionResolver->getTypeFromNode($sourceFileNode->statementList[1]->expression); $this->assertInstanceOf(\phpDocumentor\Reflection\Types\Boolean::class, $type); } @@ -41,11 +41,11 @@ class DefinitionResolverTest extends TestCase // define('XXX') (only one argument) must not introduce a new symbol $parser = new PhpParser\Parser; $doc = new MockPhpDocument; - $stmts = $parser->parseSourceFile("getUri()); + $sourceFileNode = $parser->parseSourceFile("getUri()); $index = new Index; $definitionResolver = new DefinitionResolver($index); - $fqn = $definitionResolver->getDefinedFqn($stmts->statementList[1]->expression); + $fqn = $definitionResolver->getDefinedFqn($sourceFileNode->statementList[1]->expression); $this->assertNull($fqn); } @@ -54,11 +54,11 @@ class DefinitionResolverTest extends TestCase { $parser = new PhpParser\Parser; $doc = new MockPhpDocument; - $stmts = $parser->parseSourceFile("getUri()); + $sourceFileNode = $parser->parseSourceFile("getUri()); $index = new Index; $definitionResolver = new DefinitionResolver($index); - $fqn = $definitionResolver->getDefinedFqn($stmts->statementList[1]->expression); + $fqn = $definitionResolver->getDefinedFqn($sourceFileNode->statementList[1]->expression); $this->assertEquals('TEST_DEFINE', $fqn); } From 8d1732ed02b59554f3a71055b4b8a210ebcd3726 Mon Sep 17 00:00:00 2001 From: Nicholas Narsing Date: Sun, 11 Jun 2017 17:24:17 -0400 Subject: [PATCH 6/6] Exclude directory paths from file system search (#401) * Exclude directories from file system search Directories can also match the glob search pattern if their names end in ".php", which will cause a read error later since the ContentRetriever implementers are expecting files. As far as I know, the only way to fix this is to do an additional check to ensure the URI is not of a directory. This resolves #306. --- src/FilesFinder/FileSystemFilesFinder.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/FilesFinder/FileSystemFilesFinder.php b/src/FilesFinder/FileSystemFilesFinder.php index 52df4b6..a26b5d8 100644 --- a/src/FilesFinder/FileSystemFilesFinder.php +++ b/src/FilesFinder/FileSystemFilesFinder.php @@ -22,7 +22,11 @@ class FileSystemFilesFinder implements FilesFinder return coroutine(function () use ($glob) { $uris = []; foreach (new GlobIterator($glob) as $path) { - $uris[] = pathToUri($path); + // Exclude any directories that also match the glob pattern + if (!is_dir($path)) { + $uris[] = pathToUri($path); + } + yield timeout(); } return $uris;