Skip to content

Commit fa67f84

Browse files
committed
perf: get direct children
1 parent 91ca99a commit fa67f84

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

src/CompletionProvider.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon
220220

221221
// The FQNs of the symbol and its parents (eg the implemented interfaces)
222222
foreach ($this->expandParentFqns($fqns) as $parentFqn) {
223+
// Add the object access operator to only get members of all parents
224+
$prefix = $parentFqn . '->';
225+
$prefixLen = strlen($prefix);
223226
// Collect fqn definitions
224-
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) {
225-
// Add the object access operator to only get members of all parents
226-
$prefix = $parentFqn . '->';
227-
if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) {
227+
foreach ($this->index->getChildDefinitionsForFqn($parentFqn) as $fqn => $def) {
228+
if (substr($fqn, 0, $prefixLen) === $prefix && $def->isMember) {
228229
$list->items[] = CompletionItem::fromDefinition($def);
229230
}
230231
}
@@ -250,11 +251,12 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon
250251

251252
// The FQNs of the symbol and its parents (eg the implemented interfaces)
252253
foreach ($this->expandParentFqns($fqns) as $parentFqn) {
254+
// Append :: operator to only get static members of all parents
255+
$prefix = strtolower($parentFqn . '::');
256+
$prefixLen = strlen($prefix);
253257
// Collect fqn definitions
254-
foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) {
255-
// Append :: operator to only get static members of all parents
256-
$prefix = strtolower($parentFqn . '::');
257-
if (substr(strtolower($fqn), 0, strlen($prefix)) === $prefix && $def->isMember) {
258+
foreach ($this->index->getChildDefinitionsForFqn($parentFqn) as $fqn => $def) {
259+
if (substr(strtolower($fqn), 0, $prefixLen) === $prefix && $def->isMember) {
258260
$list->items[] = CompletionItem::fromDefinition($def);
259261
}
260262
}

src/Index/AbstractAggregateIndex.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ public function getDefinitions(): \Generator
112112
}
113113

114114
/**
115-
* Returns a Generator that yields all the descendant Definitions of a given FQN
115+
* Returns a Generator that yields all the direct child Definitions of a given FQN
116116
*
117117
* @param string $fqn
118118
* @return \Generator yields Definition
119119
*/
120-
public function getDescendantDefinitionsForFqn(string $fqn): \Generator
120+
public function getChildDefinitionsForFqn(string $fqn): \Generator
121121
{
122122
foreach ($this->getIndexes() as $index) {
123-
yield from $index->getDescendantDefinitionsForFqn($fqn);
123+
yield from $index->getChildDefinitionsForFqn($fqn);
124124
}
125125
}
126126

src/Index/Index.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ public function getDefinitions(): \Generator
107107
}
108108

109109
/**
110-
* Returns a Generator that yields all the descendant Definitions of a given FQN
110+
* Returns a Generator that yields all the direct child Definitions of a given FQN
111111
*
112112
* @param string $fqn
113113
* @return \Generator yields Definition
114114
*/
115-
public function getDescendantDefinitionsForFqn(string $fqn): \Generator
115+
public function getChildDefinitionsForFqn(string $fqn): \Generator
116116
{
117117
$parts = $this->splitFqn($fqn);
118118
if ('' === end($parts)) {
@@ -122,11 +122,15 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator
122122
}
123123

124124
$result = $this->getIndexValue($parts, $this->definitions);
125-
126-
if ($result instanceof Definition) {
127-
yield $fqn => $result;
128-
} elseif (is_array($result)) {
129-
yield from $this->yieldDefinitionsRecursively($result, $fqn);
125+
if (!$result) {
126+
return;
127+
}
128+
foreach ($result as $name => $item) {
129+
// Don't yield the parent
130+
if ($name === '') {
131+
continue;
132+
}
133+
yield $fqn.$name => $item;
130134
}
131135
}
132136

@@ -374,7 +378,7 @@ private function getIndexValue(array $parts, array &$storage)
374378

375379
$parts = array_slice($parts, 1);
376380
// we've reached the last provided part
377-
if (0 === count($parts)) {
381+
if (empty($parts)) {
378382
return $storage[$part];
379383
}
380384

src/Index/ReadableIndex.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ public function isStaticComplete(): bool;
3838
public function getDefinitions(): \Generator;
3939

4040
/**
41-
* Returns a Generator that yields all the descendant Definitions of a given FQN
41+
* Returns a Generator that yields all the direct child Definitions of a given FQN
4242
*
4343
* @param string $fqn
4444
* @return \Generator yields Definition
4545
*/
46-
public function getDescendantDefinitionsForFqn(string $fqn): \Generator;
46+
public function getChildDefinitionsForFqn(string $fqn): \Generator;
4747

4848
/**
4949
* Returns the Definition object by a specific FQN

0 commit comments

Comments
 (0)