Skip to content

Commit db3fa23

Browse files
SanderMullerclaude
andcommitted
Invalidate the OPcache entries of included files before the autoload trap runs the autoloaders
AutoloadSourceLocator runs the autoloaders inside FileReadTrapStreamWrapper, which serves an empty script for each file an autoloader includes. When the file was already included for real, OPcache has compiled the real file. With OPcache active, the trapped include runs that script, and the process dies with "Cannot redeclare". Two ways to get there: - Composer's "files" autoload loads a file in a PSR-4 directory, and PHPStan asks for a class with the name of its function. - A bootstrap autoloader includes a file for real that does not declare the class. OPcache is active in the --debug main process and in spawned workers, because the process restart switches it on. Forked workers switch it off, so a default run on Linux or macOS does not crash. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 579a7f1 commit db3fa23

11 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ jobs:
254254
cd e2e/bug-15102c
255255
composer install
256256
../../bin/phpstan analyze
257+
- script: |
258+
cd e2e/composer-files-in-psr4-directory
259+
composer install
260+
# The crash needs OPcache: forked workers switch it off, the --debug main process and spawned
261+
# workers (pcntl_fork disabled, as on Windows) keep it on.
262+
php -r 'exit(extension_loaded("Zend OPcache") ? 0 : 1);'
263+
../../bin/phpstan analyze --debug
264+
php -d disable_functions=pcntl_fork ../../bin/phpstan analyze
265+
- script: |
266+
cd e2e/bootstrap-autoloader-wrong-file
267+
php -r 'exit(extension_loaded("Zend OPcache") ? 0 : 1);'
268+
../../bin/phpstan analyze --debug
269+
php -d disable_functions=pcntl_fork ../../bin/phpstan analyze
257270
- script: |
258271
cd e2e/bug-14724
259272
composer install
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
// A hand-written autoloader, not Composer's: it includes the file for the requested
4+
// name and does not check what the file declared.
5+
spl_autoload_register(static function (string $class): void {
6+
$prefix = 'BootstrapAutoloaderWrongFile\\';
7+
if (!str_starts_with($class, $prefix)) {
8+
return;
9+
}
10+
11+
$file = __DIR__ . '/src/' . substr($class, strlen($prefix)) . '.php';
12+
if (!is_file($file)) {
13+
return;
14+
}
15+
16+
require $file;
17+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
parameters:
2+
level: 8
3+
bootstrapFiles:
4+
- bootstrap.php
5+
paths:
6+
- test.php
7+
ignoreErrors:
8+
# The autoloader includes src/Widget.php for this name, and the file declares Widgets.
9+
# The analysis must report the unknown class, not die re-including that file.
10+
-
11+
message: '#^Parameter \$widget of method BootstrapAutoloaderWrongFile\\Consumer\:\:doFoo\(\) has invalid type BootstrapAutoloaderWrongFile\\Widget\.$#'
12+
identifier: class.notFound
13+
count: 1
14+
path: test.php
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace BootstrapAutoloaderWrongFile;
4+
5+
// The declared name does not match the file name, so the autoloader includes this
6+
// file for BootstrapAutoloaderWrongFile\Widget and Widget is never declared.
7+
class Widgets
8+
{
9+
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace BootstrapAutoloaderWrongFile;
4+
5+
class Consumer
6+
{
7+
8+
public function doFoo(Widget $widget): void
9+
{
10+
}
11+
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"autoload": {
3+
"psr-4": {
4+
"ComposerFilesInPsr4Directory\\": "src/"
5+
},
6+
"files": [
7+
"src/helpers.php"
8+
]
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- test.php
5+
ignoreErrors:
6+
# The analysis must report the unknown class, not die including src/helpers.php again.
7+
-
8+
message: '#^Parameter \$helpers of function ComposerFilesInPsr4Directory\\doFoo\(\) has invalid type ComposerFilesInPsr4Directory\\helpers\.$#'
9+
identifier: class.notFound
10+
count: 1
11+
path: test.php
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ComposerFilesInPsr4Directory;
4+
5+
// Composer's "files" autoload includes this file before the analysis starts. The PSR-4 map
6+
// also sends the class name ComposerFilesInPsr4Directory\helpers to it.
7+
function helpers(): void
8+
{
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ComposerFilesInPsr4Directory;
4+
5+
// The function name used as a class name makes PHPStan look for a class of that name.
6+
function doFoo(helpers $helpers): void
7+
{
8+
}

0 commit comments

Comments
 (0)