Allow autoloaders to require/require_once for #4836 via a fake read#491
Merged
ondrejmirtes merged 1 commit intophpstan:masterfrom Apr 11, 2021
Merged
Allow autoloaders to require/require_once for #4836 via a fake read#491ondrejmirtes merged 1 commit intophpstan:masterfrom
ondrejmirtes merged 1 commit intophpstan:masterfrom
Conversation
Member
|
I copied your test project to PHPStan's E2E tests: phpstan/phpstan@2bef7e4 After it fails the build, I'll merge this. Thank you very much! |
Member
|
BTW Feel free to also submit this to https://github.com/Roave/BetterReflection, thank you. |
dktapps
reviewed
Nov 24, 2021
| // Dummy return value that is also valid PHP for require(). We'll read | ||
| // and process the file elsewhere, so it's OK to provide dummy data for | ||
| // this read. | ||
| return ''; |
Contributor
There was a problem hiding this comment.
This causes problems when OPcache is enabled. OPcache will remember this empty string when the file is next autoloaded (which might not be within FileReadTrapStreamWrapper).
Contributor
There was a problem hiding this comment.
It also causes problems when require_once or include_once is used, regardless of whether opcache is enabled or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
requireorrequire_onceand 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
FileReadTrapStreamWrapperis to use a custom autoloader's knowledge of where to find a file, grab the path, and shut down the rest of theincludeby returningfalsefromstream_open.This PR allows
requireandrequire_onceto 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_onceandrequire_oncewill not try to load the same path again. I think this is okay because I assume PHPStan loads the file itself later.