Skip to content

Commit 5c50118

Browse files
schlndhondrejmirtes
authored andcommitted
optimize repeated container creation in tests
1 parent be279db commit 5c50118

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Testing/TestCase.neon

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ services:
99
arguments:
1010
phpParser: @phpParserDecorator
1111
php8Parser: @php8PhpParser
12+
fileExtensions: %fileExtensions%
13+
obsoleteExcludesAnalyse: %excludes_analyse%
14+
excludePaths: %excludePaths%
1215

1316
cacheStorage:
1417
class: PHPStan\Cache\MemoryCacheStorage

src/Testing/TestCaseSourceLocatorFactory.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,39 @@
1212
use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
1313
use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
1414
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
15+
use PHPStan\Php\PhpVersion;
1516
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
1617
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
1718
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
1819
use PHPStan\Reflection\BetterReflection\SourceLocator\PhpVersionBlacklistSourceLocator;
1920
use ReflectionClass;
2021
use function dirname;
2122
use function is_file;
23+
use function serialize;
24+
use function sha1;
2225

2326
class TestCaseSourceLocatorFactory
2427
{
2528

29+
/** @var array<string, list<SourceLocator>> */
30+
private static array $composerSourceLocatorsCache = [];
31+
32+
/**
33+
* @param string[] $fileExtensions
34+
* @param string[] $obsoleteExcludesAnalyse
35+
* @param array{analyse?: array<int, string>, analyseAndScan?: array<int, string>}|null $excludePaths
36+
*/
2637
public function __construct(
2738
private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker,
2839
private Parser $phpParser,
2940
private Parser $php8Parser,
3041
private FileNodesFetcher $fileNodesFetcher,
3142
private PhpStormStubsSourceStubber $phpstormStubsSourceStubber,
3243
private ReflectionSourceStubber $reflectionSourceStubber,
44+
private PhpVersion $phpVersion,
45+
private array $fileExtensions,
46+
private array $obsoleteExcludesAnalyse,
47+
private ?array $excludePaths,
3348
)
3449
{
3550
}
@@ -38,8 +53,14 @@ public function create(): SourceLocator
3853
{
3954
$classLoaders = ClassLoader::getRegisteredLoaders();
4055
$classLoaderReflection = new ReflectionClass(ClassLoader::class);
41-
$locators = [];
42-
if ($classLoaderReflection->hasProperty('vendorDir')) {
56+
$cacheKey = sha1(serialize([
57+
$this->phpVersion->getVersionId(),
58+
$this->fileExtensions,
59+
$this->obsoleteExcludesAnalyse,
60+
$this->excludePaths,
61+
]));
62+
if ($classLoaderReflection->hasProperty('vendorDir') && ! isset(self::$composerSourceLocatorsCache[$cacheKey])) {
63+
$composerLocators = [];
4364
$vendorDirProperty = $classLoaderReflection->getProperty('vendorDir');
4465
$vendorDirProperty->setAccessible(true);
4566
foreach ($classLoaders as $classLoader) {
@@ -52,10 +73,13 @@ public function create(): SourceLocator
5273
if ($composerSourceLocator === null) {
5374
continue;
5475
}
55-
$locators[] = $composerSourceLocator;
76+
$composerLocators[] = $composerSourceLocator;
5677
}
78+
79+
self::$composerSourceLocatorsCache[$cacheKey] = $composerLocators;
5780
}
5881

82+
$locators = self::$composerSourceLocatorsCache[$cacheKey] ?? [];
5983
$astLocator = new Locator($this->phpParser);
6084
$astPhp8Locator = new Locator($this->php8Parser);
6185

0 commit comments

Comments
 (0)