@@ -15,11 +15,12 @@ class Index implements ReadableIndex, \Serializable
1515 use EmitterTrait;
1616
1717 /**
18- * An associative array that maps fully qualified symbol names to Definitions (global or not)
18+ * An associative array that maps namespaces to
19+ * an associative array that maps fully qualified symbol names to global Definitions
1920 *
20- * @var Definition[]
21+ * @var array
2122 */
22- private $ definitions = [];
23+ private $ namespaceDefinitions = [];
2324
2425 /**
2526 * An associative array that maps fully qualified symbol names to global Definitions
@@ -28,13 +29,6 @@ class Index implements ReadableIndex, \Serializable
2829 */
2930 private $ globalDefinitions = [];
3031
31- /**
32- * An associative array that maps namespaces to an associative array of FQN to Definitions
33- *
34- * @var Definition[]
35- */
36- private $ namespaceDefinitions = [];
37-
3832 /**
3933 * An associative array that maps fully qualified symbol names to arrays of document URIs that reference the symbol
4034 *
@@ -98,14 +92,18 @@ public function isStaticComplete(): bool
9892 }
9993
10094 /**
101- * Returns an associative array [string => Definition] that maps fully qualified symbol names
102- * to Definitions (global or not)
95+ * Returns a Generator providing an associative array [string => Definition]
96+ * that maps fully qualified symbol names to Definitions (global or not)
10397 *
104- * @return Definition[]
98+ * @return \Generator providing Definition[]
10599 */
106- public function getDefinitions (): array
100+ public function getDefinitions (): \ Generator
107101 {
108- return $ this ->definitions ;
102+ foreach ($ this ->namespaceDefinitions as $ namespaceDefinition ) {
103+ foreach ($ namespaceDefinition as $ fqn => $ definition ) {
104+ yield $ fqn => $ definition ;
105+ }
106+ }
109107 }
110108
111109 /**
@@ -142,9 +140,13 @@ public function getDefinitionsForNamespace(string $namespace): array
142140 */
143141 public function getDefinition (string $ fqn , bool $ globalFallback = false )
144142 {
145- if (isset ($ this ->definitions [$ fqn ])) {
146- return $ this ->definitions [$ fqn ];
143+ $ namespace = $ this ->extractNamespace ($ fqn );
144+ $ definitions = $ this ->getDefinitionsForNamespace ($ namespace );
145+
146+ if (isset ($ definitions [$ fqn ])) {
147+ return $ definitions [$ fqn ];
147148 }
149+
148150 if ($ globalFallback ) {
149151 $ parts = explode ('\\' , $ fqn );
150152 $ fqn = end ($ parts );
@@ -161,9 +163,13 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
161163 */
162164 public function setDefinition (string $ fqn , Definition $ definition )
163165 {
164- $ this ->definitions [$ fqn ] = $ definition ;
166+ $ namespace = $ this ->extractNamespace ($ fqn );
167+ if (!isset ($ this ->namespaceDefinitions [$ namespace ])) {
168+ $ this ->namespaceDefinitions [$ namespace ] = [];
169+ }
170+
171+ $ this ->namespaceDefinitions [$ namespace ][$ fqn ] = $ definition ;
165172 $ this ->setGlobalDefinition ($ fqn , $ definition );
166- $ this ->setNamespaceDefinition ($ fqn , $ definition );
167173 $ this ->emit ('definition-added ' );
168174 }
169175
@@ -176,10 +182,17 @@ public function setDefinition(string $fqn, Definition $definition)
176182 */
177183 public function removeDefinition (string $ fqn )
178184 {
179- unset($ this ->definitions [$ fqn ]);
185+ $ namespace = $ this ->extractNamespace ($ fqn );
186+ if (isset ($ this ->namespaceDefinitions [$ namespace ])) {
187+ unset($ this ->namespaceDefinitions [$ namespace ][$ fqn ]);
188+
189+ if (empty ($ this ->namespaceDefinitions [$ namespace ])) {
190+ unset($ this ->namespaceDefinitions [$ namespace ]);
191+ }
192+ }
193+
180194 unset($ this ->globalDefinitions [$ fqn ]);
181195 unset($ this ->references [$ fqn ]);
182- $ this ->removeNamespaceDefinition ($ fqn );
183196 }
184197
185198 /**
@@ -252,9 +265,10 @@ public function unserialize($serialized)
252265 $ this ->$ prop = $ val ;
253266 }
254267
255- foreach ($ this ->definitions as $ fqn => $ definition ) {
256- $ this ->setGlobalDefinition ($ fqn , $ definition );
257- $ this ->setNamespaceDefinition ($ fqn , $ definition );
268+ foreach ($ this ->namespaceDefinitions as $ namespaceDefinition ) {
269+ foreach ($ namespaceDefinition as $ fqn => $ definition ) {
270+ $ this ->setGlobalDefinition ($ fqn , $ definition );
271+ }
258272 }
259273 }
260274
@@ -265,7 +279,7 @@ public function unserialize($serialized)
265279 public function serialize ()
266280 {
267281 return serialize ([
268- 'definitions ' => $ this ->definitions ,
282+ 'namespaceDefinitions ' => $ this ->namespaceDefinitions ,
269283 'references ' => $ this ->references ,
270284 'complete ' => $ this ->complete ,
271285 'staticComplete ' => $ this ->staticComplete
@@ -286,40 +300,6 @@ private function setGlobalDefinition(string $fqn, Definition $definition)
286300 }
287301 }
288302
289- /**
290- * Registers a definition to a namespace
291- *
292- * @param string $fqn The fully qualified name of the symbol
293- * @param Definition $definition The Definition object
294- * @return void
295- */
296- private function setNamespaceDefinition (string $ fqn , Definition $ definition )
297- {
298- $ namespace = $ this ->extractNamespace ($ fqn );
299- if (!isset ($ this ->namespaceDefinitions [$ namespace ])) {
300- $ this ->namespaceDefinitions [$ namespace ] = [];
301- }
302- $ this ->namespaceDefinitions [$ namespace ][$ fqn ] = $ definition ;
303- }
304-
305- /**
306- * Removes a definition from a namespace
307- *
308- * @param string $fqn The fully qualified name of the symbol
309- * @return void
310- */
311- private function removeNamespaceDefinition (string $ fqn )
312- {
313- $ namespace = $ this ->extractNamespace ($ fqn );
314- if (isset ($ this ->namespaceDefinitions [$ namespace ])) {
315- unset($ this ->namespaceDefinitions [$ namespace ][$ fqn ]);
316-
317- if (0 === sizeof ($ this ->namespaceDefinitions [$ namespace ])) {
318- unset($ this ->namespaceDefinitions [$ namespace ]);
319- }
320- }
321- }
322-
323303 /**
324304 * @param string $fqn
325305 * @return string The namespace extracted from the given FQN
0 commit comments