diff --git a/src/File/FileReader.php b/src/File/FileReader.php index 5bc5220ae7..40b65477b9 100644 --- a/src/File/FileReader.php +++ b/src/File/FileReader.php @@ -4,16 +4,23 @@ use function file_get_contents; use function is_file; +use function stream_resolve_include_path; class FileReader { public static function read(string $fileName): string { - if (!is_file($fileName)) { - throw new CouldNotReadFileException($fileName); + $path = $fileName; + + if (!is_file($path)) { + $path = stream_resolve_include_path($fileName); + + if ($path === false) { + throw new CouldNotReadFileException($fileName); + } } - $contents = @file_get_contents($fileName); + $contents = @file_get_contents($path); if ($contents === false) { throw new CouldNotReadFileException($fileName); } diff --git a/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php b/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php index 61007436a5..10e2f78e54 100644 --- a/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php +++ b/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php @@ -3,7 +3,9 @@ namespace PHPStan\Reflection\BetterReflection\SourceLocator; use PHPStan\ShouldNotHappenException; +use function is_file; use function stat; +use function stream_resolve_include_path; use function stream_wrapper_register; use function stream_wrapper_restore; use function stream_wrapper_unregister; @@ -94,11 +96,15 @@ public static function withStreamWrapperOverride( */ public function stream_open($path, $mode, $options, &$openedPath): bool { - self::$autoloadLocatedFiles[] = $path; + $exists = is_file($path) || (stream_resolve_include_path($path) !== false); + + if ($exists) { + self::$autoloadLocatedFiles[] = $path; + } $this->readFromFile = false; $this->seekPosition = 0; - return true; + return $exists; } /**