How to fix: weird phpstan internal errors from class loading when running analyse

Lets say you use phpstan and you bootstrap the symfony application for phpstan (or doctrine-extension of phpstan). You can get errors like this:

Internal error: Internal error: The autoloader expected class “Hoa\Zformat\Parameter” to be defined in file “phar:///app/vendor/phpstan/phpstan/phpstan.phar/vendor/composer/../hoa/zformat/Parameter.php”. The file was found but the class was not in it, the class name or namespace probably has a typo. in file
/app/src/php/WebsiteBundle/Model/StoryBlok/Story.php
Run PHPStan with — debug option and post the stack trace to:

Child process error (exit code 1):

S had this with the doctrine extension and this error:

The autoloader expected class "Stringable" to be defined in file "/home/joris/vhosts/wachtrij/backend/vendor/composer/../symfony/polyfill-php80/Resources/stubs/Stringable.php". The file was found but the class was not in it, the class name or namespace probably has a typo.

So I guess it can be any class that is “not found”. Problem here is that the Symfony debug class loader is a bit confused, when its run inside the phar that is in turn called WHILE analysing with phpstan. That way its very random in which file it’s happening, too.

The fix is pretty easy (luckily), in the script that you reference in your phpstan config to bootstrap either the doctrine manager or the symfony console application: make sure to remove the code:

Debug::enable();

that’s all. That will not use the debug class loader and make it fail to search inside a phar.

--

--

Senior Web Developer — Entrepreneur — yaymemories.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store