Skip to content

Commit

Permalink
optimize repeated container creation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Jan 7, 2024
1 parent 53a61dc commit 53b24e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Testing/TestCase.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ services:
arguments:
phpParser: @phpParserDecorator
php8Parser: @php8PhpParser
fileExtensions: %fileExtensions%
obsoleteExcludesAnalyse: %excludes_analyse%
excludePaths: %excludePaths%

cacheStorage:
class: PHPStan\Cache\MemoryCacheStorage
Expand Down
28 changes: 25 additions & 3 deletions src/Testing/TestCaseSourceLocatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,39 @@
use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\BetterReflection\SourceLocator\PhpVersionBlacklistSourceLocator;
use ReflectionClass;
use function dirname;
use function is_file;
use function serialize;
use function sha1;

class TestCaseSourceLocatorFactory
{

/** @var array<string, list<SourceLocator>> */
private static array $composerSourceLocatorsCache = [];

/**
* @param string[] $fileExtensions
* @param string[] $obsoleteExcludesAnalyse
* @param array{analyse?: array<int, string>, analyseAndScan?: array<int, string>}|null $excludePaths
*/
public function __construct(
private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker,
private Parser $phpParser,
private Parser $php8Parser,
private FileNodesFetcher $fileNodesFetcher,
private PhpStormStubsSourceStubber $phpstormStubsSourceStubber,
private ReflectionSourceStubber $reflectionSourceStubber,
private PhpVersion $phpVersion,
private array $fileExtensions,
private array $obsoleteExcludesAnalyse,
private ?array $excludePaths,
)
{
}
Expand All @@ -38,8 +53,14 @@ public function create(): SourceLocator
{
$classLoaders = ClassLoader::getRegisteredLoaders();
$classLoaderReflection = new ReflectionClass(ClassLoader::class);
$locators = [];
if ($classLoaderReflection->hasProperty('vendorDir')) {
$cacheKey = sha1(serialize([
$this->phpVersion->getVersionId(),
$this->fileExtensions,
$this->obsoleteExcludesAnalyse,
$this->excludePaths,
]));
if ($classLoaderReflection->hasProperty('vendorDir') && ! isset(self::$composerSourceLocatorsCache[$cacheKey])) {
self::$composerSourceLocatorsCache[$cacheKey] = [];
$vendorDirProperty = $classLoaderReflection->getProperty('vendorDir');
$vendorDirProperty->setAccessible(true);
foreach ($classLoaders as $classLoader) {
Expand All @@ -52,10 +73,11 @@ public function create(): SourceLocator
if ($composerSourceLocator === null) {
continue;
}
$locators[] = $composerSourceLocator;
self::$composerSourceLocatorsCache[$cacheKey][] = $composerSourceLocator;
}
}

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

Expand Down

0 comments on commit 53b24e2

Please sign in to comment.