Repository navigation
Description
Bug report
Since 2.3.0, analysing a file that only uses a class (a parameter type, Foo::class, new Foo()) makes PHPStan load every class named in the return types of that class's @method tags. When those classes are large, a single trivial file costs hundreds of megabytes.
A real-world case is Aws\Sdk from aws/aws-sdk-php: it declares 870 @method tags (createS3(), createMultiRegionS3(), ...) that return 435 different Aws\*\*Client classes. Analysing one file that references Aws\Sdk takes over 800 MB on 2.3.x and about 50 MB on 2.2.17. In a parallel run with --memory-limit=1G, the worker that analyses such a file crashes with "PHPStan process crashed because it reached configured PHP memory limit".
Those classes are not analysed, and nothing in the analysed code calls the magic methods.
Measured
--debug, memory reported on the --- consumed line for src/uses.php from the reproduction below, aws/aws-sdk-php 3.403.4, 435 tags:
| PHPStan | PHP | consumed | peak |
|---|---|---|---|
| 2.2.17 | 8.5.11 NTS, macOS arm64 | 50.89 MB | 90.91 MB |
| 2.3.5 | 8.5.11 NTS, macOS arm64 | 826 MB | 866 MB |
Earlier measurements on PHP 8.5.12 ZTS, Linux arm64, aws/aws-sdk-php 3.403.3: 52.89 MB on every release from 2.2.13 to 2.2.17, 844 MB on 2.3.0, 842 MB on 2.3.5 and 838 MB on 2.3.x-dev (6379c5a). That is roughly 1.8 MB per class named in the tags.
With the real class instead of the generated one (function useIt(\Aws\Sdk $sdk): int, no scanFiles), 2.3.5 reports 838 MB for the file.
Suspected cause
This is a guess from reading the code, not a bisect to a commit.
DependencyResolver walks the @method tags (and other class-level PHPDoc tags) of a class and calls ReflectionProvider::getClass() for every class they reference. That walk already existed in 2.2.x. In 2.3.0, phpstan/phpstan-src#6662 ("Return dependencies in the results of the handlers") changed how a file's dependencies are collected. It looks like a class that is only referenced (here as a parameter type) is now expanded through its PHPDoc tags, so every class named in its @method tags is reflected and its file parsed.
Possibly related reports about eager class reflection with a different trigger (two-element arrays): #15292, #15494.
Code snippet that reproduces the problem
This cannot be shown on the playground: it needs a vendor package with large classes and a memory measurement. Minimal steps:
mkdir repro && cd repro
composer require --dev phpstan/phpstan:2.3.5 aws/aws-sdk-php
mkdir scan srcgenerate.php writes scan/ManyMethods.php with one @method tag per AWS client class:
<?php
$sdk = file_get_contents(__DIR__ . '/vendor/aws/aws-sdk-php/src/Sdk.php');
preg_match_all('~@method \\\\(Aws\\\\[\w\\\\]+Client) create(\w+)\(~', $sdk, $matches, PREG_SET_ORDER);
$tags = [];
foreach ($matches as $i => [, $class, $name]) {
if (str_starts_with($name, 'MultiRegion')) {
continue;
}
$tags[] = sprintf(' * @method \\%s make%d()', $class, $i);
}
file_put_contents(__DIR__ . '/scan/ManyMethods.php', "<?php\n\n/**\n" . implode("\n", $tags) . "\n */\nfinal class ManyMethods\n{\n}\n");
echo count($tags), " tags\n";src/uses.php, the only analysed file:
<?php
declare(strict_types=1);
function useIt(ManyMethods $m): int
{
return 1;
}phpstan.neon:
parameters:
level: 9
scanFiles:
- scan/ManyMethods.phpRun:
php generate.php
php -d memory_limit=4G vendor/bin/phpstan analyse --debug -vvv --memory-limit=4G -c phpstan.neon src/uses.php | grep consumedTo compare, use phpstan/phpstan:2.2.17 in the composer require line and run vendor/bin/phpstan clear-result-cache -c phpstan.neon between runs.
Expected output
Using a class whose @method tags name many other classes costs about the same as on 2.2.x. Recording a result cache dependency on those classes should not require loading and parsing each of them, at least not for classes that are only referenced and not analysed.
Actual: every class in the @method return types is loaded, and one trivial file takes over 800 MB. Staying on 2.2.17 avoids it.
Activity
footballencarta commentedon Nov 26, 2026
I can confirm this with the real
Aws\Sdkclass, and it is still present in 2.3.7. I bisected it: there are two separate causes, and neither is phpstan-src#6662.Environment: PHP 8.5.13 NTS, macOS arm64. aws/aws-sdk-php 3.408.0, where
Sdk.phphas 890@methodtags.Per release
The analysed file only calls
$sdk->createClient('sqs')on anAws\Sdkparameter. The memory is--- consumedfrom--debug -vvv:Bisect
The test file and config are the same as in the report,
function useIt(\Aws\Sdk $sdk): int { return 1; }, with the SDK'ssrcinscanDirectories, built from phpstan-src.1.
e7b7578fd"DeclarationDependencyTracker for extensions describing a class" (first in 2.3.0). Its parent uses 80 MB and the commit uses 900 MB.ValueDependencyCollector::noteClassDependency()calls$classReflection->getAncestors()for every class reflection returned byDependencyResolver. That includes every class named inAws\Sdk's@methodtags. With the loop removed, the commit uses 80 MB.2.
2decb5fe8"Track every class, function and constant lookup instead of re-analysing the files with errors" (first in 2.3.6). I bisectede7b7578fd..2.3.7with the loop from (1) removed at each step: the parent uses 76 MB and this commit uses 1.15 GB. The transitive walk inFileAnalyser::resolveDependenciesWithFoundSymbols()reaches the same classes again.On 2.3.7, each cause is enough by itself:
getAncestors()loop innoteClassDependency()removedwhile ($keysToFollow !== [] || $includeAnalysis)loop disabledThese patches are only diagnostics: both of them break result-cache invalidation. I ruled out
ClassResultCacheValueExtension::getValue()andBetterReflectionProvider::recordMissingAncestors(), because short-circuiting them changes nothing.How it shows up with Larastan
In a Laravel app, the common route is
Aws\Laravel\AwsFacade::createClient('sqs'), because Larastan resolves the facade toAws\Sdk. A one-line file with that call costs 894 MB. A 10-file project fails at--memory-limit=512Mwith onlyChild process error: PHPStan process crashed because it reached configured PHP memory limit, and the error does not name the file.Workaround
A stub that replaces the class PHPDoc brings the file back to 30 MB:
Without the empty
AwsClientInterfacedeclaration, stub validation reportsclass.notFoundforAws\AwsClientInterface, although the class exists in the vendor directory.