Skip to content

Commit e30f446

Browse files
committed
Simplify AutoloadSourceLocator - let it actually autoload the file
1 parent eba247f commit e30f446

File tree

3 files changed

+4
-301
lines changed

3 files changed

+4
-301
lines changed

Diff for: build/baseline-8.0.neon

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Strict comparison using \\=\\=\\= between array and false will always evaluate to false\\.$#"
5-
count: 1
6-
path: ../src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php
7-
83
-
94
message: "#^Strict comparison using \\=\\=\\= between array and false will always evaluate to false\\.$#"
105
count: 1

Diff for: src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php

+4-52
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
use function interface_exists;
2929
use function is_file;
3030
use function is_string;
31-
use function restore_error_handler;
32-
use function set_error_handler;
33-
use function spl_autoload_functions;
3431
use function strtolower;
3532
use function trait_exists;
3633
use const PHP_VERSION_ID;
@@ -180,8 +177,10 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
180177
);
181178
}
182179
}
180+
183181
return null;
184182
}
183+
185184
[$potentiallyLocatedFile, $className, $startLine] = $locateResult;
186185

187186
return $this->findReflection($reflector, $potentiallyLocatedFile, new Identifier($className, $identifier->getType()), $startLine);
@@ -259,23 +258,11 @@ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $id
259258
}
260259

261260
/**
262-
* Attempt to locate a class by name.
263-
*
264-
* If class already exists, simply use internal reflection API to get the
265-
* filename and store it.
266-
*
267-
* If class does not exist, we make an assumption that whatever autoloaders
268-
* that are registered will be loading a file. We then override the file://
269-
* protocol stream wrapper to "capture" the filename we expect the class to
270-
* be in, and then restore it. Note that class_exists will cause an error
271-
* that it cannot find the file, so we squelch the errors by overriding the
272-
* error handler temporarily.
273-
*
274261
* @return array{string, string, int|null}|null
275262
*/
276263
private function locateClassByName(string $className): ?array
277264
{
278-
if (class_exists($className, false) || interface_exists($className, false) || trait_exists($className, false)) {
265+
if (class_exists($className) || interface_exists($className) || trait_exists($className)) {
279266
$reflection = new ReflectionClass($className);
280267
$filename = $reflection->getFileName();
281268

@@ -290,42 +277,7 @@ private function locateClassByName(string $className): ?array
290277
return [$filename, $reflection->getName(), $reflection->getStartLine() !== false ? $reflection->getStartLine() : null];
291278
}
292279

293-
$this->silenceErrors();
294-
295-
try {
296-
/** @var array{string, string, null}|null */
297-
return FileReadTrapStreamWrapper::withStreamWrapperOverride(
298-
static function () use ($className): ?array {
299-
$functions = spl_autoload_functions();
300-
if ($functions === false) {
301-
return null;
302-
}
303-
304-
foreach ($functions as $preExistingAutoloader) {
305-
$preExistingAutoloader($className);
306-
307-
/**
308-
* This static variable is populated by the side-effect of the stream wrapper
309-
* trying to read the file path when `include()` is used by an autoloader.
310-
*
311-
* This will not be `null` when the autoloader tried to read a file.
312-
*/
313-
if (FileReadTrapStreamWrapper::$autoloadLocatedFile !== null) {
314-
return [FileReadTrapStreamWrapper::$autoloadLocatedFile, $className, null];
315-
}
316-
}
317-
318-
return null;
319-
},
320-
);
321-
} finally {
322-
restore_error_handler();
323-
}
324-
}
325-
326-
private function silenceErrors(): void
327-
{
328-
set_error_handler(static fn (): bool => true);
280+
return null;
329281
}
330282

331283
}

Diff for: src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php

-244
This file was deleted.

0 commit comments

Comments
 (0)