@@ -93,7 +93,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
9393
9494 $ cached = $ this ->cache ->load ($ cacheKey , $ variableCacheKey );
9595 if (is_array ($ cached )) {
96- $ cached = $ this ->resolveStubFilename ($ cached );
96+ $ cached = $ this ->mapStubFilenames ($ cached, fn ( string $ filename ): ? string => $ this -> resolveStubFilename ( $ filename ) );
9797 }
9898 if (is_array ($ cached )) {
9999 if ($ identifier ->isConstant ()) {
@@ -117,7 +117,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
117117 || $ reflection instanceof ReflectionFunction
118118 || $ reflection instanceof ReflectionConstant
119119 ) {
120- $ exported = $ this ->relativizeStubFilename ($ reflection ->exportToCache ());
120+ $ exported = $ this ->mapStubFilenames ($ reflection ->exportToCache (), fn ( string $ filename ): ? string => $ this -> relativizeStubFilename ( $ filename ));
121121 if ($ exported !== null ) {
122122 $ this ->cache ->save ($ cacheKey , $ variableCacheKey , $ exported );
123123 }
@@ -127,34 +127,71 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
127127 }
128128
129129 /**
130- * @param array<string, mixed> $exported
130+ * Maps the stub path of every located source in the exported blob: the
131+ * reflection's own, and the ones that the methods an enum gets from the
132+ * engine (cases(), from(), tryFrom()) carry next to it.
133+ *
134+ * @param array<string, mixed> $data
135+ * @param callable(string): ?string $mapFilename
136+ * @return array<string, mixed>|null null when any of the paths cannot be mapped
137+ */
138+ private function mapStubFilenames (array $ data , callable $ mapFilename ): ?array
139+ {
140+ $ locatedSource = $ this ->mapStubFilename ($ data ['locatedSource ' ] ?? null , $ mapFilename );
141+ if ($ locatedSource === null ) {
142+ return null ;
143+ }
144+ $ data ['locatedSource ' ] = $ locatedSource ;
145+
146+ foreach ($ data ['immediateMethods ' ] ?? [] as $ i => $ method ) {
147+ if (!isset ($ method ['locatedSource ' ])) {
148+ continue ;
149+ }
150+
151+ $ locatedSource = $ this ->mapStubFilename ($ method ['locatedSource ' ], $ mapFilename );
152+ if ($ locatedSource === null ) {
153+ return null ;
154+ }
155+ $ data ['immediateMethods ' ][$ i ]['locatedSource ' ] = $ locatedSource ;
156+ }
157+
158+ return $ data ;
159+ }
160+
161+ /**
162+ * @param callable(string): ?string $mapFilename
131163 * @return array<string, mixed>|null
132164 */
133- private function relativizeStubFilename ( array $ exported ): ?array
165+ private function mapStubFilename ( mixed $ locatedSource , callable $ mapFilename ): ?array
134166 {
135- $ filename = $ exported [ ' locatedSource ' ] ['data ' ]['filename ' ] ?? null ;
167+ $ filename = $ locatedSource ['data ' ]['filename ' ] ?? null ;
136168 if (!is_string ($ filename )) {
137169 return null ;
138170 }
139171
172+ $ filename = $ mapFilename ($ filename );
173+ if ($ filename === null ) {
174+ return null ;
175+ }
176+
177+ $ locatedSource ['data ' ]['filename ' ] = $ filename ;
178+
179+ return $ locatedSource ;
180+ }
181+
182+ private function relativizeStubFilename (string $ filename ): ?string
183+ {
140184 $ markerPosition = strpos ($ filename , self ::STUBS_DIR_MARKER );
141185 if ($ markerPosition === false ) {
142186 return null ;
143187 }
144188
145- $ exported ['locatedSource ' ]['data ' ]['filename ' ] = self ::RELATIVE_FILENAME_PREFIX . substr ($ filename , $ markerPosition + strlen (self ::STUBS_DIR_MARKER ));
146-
147- return $ exported ;
189+ return self ::RELATIVE_FILENAME_PREFIX . substr ($ filename , $ markerPosition + strlen (self ::STUBS_DIR_MARKER ));
148190 }
149191
150- /**
151- * @param array<string, mixed> $cached
152- * @return array<string, mixed>|null
153- */
154- private function resolveStubFilename (array $ cached ): ?array
192+ private function resolveStubFilename (string $ filename ): ?string
155193 {
156- $ filename = $ cached ['locatedSource ' ]['data ' ]['filename ' ] ?? null ;
157- if (!is_string ($ filename ) || !str_starts_with ($ filename , self ::RELATIVE_FILENAME_PREFIX )) {
194+ if (!str_starts_with ($ filename , self ::RELATIVE_FILENAME_PREFIX )) {
158195 return null ;
159196 }
160197
@@ -163,9 +200,7 @@ private function resolveStubFilename(array $cached): ?array
163200 return null ;
164201 }
165202
166- $ cached ['locatedSource ' ]['data ' ]['filename ' ] = $ resolved ;
167-
168- return $ cached ;
203+ return $ resolved ;
169204 }
170205
171206 /**
0 commit comments