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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ jobs:
cd e2e/composer-version-config
composer install
../../bin/phpstan analyze test.php --level=0

- script: |
cd e2e/bug-12972b
composer install
../../bin/phpstan analyze
steps:
- name: "Checkout"
uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions e2e/bug-12972b/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
9 changes: 9 additions & 0 deletions e2e/bug-12972b/autoloader.php
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');
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

}
});
7 changes: 7 additions & 0 deletions e2e/bug-12972b/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"autoload": {
"classmap": [
"src/"
]
}
}
8 changes: 8 additions & 0 deletions e2e/bug-12972b/phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 9

paths:
- src

bootstrapFiles:
- autoloader.php
6 changes: 6 additions & 0 deletions e2e/bug-12972b/real-world.php
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());
9 changes: 9 additions & 0 deletions e2e/bug-12972b/src/folder/file2.php
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 {}
}
10 changes: 10 additions & 0 deletions e2e/bug-12972b/src/other/file.php
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;
}
}
Loading