Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize repeated container creation in tests #2860

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
30 changes: 27 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])) {
$composerLocators = [];
$vendorDirProperty = $classLoaderReflection->getProperty('vendorDir');
$vendorDirProperty->setAccessible(true);
foreach ($classLoaders as $classLoader) {
Expand All @@ -52,10 +73,13 @@ public function create(): SourceLocator
if ($composerSourceLocator === null) {
continue;
}
$locators[] = $composerSourceLocator;
$composerLocators[] = $composerSourceLocator;
}

self::$composerSourceLocatorsCache[$cacheKey] = $composerLocators;
}

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

Expand Down