phpstan: PHP Fatal error: Allowed memory size of XXXXX bytes exhausted

Bug report

php ./vendor/bin/phpstan --version

PHPStan - PHP Static Analysis Tool 1.9.3

php -v

PHP 8.1.13 (cli) (built: Nov 26 2022 14:07:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies

When parsing some PHP files it stuck with a recursive load and memory is drained:

php ./vendor/bin/phpstan analyse app --memory-limit=1G 
Note: Using configuration file /home/lito/www/phpstan-memory-exhausted/phpstan.neon.
 6/6 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 -- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                                                                                                                            
 -- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
     Child process error (exit code 255): PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 262144 bytes) in                                                                     
     phar:///home/lito/www/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php on line 559                                                                                
                                                                                                                                                                                                                      
     PHPStan process crashed because it reached configured PHP memory limit: 1G                                                                                                                                       
     Increase your memory limit in php.ini or run PHPStan with --memory-limit CLI option.                                                                                                                             
     PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 2621440 bytes) in /home/lito/www/phpstan-memory-exhausted/vendor/symfony/console/Output/AnsiColorMode.php on line 73  
                                                                                                                                                                                                                      
 -- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

Code snippet that reproduces the problem

git clone https://github.com/eusonlito/phpstan-memory-exhausted.git

cd phpstan-memory-exhausted

composer install

php ./vendor/bin/phpstan analyse app --memory-limit=1G
  • Deleting methods payloadRow, payloadRowIsSame and payloadRowOther at app/Domains/Log/Model/Traits/Payload.php it works
  • Deleting database migration table log at database/migrations/2021_01_14_000000_base.php it works

Expected output

Finish without error

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@eusonlito Thank you for your kind words! 😃

Fixed the problem: https://github.com/phpstan/phpstan-src/commit/a7fed03bbf1bef545c8afcbf6c906ac93b34c876

@rajyan I had to modify some conditional types tests, the results are a bit worse now, but I think the code makes more sense - if we’re assigning a variable, we want to invalidate everything.

This is the snippet of the stack trace of the infinite recursion:

#14 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3370): PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs(Object(PHPStan\Analyser\MutatingScope), Array, Array)
#15 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1403): PHPStan\Analyser\MutatingScope->methodCallReturnType(Object(PHPStan\Type\Generic\GenericObjectType), 'first', Object(PhpParser\Node\Expr\MethodCall))
#16 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1409): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()
#17 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(559): PHPStan\Analyser\MutatingScope->resolveType('$this->related-...', Object(PhpParser\Node\Expr\MethodCall))
#18 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Properties/PropertyReflectionFinder.php(73): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))
#19 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2484): PHPStan\Rules\Properties\PropertyReflectionFinder->findPropertyReflectionFromNode(Object(PhpParser\Node\Expr\PropertyFetch), Object(PHPStan\Analyser\MutatingScope))
#20 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2446): PHPStan\Analyser\MutatingScope->shouldInvalidateExpression('$value', Object(PhpParser\Node\Expr\Variable), Object(PhpParser\Node\Expr\PropertyFetch), true)
#21 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2434): PHPStan\Analyser\MutatingScope->invalidateExpression(Object(PhpParser\Node\Expr\Variable), true)
#22 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2359): PHPStan\Analyser\MutatingScope->assignExpression(Object(PhpParser\Node\Expr\Variable), Object(PHPStan\Type\MixedType), Object(PHPStan\Type\MixedType))
#23 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2218): PHPStan\Analyser\MutatingScope->assignVariable('value', Object(PHPStan\Type\MixedType), Object(PHPStan\Type\MixedType))
#24 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1045): PHPStan\Analyser\MutatingScope->enterArrowFunctionWithoutReflection(Object(PhpParser\Node\Expr\ArrowFunction), NULL)
#25 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(559): PHPStan\Analyser\MutatingScope->resolveType('fn($value) => $...', Object(PhpParser\Node\Expr\ArrowFunction))
#26 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ParametersAcceptorSelector.php(128): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\ArrowFunction))
#27 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3370): PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs(Object(PHPStan\Analyser\MutatingScope), Array, Array)