Skip to content

2.2.13: "Cannot redeclare function" fatal error with php-standard-library/type package #15184

Closed
phpstan/phpstan-src
#6431

Description

@theodorejb

Bug report

PHPStan 2.2.12 works fine, but after updating to v2.2.13 phpstan analyze crashes with the following error on a project requiring php-standard-library/type ^6.2:

 -- -------------------------------------------------------------------------
     Error
 -- -------------------------------------------------------------------------
     Child process error (exit code 255): Fatal error: Cannot redeclare
     function Psl\Type\optional() (previously declared in
     .../vendor/php-standard-library/type/src/Psl/Type/optional.php:18) in
     .../vendor/php-standard-library/type/src/Psl/Type/optional.php on line 18
     Stack trace:
     #0 phar://.../phpstan.phar/vendor/composer/ClassLoader.php(427):
        {closure:Composer\Autoload\ClassLoader::initializeIncludeClosure():575}('...')
     #1 phar://.../src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php(271):
        Composer\Autoload\ClassLoader->loadClass('Psl\Type\optional')
     #2 phar://.../src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php(69):
        AutoloadSourceLocator::{closure:...locateClassByName():264}()
     #3 phar://.../src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php(264):
        FileReadTrapStreamWrapper::withStreamWrapperOverride(Object(Closure))
     ...
     #10 phar://.../src/Reflection/ReflectionProvider/MemoizingReflectionProvider.php(38):
        BetterReflectionProvider->hasClass('Psl\Type\optional')
     #11 phar://.../src/Reflection/ClassReflection.php(878):
        MemoizingReflectionProvider->hasClass('Psl\Type\optional')
     #12 [internal function]: ClassReflection->is('Psl\Type\optional')
     #13 phar://.../src/Analyser/MutatingScope.php(2056):
        PHPStanTurbo\ScopeOps::invalidateExpressionEntries(...)

It looks like this is caused by 2.2.13 spawning workers with OPcache enabled, which breaks FileReadTrapStreamWrapper. OPcache serves an already-cached file from shared memory without going through the stream wrapper, so the autoloader's include re-executes the file and crashes.

Code snippet that reproduces the problem

No response

Expected output

No error.

Environment

  • PHPStan 2.2.13 (phar, installed via Composer), config includes php-standard-library/phpstan-extension ^2.1, level: 8
  • PHP 8.5.9 NTS, VC++ 2022 x64, Windows 11 Pro
  • php-standard-library/type 6.2

Did PHPStan help you today? Did it make you happy in any way?

Thank you for all the efforts to improve quality and performance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions