Skip to content

Commit 3422c33

Browse files
staabmphpstan-bot
authored andcommitted
Add regression test for static caching of FunctionSignatureMapProvider
- New test in tests/PHPStan/Reflection/SignatureMap/FunctionSignatureMapProviderTest.php - Verifies that creating a second FunctionSignatureMapProvider instance reuses the static cache instead of reloading the ~7MB functionMap.php file - The fix (commit 394064b) changed signatureMaps and functionMetadata from instance properties to static properties, preventing repeated loading in test suites Closes phpstan/phpstan#10039
1 parent de68533 commit 3422c33

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\SignatureMap;
4+
5+
use PHPStan\Php\PhpVersion;
6+
use PHPStan\Reflection\InitializerExprTypeResolver;
7+
use PHPStan\Testing\PHPStanTestCase;
8+
use function memory_get_usage;
9+
10+
class FunctionSignatureMapProviderTest extends PHPStanTestCase
11+
{
12+
13+
/**
14+
* Regression test for https://github.com/phpstan/phpstan/issues/10039
15+
*
16+
* The signature map and function metadata should be cached in static properties
17+
* so the large functionMap.php file is loaded once and shared across all instances.
18+
* Without static caching, each new DI container (e.g., in test suites) would reload
19+
* the entire ~7MB functionMap.php, causing excessive memory usage.
20+
*/
21+
public function testBug10039(): void
22+
{
23+
$parser = self::getContainer()->getByType(SignatureMapParser::class);
24+
$initializerResolver = self::getContainer()->getByType(InitializerExprTypeResolver::class);
25+
$phpVersion = new PhpVersion(80200);
26+
27+
// Create first instance and load the signature map
28+
$provider1 = new FunctionSignatureMapProvider($parser, $initializerResolver, $phpVersion, false);
29+
$provider1->getSignatureMap();
30+
31+
// Create a second instance with the same parameters
32+
$provider2 = new FunctionSignatureMapProvider($parser, $initializerResolver, $phpVersion, false);
33+
34+
// With static caching, the second call should not allocate significant memory
35+
// because it returns the already-cached map. Without static caching (the bug),
36+
// each instance would load and process the entire functionMap.php (~7MB).
37+
$memBefore = memory_get_usage();
38+
$provider2->getSignatureMap();
39+
$memDiff = memory_get_usage() - $memBefore;
40+
41+
$this->assertLessThan(
42+
1024 * 1024, // 1 MB - the full load uses ~7 MB
43+
$memDiff,
44+
'Second FunctionSignatureMapProvider instance should reuse the static cache, not reload functionMap.php',
45+
);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)