Skip to content

Commit

Permalink
Support autoloaders that rely on the include path and don't check fil…
Browse files Browse the repository at this point in the history
…e existence before trying to include a file
  • Loading branch information
pprkut authored and ondrejmirtes committed Sep 22, 2022
1 parent a628fb3 commit b0babd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/File/FileReader.php
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit b0babd0

Please sign in to comment.