diff --git a/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php b/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php index 1195311fd2..83e7d86992 100644 --- a/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php +++ b/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php @@ -129,7 +129,7 @@ public function create(string $projectInstallationPath): ?SourceLocator } if (count($files) > 0) { - $locators[] = $this->optimizedDirectorySourceLocatorFactory->createByFiles($files); + $locators[] = $this->optimizedDirectorySourceLocatorFactory->createByFiles($files, 'odsl-installed-files'); } $binDir = ComposerHelper::getBinDirFromComposerConfig($projectInstallationPath, $composer); diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php index 5803517e63..c64ff2dd2b 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php @@ -52,6 +52,15 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo } $cacheKey = sprintf('odsl-%s', $directory); + return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey); + } + + /** + * @param array $fileHashes + * @param non-empty-string $cacheKey + */ + private function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator + { $variableCacheKey = 'v1'; /** @var array|null $cached */ @@ -93,23 +102,20 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo /** * @param string[] $files + * @param non-empty-string&literal-string $uniqueCacheIdentifier */ - public function createByFiles(array $files): OptimizedDirectorySourceLocator + public function createByFiles(array $files, string $uniqueCacheIdentifier): OptimizedDirectorySourceLocator { - $symbols = []; + $fileHashes = []; foreach ($files as $file) { - [$newClasses, $newFunctions, $newConstants] = $this->findSymbols($file); - $symbols[$file] = ['', $newClasses, $newFunctions, $newConstants]; + $hash = sha1_file($file); + if ($hash === false) { + continue; + } + $fileHashes[$file] = $hash; } - [$classToFile, $functionToFiles, $constantToFile] = $this->changeStructure($symbols); - - return new OptimizedDirectorySourceLocator( - $this->fileNodesFetcher, - $classToFile, - $functionToFiles, - $constantToFile, - ); + return $this->createCachedDirectorySourceLocator($fileHashes, $uniqueCacheIdentifier); } /** diff --git a/tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorTest.php b/tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorTest.php index 06c9407908..30b555e7b3 100644 --- a/tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorTest.php +++ b/tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorTest.php @@ -307,7 +307,7 @@ public function testFunctionDoesNotExist(string $functionName): void public function testBug5525(): void { $factory = self::getContainer()->getByType(OptimizedDirectorySourceLocatorFactory::class); - $locator = $factory->createByFiles([__DIR__ . '/data/bug-5525.php']); + $locator = $factory->createByFiles([__DIR__ . '/data/bug-5525.php'], 'bug5525-odsl-installed-files'); $reflector = new DefaultReflector($locator); $class = $reflector->reflectClass('Faker\\Provider\\nl_BE\\Text');