Allow autoloaders to require/require_once for #4836 via a fake read #491
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
PHPStan issue #4836 occurs when a custom autoloader uses
require
orrequire_once
and PHPStan tries to process a file that contains an anonymous class definition with dependencies that must be autoloaded (see issue for details).Approach
The goal of
FileReadTrapStreamWrapper
is to use a custom autoloader's knowledge of where to find a file, grab the path, and shut down the rest of theinclude
by returningfalse
fromstream_open
.This PR allows
require
andrequire_once
to be used in a custom autoloader by allowing the entire file read to occur, with an empty string returned fromstream_read()
to serve as dummy data that is also valid PHP.Testing
I ran the complete PHPStan test suite and observed no errors. I also confirmed that the updated code succeeded in my sample repository.
Potential issues
Since we simulate a file read from beginning to end,
include_once
andrequire_once
will not try to load the same path again. I think this is okay because I assume PHPStan loads the file itself later.