Skip to content

Reproduce autoloader bug #4003

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

Draft
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Draft

Reproduce autoloader bug #4003

wants to merge 2 commits into from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented May 21, 2025

this test demonstrates how PHPStan analyzes code differently than it would happen at runtime.


runtime: run php real-world.php
you see the program runs, without the custom autoloader of e2e/bug-12972b/autoloader.php beeing invoked.
all involved classes are autoloaded by the composer class loader. the custom class loader is not invoked for \other12972\MyClass::class.


static analysis time: run ../../bin/phpstan analyze

you see PHPStan invokes the custom class loader for class \other12972\MyClass::class, which does not happen at regular runtime. the composer class loader is not invoked for \other12972\MyClass::class (like it is at runtime).

because of the difference in autoloading functions order in runtime vs. analysis time we see errors which cannot happen at runtime - see phpstan/phpstan#12972


spl_autoload_register(function($class) {
if ($class === \other12972\MyClass::class) {
throw new LogicException('this should not happen');
Copy link
Contributor Author

@staabm staabm May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in our real world project this autoloader is scanning some paths in the filesystem (similar to how a include-path worked in the past / or how the %PATH% variable works in a shell).

since it scans the filesystem it finds some files and includes them. this in turn leads to the problem described in phpstan/phpstan#12972 -> so invoking the autoloader leads side-effects, as files get included, which has impact on ReflectionClass->getFileName() invoked later on in PHPStan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant