-
Notifications
You must be signed in to change notification settings - Fork 582
Consult bootstrap-registered custom autoloaders only after the static source locators #6069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phpstan-bot
wants to merge
5
commits into
phpstan:2.2.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-89ch8pu
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86862c1
Consult bootstrap-registered custom autoloaders only after the static…
staabm 7ddee27
move related lines in on block
staabm 90a36c4
Consult bootstrap autoloaders before/after Composer based on registra…
phpstan-bot 93f01c2
Add e2e project for a prepended bootstrap autoloader
phpstan-bot 1a07961
Require explicit $prependedToComposer argument on AutoloadFunctionsSo…
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /vendor/ | ||
| composer.lock |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| require_once __DIR__ . '/vendor/autoload.php'; | ||
|
|
||
| spl_autoload_register(function($class) { | ||
| if ($class === \other12972\MyClass::class) { | ||
| throw new LogicException('this should not happen'); | ||
| } | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "autoload": { | ||
| "classmap": [ | ||
| "src/" | ||
| ] | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| parameters: | ||
| level: 9 | ||
|
|
||
| paths: | ||
| - src | ||
|
|
||
| bootstrapFiles: | ||
| - autoloader.php |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?php | ||
|
|
||
| require 'autoloader.php'; | ||
|
|
||
| $root = new \Foo12972\MyRoot(); | ||
| $root->doBar(new \other12972\MyClass()); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| namespace Foo12972; | ||
|
|
||
| use other12972\MyClass; | ||
|
|
||
| class MyRoot { | ||
| function doBar(MyClass $myClass):void {} | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace other12972; | ||
|
|
||
| class MyClass { | ||
| public function doSomething(): int | ||
| { | ||
| return 1; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /vendor/ | ||
| composer.lock |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| require_once __DIR__ . '/vendor/autoload.php'; | ||
|
|
||
| // Registered *before* Composer's class loader (third argument = prepend). | ||
| // At runtime this resolves \shared12972c\Thing before Composer ever sees it, | ||
| // so PHPStan must consult it before the Composer class map as well. | ||
| spl_autoload_register(function ($class) { | ||
| if ($class === \shared12972c\Thing::class) { | ||
| require __DIR__ . '/prepended/Thing.php'; | ||
| } | ||
| }, true, true); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace shared12972c; | ||
|
|
||
| class Thing { | ||
| public function composerOnly(): int | ||
| { | ||
| return 1; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "autoload": { | ||
| "classmap": [ | ||
| "classmap/" | ||
| ] | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace consumer12972c; | ||
|
|
||
| use shared12972c\Thing; | ||
|
|
||
| function run(Thing $thing): int | ||
| { | ||
| return $thing->prependedOnly(); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| parameters: | ||
| level: 9 | ||
|
|
||
| paths: | ||
| - consumer | ||
|
|
||
| bootstrapFiles: | ||
| - autoloader.php |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace shared12972c; | ||
|
|
||
| class Thing { | ||
| public function prependedOnly(): int | ||
| { | ||
| return 2; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?php | ||
|
|
||
| require 'autoloader.php'; | ||
|
|
||
| $thing = new \shared12972c\Thing(); | ||
| echo $thing->prependedOnly(), "\n"; |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the current fix depends on the order in which autoloaders are registered within bootstrap scripts (and whether they are prepend'ed or not).
in case we change the
autoloader.phptowe would get back a similar runtime vs. static analysis inconsistency.
can we detect in which order autoloaders are registered in the bootstrap scripts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All verification passes. Here's a summary of what I did to address staabm's review.
The review concern
staabm pointed out that the PR's fix (moving all bootstrap autoloaders to the back of the source-locator chain) depends on registration order: an autoloader registered before Composer's class loader — e.g.
spl_autoload_register($fn, true, true)— runs before Composer at runtime, so consulting it after the static Composer locators reintroduces the same runtime-vs-analysis inconsistency in the opposite direction. They asked: can we detect in which order autoloaders are registered?What I implemented
Yes — I now detect each bootstrap autoloader's position relative to Composer's
ClassLoaderin thespl_autoloadqueue and split them into two groups:src/autoloadFunctions.php— newcollectNewAutoloadFunctions()helper that finds Composer'sClassLoaderin the queue and classifies each newly-registered autoloader asprepended(before Composer) orappended(after Composer), while still excluding Composer itself, the phar loader, and pre-existing autoloaders. Also a newautoloadFunctionsPrependedToComposer()accessor. The logic is robust to PHPStan's own devClassLoaderalso being in the queue (prepends always land at index 0; appends always at the end).bin/phpstan+src/Command/CommandHelper.php— both collection sites now use the shared helper and populate two globals instead of one. This also de-duplicates the two near-identical loops into one place.AutoloadFunctionsSourceLocator— gained a$prependedToComposerflag selecting which group to run.BetterReflectionSourceLocatorFactory— the prepended group is consulted at the front of the chain (before analysed files / Composer class-map / PSR-4), the appended group only as a fallback at the back — mirroring runtime order in both directions.Tests
tests/PHPStan/CollectNewAutoloadFunctionsTest.php— unit tests for the ordering logic (before/after Composer, exclusions, pre-existing filtering).e2e/bug-12972c— a discriminating e2e: a bootstrap autoloader prepended before Composer resolves a class Composer also maps but with a different API. At runtime the prepended version wins; I verified PHPStan now reflects that same version (calling the Composer-only method correctly errors, proving the prepend order is honored). Wired intoe2e-tests.yml.Verification
make phpstan— No errorsmake tests(paratest) — 17607 tests, exit 0.phpfiles — cleanbug-12972b(appended throwing autoloader) — still no crashBoth commits are pushed to
create-pull-request/patch-89ch8pu.