Skip to content

Commit 28eaba2

Browse files
committed
FileReadTrapStreamWrapper - be aware of multiple files
1 parent ae53760 commit 28eaba2

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,23 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
135135
if ($locateResult === null) {
136136
return null;
137137
}
138-
[$potentiallyLocatedFile, $className, $startLine] = $locateResult;
138+
[$potentiallyLocatedFiles, $className, $startLine] = $locateResult;
139139
if ($startLine !== null) {
140140
$this->startLineByClass[strtolower($className)] = $startLine;
141141
}
142142

143-
return $this->findReflection($reflector, $potentiallyLocatedFile, new Identifier($className, $identifier->getType()), $startLine);
143+
$newIdentifier = new Identifier($className, $identifier->getType());
144+
145+
foreach ($potentiallyLocatedFiles as $potentiallyLocatedFile) {
146+
$reflection = $this->findReflection($reflector, $potentiallyLocatedFile, $newIdentifier, $startLine);
147+
if ($reflection === null) {
148+
continue;
149+
}
150+
151+
return $reflection;
152+
}
153+
154+
return null;
144155
}
145156

146157
private function findReflection(Reflector $reflector, string $file, Identifier $identifier, ?int $startLine): ?Reflection
@@ -313,7 +324,7 @@ private function getReflectionClass(string $className): ?ReflectionClass
313324
* that it cannot find the file, so we squelch the errors by overriding the
314325
* error handler temporarily.
315326
*
316-
* @return array{string, string, int|null}|null
327+
* @return array{string[], string, int|null}|null
317328
*/
318329
private function locateClassByName(string $className): ?array
319330
{
@@ -324,7 +335,7 @@ private function locateClassByName(string $className): ?array
324335
return null;
325336
}
326337

327-
return [$filename, $reflection->getName(), $reflection->getStartLine() !== false ? $reflection->getStartLine() : null];
338+
return [[$filename], $reflection->getName(), $reflection->getStartLine() !== false ? $reflection->getStartLine() : null];
328339
}
329340

330341
if (!$this->disableRuntimeReflectionProvider) {
@@ -334,7 +345,7 @@ private function locateClassByName(string $className): ?array
334345
$this->silenceErrors();
335346

336347
try {
337-
/** @var array{string, string, null}|null */
348+
/** @var array{string[], string, null}|null */
338349
$result = FileReadTrapStreamWrapper::withStreamWrapperOverride(
339350
static function () use ($className): ?array {
340351
$functions = spl_autoload_functions();
@@ -351,8 +362,8 @@ static function () use ($className): ?array {
351362
*
352363
* This will not be `null` when the autoloader tried to read a file.
353364
*/
354-
if (FileReadTrapStreamWrapper::$autoloadLocatedFile !== null) {
355-
return [FileReadTrapStreamWrapper::$autoloadLocatedFile, $className, null];
365+
if (FileReadTrapStreamWrapper::$autoloadLocatedFiles !== []) {
366+
return [FileReadTrapStreamWrapper::$autoloadLocatedFiles, $className, null];
356367
}
357368
}
358369

@@ -363,7 +374,9 @@ static function () use ($className): ?array {
363374
return null;
364375
}
365376

366-
opcache_invalidate($result[0], true);
377+
foreach ($result[0] as $file) {
378+
opcache_invalidate($file, true);
379+
}
367380

368381
return $result;
369382
} finally {

src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ final class FileReadTrapStreamWrapper
3434
/** @var string[]|null */
3535
private static ?array $registeredStreamWrapperProtocols;
3636

37-
public static ?string $autoloadLocatedFile = null;
37+
/** @var string[] */
38+
public static array $autoloadLocatedFiles = [];
3839

3940
private bool $readFromFile = false;
4041

@@ -55,7 +56,7 @@ public static function withStreamWrapperOverride(
5556
)
5657
{
5758
self::$registeredStreamWrapperProtocols = $streamWrapperProtocols;
58-
self::$autoloadLocatedFile = null;
59+
self::$autoloadLocatedFiles = [];
5960

6061
try {
6162
foreach ($streamWrapperProtocols as $protocol) {
@@ -71,7 +72,7 @@ public static function withStreamWrapperOverride(
7172
}
7273

7374
self::$registeredStreamWrapperProtocols = null;
74-
self::$autoloadLocatedFile = null;
75+
self::$autoloadLocatedFiles = [];
7576

7677
return $result;
7778
}
@@ -93,9 +94,7 @@ public static function withStreamWrapperOverride(
9394
*/
9495
public function stream_open($path, $mode, $options, &$openedPath): bool
9596
{
96-
if (self::$autoloadLocatedFile === null) {
97-
self::$autoloadLocatedFile = $path;
98-
}
97+
self::$autoloadLocatedFiles[] = $path;
9998
$this->readFromFile = false;
10099
$this->seekPosition = 0;
101100

@@ -139,11 +138,11 @@ public function stream_close(): void
139138
*/
140139
public function stream_stat()
141140
{
142-
if (self::$autoloadLocatedFile === null) {
141+
if (self::$autoloadLocatedFiles === []) {
143142
return false;
144143
}
145144

146-
return $this->url_stat(self::$autoloadLocatedFile, STREAM_URL_STAT_QUIET);
145+
return $this->url_stat(self::$autoloadLocatedFiles[0], STREAM_URL_STAT_QUIET);
147146
}
148147

149148
/**

0 commit comments

Comments
 (0)