Skip to content

Commit 522421b

Browse files
authored
Cache files in OptimizedDirectorySourceLocatorFactory
1 parent 99041a3 commit 522421b

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function create(string $projectInstallationPath): ?SourceLocator
129129
}
130130

131131
if (count($files) > 0) {
132-
$locators[] = $this->optimizedDirectorySourceLocatorFactory->createByFiles($files);
132+
$locators[] = $this->optimizedDirectorySourceLocatorFactory->createByFiles($files, 'odsl-installed-files');
133133
}
134134

135135
$binDir = ComposerHelper::getBinDirFromComposerConfig($projectInstallationPath, $composer);

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo
5252
}
5353

5454
$cacheKey = sprintf('odsl-%s', $directory);
55+
return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey);
56+
}
57+
58+
/**
59+
* @param array<string, string> $fileHashes
60+
* @param non-empty-string $cacheKey
61+
*/
62+
private function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator
63+
{
5564
$variableCacheKey = 'v1';
5665

5766
/** @var array<string, array{string, string[], string[], string[]}>|null $cached */
@@ -93,23 +102,20 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo
93102

94103
/**
95104
* @param string[] $files
105+
* @param non-empty-string&literal-string $uniqueCacheIdentifier
96106
*/
97-
public function createByFiles(array $files): OptimizedDirectorySourceLocator
107+
public function createByFiles(array $files, string $uniqueCacheIdentifier): OptimizedDirectorySourceLocator
98108
{
99-
$symbols = [];
109+
$fileHashes = [];
100110
foreach ($files as $file) {
101-
[$newClasses, $newFunctions, $newConstants] = $this->findSymbols($file);
102-
$symbols[$file] = ['', $newClasses, $newFunctions, $newConstants];
111+
$hash = sha1_file($file);
112+
if ($hash === false) {
113+
continue;
114+
}
115+
$fileHashes[$file] = $hash;
103116
}
104117

105-
[$classToFile, $functionToFiles, $constantToFile] = $this->changeStructure($symbols);
106-
107-
return new OptimizedDirectorySourceLocator(
108-
$this->fileNodesFetcher,
109-
$classToFile,
110-
$functionToFiles,
111-
$constantToFile,
112-
);
118+
return $this->createCachedDirectorySourceLocator($fileHashes, $uniqueCacheIdentifier);
113119
}
114120

115121
/**

tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function testFunctionDoesNotExist(string $functionName): void
307307
public function testBug5525(): void
308308
{
309309
$factory = self::getContainer()->getByType(OptimizedDirectorySourceLocatorFactory::class);
310-
$locator = $factory->createByFiles([__DIR__ . '/data/bug-5525.php']);
310+
$locator = $factory->createByFiles([__DIR__ . '/data/bug-5525.php'], 'bug5525-odsl-installed-files');
311311
$reflector = new DefaultReflector($locator);
312312

313313
$class = $reflector->reflectClass('Faker\\Provider\\nl_BE\\Text');

0 commit comments

Comments
 (0)