Skip to content

Commit

Permalink
Simplify AutoloadSourceLocator - let it actually autoload the file
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 22, 2021
1 parent eba247f commit e30f446
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 301 deletions.
5 changes: 0 additions & 5 deletions build/baseline-8.0.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Strict comparison using \\=\\=\\= between array and false will always evaluate to false\\.$#"
count: 1
path: ../src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php

-
message: "#^Strict comparison using \\=\\=\\= between array and false will always evaluate to false\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
use function interface_exists;
use function is_file;
use function is_string;
use function restore_error_handler;
use function set_error_handler;
use function spl_autoload_functions;
use function strtolower;
use function trait_exists;
use const PHP_VERSION_ID;
Expand Down Expand Up @@ -180,8 +177,10 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
);
}
}

return null;
}

[$potentiallyLocatedFile, $className, $startLine] = $locateResult;

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

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

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

$this->silenceErrors();

try {
/** @var array{string, string, null}|null */
return FileReadTrapStreamWrapper::withStreamWrapperOverride(
static function () use ($className): ?array {
$functions = spl_autoload_functions();
if ($functions === false) {
return null;
}

foreach ($functions as $preExistingAutoloader) {
$preExistingAutoloader($className);

/**
* This static variable is populated by the side-effect of the stream wrapper
* trying to read the file path when `include()` is used by an autoloader.
*
* This will not be `null` when the autoloader tried to read a file.
*/
if (FileReadTrapStreamWrapper::$autoloadLocatedFile !== null) {
return [FileReadTrapStreamWrapper::$autoloadLocatedFile, $className, null];
}
}

return null;
},
);
} finally {
restore_error_handler();
}
}

private function silenceErrors(): void
{
set_error_handler(static fn (): bool => true);
return null;
}

}

This file was deleted.

0 comments on commit e30f446

Please sign in to comment.