Skip to content

Commit 8768b69

Browse files
committed
use Generators to get definitions without wasting memory
1 parent ca0caf1 commit 8768b69

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

src/Index/AbstractAggregateIndex.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,42 +107,40 @@ public function isStaticComplete(): bool
107107
public function getDefinitions(): \Generator
108108
{
109109
foreach ($this->getIndexes() as $index) {
110-
yield $index->getDefinitions();
110+
foreach ($index->getDefinitions() as $fqn => $definitions) {
111+
yield $fqn => $definition;
112+
}
111113
}
112114
}
113115

114116
/**
115-
* Returns an associative array [string => Definition] that maps fully qualified symbol names
116-
* to global Definitions
117+
* Returns a Generator providing an associative array [string => Definition]
118+
* that maps fully qualified symbol names to global Definitions
117119
*
118-
* @return Definition[]
120+
* @return \Generator providing Definitions[]
119121
*/
120-
public function getGlobalDefinitions(): array
122+
public function getGlobalDefinitions(): \Generator
121123
{
122-
$defs = [];
123124
foreach ($this->getIndexes() as $index) {
124-
foreach ($index->getGlobalDefinitions() as $fqn => $def) {
125-
$defs[$fqn] = $def;
125+
foreach ($index->getGlobalDefinitions() as $fqn => $definition) {
126+
yield $fqn => $definition;
126127
}
127128
}
128-
return $defs;
129129
}
130130

131131
/**
132-
* Returns the Definitions that are in the given namespace
132+
* Returns a Generator providing the Definitions that are in the given namespace
133133
*
134134
* @param string $namespace
135-
* @return Definitions[]
135+
* @return \Generator providing Definitions[]
136136
*/
137-
public function getDefinitionsForNamespace(string $namespace): array
137+
public function getDefinitionsForNamespace(string $namespace): \Generator
138138
{
139-
$defs = [];
140139
foreach ($this->getIndexes() as $index) {
141-
foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $def) {
142-
$defs[$fqn] = $def;
140+
foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $definition) {
141+
yield $fqn => $definition;
143142
}
144143
}
145-
return $defs;
146144
}
147145

148146
/**

src/Index/Index.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,29 @@ public function getDefinitions(): \Generator
107107
}
108108

109109
/**
110-
* Returns an associative array [string => Definition] that maps fully qualified symbol names
111-
* to global Definitions
110+
* Returns a Generator providing an associative array [string => Definition]
111+
* that maps fully qualified symbol names to global Definitions
112112
*
113-
* @return Definition[]
113+
* @return \Generator providing Definitions[]
114114
*/
115-
public function getGlobalDefinitions(): array
115+
public function getGlobalDefinitions(): \Generator
116116
{
117-
return $this->globalDefinitions;
117+
foreach ($this->globalDefinitions as $fqn => $definition) {
118+
yield $fqn => $definition;
119+
}
118120
}
119121

120122
/**
121-
* Returns the Definitions that are in the given namespace
123+
* Returns a Generator providing the Definitions that are in the given namespace
122124
*
123125
* @param string $namespace
124-
* @return Definitions[]
126+
* @return \Generator providing Definitions[]
125127
*/
126-
public function getDefinitionsForNamespace(string $namespace): array
128+
public function getDefinitionsForNamespace(string $namespace): \Generator
127129
{
128-
return isset($this->namespaceDefinitions[$namespace])
129-
? $this->namespaceDefinitions[$namespace]
130-
: []
131-
;
130+
foreach ($this->doGetDefinitionsForNamespace($namespace) as $fqn => $definition) {
131+
yield $fqn => $definition;
132+
}
132133
}
133134

134135
/**
@@ -141,7 +142,7 @@ public function getDefinitionsForNamespace(string $namespace): array
141142
public function getDefinition(string $fqn, bool $globalFallback = false)
142143
{
143144
$namespace = $this->extractNamespace($fqn);
144-
$definitions = $this->getDefinitionsForNamespace($namespace);
145+
$definitions = $this->doGetDefinitionsForNamespace($namespace);
145146

146147
if (isset($definitions[$fqn])) {
147148
return $definitions[$fqn];
@@ -314,4 +315,18 @@ private function extractNamespace(string $fqn): string
314315

315316
return $fqn;
316317
}
318+
319+
/**
320+
* Returns the Definitions that are in the given namespace
321+
*
322+
* @param string $namespace
323+
* @return Definition[]
324+
*/
325+
private function doGetDefinitionsForNamespace(string $namespace): array
326+
{
327+
return isset($this->namespaceDefinitions[$namespace])
328+
? $this->namespaceDefinitions[$namespace]
329+
: []
330+
;
331+
}
317332
}

src/Index/ReadableIndex.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ public function isStaticComplete(): bool;
3838
public function getDefinitions(): \Generator;
3939

4040
/**
41-
* Returns an associative array [string => Definition] that maps fully qualified symbol names
42-
* to global Definitions
41+
* Returns a Generator providing an associative array [string => Definition]
42+
* that maps fully qualified symbol names to global Definitions
4343
*
44-
* @return Definitions[]
44+
* @return \Generator providing Definitions[]
4545
*/
46-
public function getGlobalDefinitions(): array;
46+
public function getGlobalDefinitions(): \Generator;
4747

4848
/**
49-
* Returns the Definitions that are in the given namespace
49+
* Returns a Generator providing the Definitions that are in the given namespace
5050
*
5151
* @param string $namespace
52-
* @return Definitions[]
52+
* @return \Generator providing Definitions[]
5353
*/
54-
public function getDefinitionsForNamespace(string $namespace): array;
54+
public function getDefinitionsForNamespace(string $namespace): \Generator;
5555

5656
/**
5757
* Returns the Definition object by a specific FQN

0 commit comments

Comments
 (0)