From ca2df5bbb3227eea534d65283b0ecf6561bbd73d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 26 Nov 2025 08:26:03 +0100 Subject: [PATCH 1/4] cache files in OptimizedDirectorySourceLocatorFactory --- ...OptimizedDirectorySourceLocatorFactory.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php index 5803517e63..bd9eed3615 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php @@ -10,11 +10,13 @@ use PHPStan\Reflection\ConstantNameHelper; use function array_key_exists; use function count; +use function implode; use function in_array; use function ltrim; use function php_strip_whitespace; use function preg_match_all; use function preg_replace; +use function sha1; use function sha1_file; use function sprintf; use function strtolower; @@ -52,6 +54,11 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo } $cacheKey = sprintf('odsl-%s', $directory); + return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey); + } + + public function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator + { $variableCacheKey = 'v1'; /** @var array|null $cached */ @@ -96,20 +103,17 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo */ public function createByFiles(array $files): 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, - ); + $cacheKey = sprintf('odsl-files-%s', sha1(implode(',', $files))); + return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey); } /** From 8377d0d51ceaebcffc76f4eb2d6c2954c30fdeff Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 26 Nov 2025 08:33:04 +0100 Subject: [PATCH 2/4] fix build --- .../SourceLocator/OptimizedDirectorySourceLocatorFactory.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php index bd9eed3615..d38ed77d3d 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php @@ -57,6 +57,10 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey); } + /** + * @param array $fileHashes + * @param non-empty-string $cacheKey + */ public function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator { $variableCacheKey = 'v1'; From 3d87e3e1bf3f296e1ebc04df3e0e5d3cbcd35922 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 27 Nov 2025 08:49:56 +0100 Subject: [PATCH 3/4] reduce visibility --- .../SourceLocator/OptimizedDirectorySourceLocatorFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php index d38ed77d3d..3332a23639 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php @@ -61,7 +61,7 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo * @param array $fileHashes * @param non-empty-string $cacheKey */ - public function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator + private function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator { $variableCacheKey = 'v1'; From 96a169d027dd3dfddd55bcd8c76d36fb4736dccc Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 27 Nov 2025 10:38:34 +0100 Subject: [PATCH 4/4] use call-site cache identifier --- .../ComposerJsonAndInstalledJsonSourceLocatorMaker.php | 2 +- .../OptimizedDirectorySourceLocatorFactory.php | 8 +++----- .../SourceLocator/OptimizedDirectorySourceLocatorTest.php | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) 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 3332a23639..c64ff2dd2b 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php @@ -10,13 +10,11 @@ use PHPStan\Reflection\ConstantNameHelper; use function array_key_exists; use function count; -use function implode; use function in_array; use function ltrim; use function php_strip_whitespace; use function preg_match_all; use function preg_replace; -use function sha1; use function sha1_file; use function sprintf; use function strtolower; @@ -104,8 +102,9 @@ private function createCachedDirectorySourceLocator(array $fileHashes, string $c /** * @param string[] $files + * @param non-empty-string&literal-string $uniqueCacheIdentifier */ - public function createByFiles(array $files): OptimizedDirectorySourceLocator + public function createByFiles(array $files, string $uniqueCacheIdentifier): OptimizedDirectorySourceLocator { $fileHashes = []; foreach ($files as $file) { @@ -116,8 +115,7 @@ public function createByFiles(array $files): OptimizedDirectorySourceLocator $fileHashes[$file] = $hash; } - $cacheKey = sprintf('odsl-files-%s', sha1(implode(',', $files))); - return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey); + 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');