From b03950cb53cf492459ec3494d6316d106d324b24 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sat, 18 Nov 2017 14:53:18 -0800 Subject: [PATCH] Cleanup --- src/CompletionProvider.php | 4 ++-- src/Index/AbstractAggregateIndex.php | 12 +++++----- src/Index/Index.php | 34 +++++++++++++--------------- src/Index/ReadableIndex.php | 12 +++++----- src/Server/TextDocument.php | 21 ++++------------- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index fe78ef7..2f22333 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -221,7 +221,7 @@ class CompletionProvider // The FQNs of the symbol and its parents (eg the implemented interfaces) foreach ($this->expandParentFqns($fqns) as $parentFqn) { // Collect fqn definitions - foreach ($this->index->getDefinitionsForFqn($parentFqn) as $fqn => $def) { + foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) { // Add the object access operator to only get members of all parents $prefix = $parentFqn . '->'; if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) { @@ -251,7 +251,7 @@ class CompletionProvider // The FQNs of the symbol and its parents (eg the implemented interfaces) foreach ($this->expandParentFqns($fqns) as $parentFqn) { // Collect fqn definitions - foreach ($this->index->getDefinitionsForFqn($parentFqn) as $fqn => $def) { + foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) { // Append :: operator to only get static members of all parents $prefix = strtolower($parentFqn . '::'); if (substr(strtolower($fqn), 0, strlen($prefix)) === $prefix && $def->isMember) { diff --git a/src/Index/AbstractAggregateIndex.php b/src/Index/AbstractAggregateIndex.php index 1998889..90490ab 100644 --- a/src/Index/AbstractAggregateIndex.php +++ b/src/Index/AbstractAggregateIndex.php @@ -102,7 +102,7 @@ abstract class AbstractAggregateIndex implements ReadableIndex * Returns a Generator providing an associative array [string => Definition] * that maps fully qualified symbol names to Definitions (global or not) * - * @return \Generator providing Definition[] + * @return \Generator yields Definition */ public function getDefinitions(): \Generator { @@ -112,15 +112,15 @@ abstract class AbstractAggregateIndex implements ReadableIndex } /** - * Returns a Generator providing the Definitions that are in the given FQN + * Returns a Generator that yields all the descendant Definitions of a given FQN * * @param string $fqn - * @return \Generator providing Definitions[] + * @return \Generator yields Definition */ - public function getDefinitionsForFqn(string $fqn): \Generator + public function getDescendantDefinitionsForFqn(string $fqn): \Generator { foreach ($this->getIndexes() as $index) { - yield from $index->getDefinitionsForFqn($fqn); + yield from $index->getDescendantDefinitionsForFqn($fqn); } } @@ -144,7 +144,7 @@ abstract class AbstractAggregateIndex implements ReadableIndex * Returns a Generator providing all URIs in this index that reference a symbol * * @param string $fqn The fully qualified name of the symbol - * @return \Generator providing string[] + * @return \Generator yields string */ public function getReferenceUris(string $fqn): \Generator { diff --git a/src/Index/Index.php b/src/Index/Index.php index 7088e03..4000217 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -99,7 +99,7 @@ class Index implements ReadableIndex, \Serializable * Returns a Generator providing an associative array [string => Definition] * that maps fully qualified symbol names to Definitions (global or not) * - * @return \Generator providing Definition[] + * @return \Generator yields Definition */ public function getDefinitions(): \Generator { @@ -107,12 +107,12 @@ class Index implements ReadableIndex, \Serializable } /** - * Returns a Generator providing the Definitions that are in the given FQN + * Returns a Generator that yields all the descendant Definitions of a given FQN * * @param string $fqn - * @return \Generator providing Definitions[] + * @return \Generator yields Definition */ - public function getDefinitionsForFqn(string $fqn): \Generator + public function getDescendantDefinitionsForFqn(string $fqn): \Generator { $parts = $this->splitFqn($fqn); if ('' === end($parts)) { @@ -188,7 +188,7 @@ class Index implements ReadableIndex, \Serializable * Returns a Generator providing all URIs in this index that reference a symbol * * @param string $fqn The fully qualified name of the symbol - * @return \Generator providing string[] + * @return \Generator yields string */ public function getReferenceUris(string $fqn): \Generator { @@ -280,9 +280,9 @@ class Index implements ReadableIndex, \Serializable } /** - * Returns a Genrerator containing all the into the given $storage recursively. - * The generator yields key => value pairs, eg - * 'Psr\Log\LoggerInterface->log()' => $definition + * Returns a Generator that yields all the Definitions in the given $storage recursively. + * The generator yields key => value pairs, e.g. + * `'Psr\Log\LoggerInterface->log()' => $definition` * * @param array &$storage * @param string $prefix (optional) @@ -319,12 +319,12 @@ class Index implements ReadableIndex, \Serializable $parts = array_slice($parts, 1); } - $parts[0] = '\\'.$parts[0]; + $parts[0] = '\\' . $parts[0]; } // write back the backslashes prefixes for the other parts for ($i = 1; $i < count($parts); $i++) { - $parts[$i] = '\\'.$parts[$i]; + $parts[$i] = '\\' . $parts[$i]; } // split the last part in 2 parts at the operator @@ -337,7 +337,7 @@ class Index implements ReadableIndex, \Serializable // replace the last part by its pieces array_pop($parts); $parts[] = $endParts[0]; - $parts[] = $operator.$endParts[1]; + $parts[] = $operator . $endParts[1]; break; } } @@ -382,8 +382,8 @@ class Index implements ReadableIndex, \Serializable } /** - * Recusrive function which store the given definition in the given $storage - * array represented as a tree matching the given $parts. + * Recursive function that stores the given Definition in the given $storage array represented + * as a tree matching the given $parts. * * @param int $level The current level of FQN part * @param string[] $parts The splitted FQN @@ -408,11 +408,9 @@ class Index implements ReadableIndex, \Serializable } /** - * Recusrive function which remove the definition matching the given $parts - * from the given $storage array. - * The function also looks up recursively to remove the parents of the - * definition which no longer has children to avoid to let empty arrays - * in the index. + * Recursive function that removes the definition matching the given $parts from the given + * $storage array. The function also looks up recursively to remove the parents of the + * definition which no longer has children to avoid to let empty arrays in the index. * * @param int $level The current level of FQN part * @param string[] $parts The splitted FQN diff --git a/src/Index/ReadableIndex.php b/src/Index/ReadableIndex.php index 5c54557..90ddcc4 100644 --- a/src/Index/ReadableIndex.php +++ b/src/Index/ReadableIndex.php @@ -33,17 +33,17 @@ interface ReadableIndex extends EmitterInterface * Returns a Generator providing an associative array [string => Definition] * that maps fully qualified symbol names to Definitions (global or not) * - * @return \Generator providing Definition[] + * @return \Generator yields Definition */ public function getDefinitions(): \Generator; /** - * Returns a Generator providing the Definitions that are in the given FQN + * Returns a Generator that yields all the descendant Definitions of a given FQN * * @param string $fqn - * @return \Generator providing Definitions[] + * @return \Generator yields Definition */ - public function getDefinitionsForFqn(string $fqn): \Generator; + public function getDescendantDefinitionsForFqn(string $fqn): \Generator; /** * Returns the Definition object by a specific FQN @@ -55,10 +55,10 @@ interface ReadableIndex extends EmitterInterface public function getDefinition(string $fqn, bool $globalFallback = false); /** - * Returns a Generator providing all URIs in this index that reference a symbol + * Returns a Generator that yields all URIs in this index that reference a symbol * * @param string $fqn The fully qualified name of the symbol - * @return \Generator providing string[] + * @return \Generator yields string */ public function getReferenceUris(string $fqn): \Generator; } diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 53d804f..0ad2d81 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -220,9 +220,11 @@ class TextDocument return []; } } - $refDocuments = yield Promise\all(iterator_to_array( - $this->getOrLoadReferences($fqn) - )); + $refDocumentPromises = []; + foreach ($this->index->getReferenceUris($fqn) as $uri) { + $refDocumentPromises[] = $this->documentLoader->getOrLoad($uri); + } + $refDocuments = yield Promise\all($refDocumentPromises); foreach ($refDocuments as $document) { $refs = $document->getReferenceNodesByFqn($fqn); if ($refs !== null) { @@ -398,17 +400,4 @@ class TextDocument return [new SymbolLocationInformation($descriptor, $def->symbolInformation->location)]; }); } - - /** - * Gets or loads the documents referencing the given FQN. - * - * @param string $fqn - * @return \Generator providing Promise - */ - private function getOrLoadReferences(string $fqn): \Generator - { - foreach ($this->index->getReferenceUris($fqn) as $ref) { - yield $this->documentLoader->getOrLoad($ref); - } - } }