From ccf206355edcb215da7db06fae9238d07315b453 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 23 Jun 2022 14:30:02 +0200 Subject: [PATCH 1/6] use a separate resultCache per project config file --- .../ResultCache/ResultCacheClearer.php | 8 ++-- .../ResultCache/ResultCacheManager.php | 37 ++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Analyser/ResultCache/ResultCacheClearer.php b/src/Analyser/ResultCache/ResultCacheClearer.php index d511a3fa92..9fee37479e 100644 --- a/src/Analyser/ResultCache/ResultCacheClearer.php +++ b/src/Analyser/ResultCache/ResultCacheClearer.php @@ -17,11 +17,11 @@ public function __construct(private string $cacheFilePath, private string $tempR public function clear(): string { $dir = dirname($this->cacheFilePath); - if (!is_file($this->cacheFilePath)) { - return $dir; - } - @unlink($this->cacheFilePath); + $finder = new Finder(); + foreach ($finder->files()->depth(0)->name('resultCache*.php')->in($dir) as $resultCacheFile) { + @unlink($resultCacheFile->getPathname()); + } return $dir; } diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index 9631426b1b..c2ef32b808 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -27,11 +27,13 @@ use function array_unique; use function array_values; use function count; +use function dirname; use function get_loaded_extensions; use function is_array; use function is_file; use function is_string; use function ksort; +use function md5; use function sha1; use function sort; use function sprintf; @@ -99,11 +101,10 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? return new ResultCache($allAnalysedFiles, true, time(), $this->getMeta($allAnalysedFiles, $projectConfigArray), [], [], [], []); } - $cacheFilePath = $this->cacheFilePath; + $cacheFilePath = $this->getCacheFilePath($resultCacheName, $projectConfigArray); if ($resultCacheName !== null) { - $tmpCacheFile = $this->tempResultCachePath . '/' . $resultCacheName . '.php'; - if (is_file($tmpCacheFile)) { - $cacheFilePath = $tmpCacheFile; + if (!is_file($cacheFilePath)) { + $cacheFilePath = $this->cacheFilePath; } } @@ -578,16 +579,13 @@ private function save( ksort($exportedNodes); - $file = $this->cacheFilePath; - if ($resultCacheName !== null) { - $file = $this->tempResultCachePath . '/' . $resultCacheName . '.php'; - } - $projectConfigArray = $meta['projectConfig']; if ($projectConfigArray !== null) { $meta['projectConfig'] = Neon::encode($projectConfigArray); } + $file = $this->getCacheFilePath($resultCacheName, $projectConfigArray); + FileWriter::write( $file, sprintf( @@ -603,6 +601,27 @@ private function save( ); } + /** + * @param mixed[]|null $projectConfig + */ + private function getCacheFilePath(?string $resultCacheName, ?array $projectConfig): string + { + if ($resultCacheName !== null) { + return $this->tempResultCachePath . '/' . $resultCacheName . '.php'; + } + + if ($projectConfig !== null && + array_key_exists('parameters', $projectConfig) && + !array_key_exists('resultCachePath', $projectConfig['parameters']) && + (!array_key_exists('tmpDir', $projectConfig['parameters']) || $projectConfig['parameters']['tmpDir'] === null) + ) { + + return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, $this->bootstrapFiles])) . '.php'; + } + + return $this->cacheFilePath; + } + /** * @param mixed[]|null $projectConfig * @param array $dependencies From a633809599959b6f59dc818331ff71cba9a7df53 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 30 Jun 2022 17:00:21 +0200 Subject: [PATCH 2/6] cs --- src/Analyser/ResultCache/ResultCacheClearer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Analyser/ResultCache/ResultCacheClearer.php b/src/Analyser/ResultCache/ResultCacheClearer.php index 9fee37479e..03d7b43671 100644 --- a/src/Analyser/ResultCache/ResultCacheClearer.php +++ b/src/Analyser/ResultCache/ResultCacheClearer.php @@ -4,7 +4,6 @@ use Symfony\Component\Finder\Finder; use function dirname; -use function is_file; use function unlink; class ResultCacheClearer From bab9ff09b54cf4f7bee5a87de19c7074113dfb3a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 30 Jun 2022 17:16:16 +0200 Subject: [PATCH 3/6] test separate cache files are generated --- tests/e2e/ResultCacheEndToEndTest.php | 11 +++++++++++ tests/e2e/phpstan_separate_cache.neon | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/e2e/phpstan_separate_cache.neon diff --git a/tests/e2e/ResultCacheEndToEndTest.php b/tests/e2e/ResultCacheEndToEndTest.php index 3ba8efae97..62b6bf2d7f 100644 --- a/tests/e2e/ResultCacheEndToEndTest.php +++ b/tests/e2e/ResultCacheEndToEndTest.php @@ -10,9 +10,11 @@ use PHPUnit\Framework\TestCase; use function array_map; use function chdir; +use function count; use function escapeshellarg; use function exec; use function file_put_contents; +use function glob; use function implode; use function ksort; use function sort; @@ -136,6 +138,15 @@ public function testResultCachePath(): void $this->assertResultCache(__DIR__ . '/resultCache_1.php', sys_get_temp_dir() . '/phpstan/myResultCacheFile.php'); } + public function testSeparateResultCachePath(): void + { + $this->runPhpstan(0, __DIR__ . '/phpstan_separate_cache.neon'); + + $resultCacheFiles = glob(sys_get_temp_dir() . '/phpstan/resultCache-*.php'); + $this->assertNotFalse($resultCacheFiles); + $this->assertTrue(count($resultCacheFiles) > 0); + } + /** * @return mixed[] */ diff --git a/tests/e2e/phpstan_separate_cache.neon b/tests/e2e/phpstan_separate_cache.neon new file mode 100644 index 0000000000..72ea80c29b --- /dev/null +++ b/tests/e2e/phpstan_separate_cache.neon @@ -0,0 +1,7 @@ +# test whether a config, which does not define tmpDir or resultCachePath, will get a separate md5-hashed result cache + +includes: + - baseline.neon + +parameters: + level: 2 From 3e83afc881e9dff0e53a706e6e74e19d3a6372cc Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 18 Jul 2022 20:39:38 +0200 Subject: [PATCH 4/6] use phpstan-version in result-cache hash refs https://github.com/phpstan/phpstan/issues/7601#issuecomment-1187869831 --- src/Analyser/ResultCache/ResultCacheManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index c2ef32b808..8ee30ae8d7 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -616,7 +616,7 @@ private function getCacheFilePath(?string $resultCacheName, ?array $projectConfi (!array_key_exists('tmpDir', $projectConfig['parameters']) || $projectConfig['parameters']['tmpDir'] === null) ) { - return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, $this->bootstrapFiles])) . '.php'; + return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, $this->getPhpstanVersion()])) . '.php'; } return $this->cacheFilePath; From 1f90cc21ed8ebbca1ceff6e1f4ee1bd47e4dc62b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 18 Jul 2022 20:42:16 +0200 Subject: [PATCH 5/6] fixed method case --- src/Analyser/ResultCache/ResultCacheManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index 8ee30ae8d7..f7db40235c 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -616,7 +616,7 @@ private function getCacheFilePath(?string $resultCacheName, ?array $projectConfi (!array_key_exists('tmpDir', $projectConfig['parameters']) || $projectConfig['parameters']['tmpDir'] === null) ) { - return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, $this->getPhpstanVersion()])) . '.php'; + return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, $this->getPhpStanVersion()])) . '.php'; } return $this->cacheFilePath; From e24b20e7fe1e5fba2e6d5c9e2fe83c1f3e82901d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 31 Aug 2022 13:04:16 +0200 Subject: [PATCH 6/6] fix after rebase --- src/Analyser/ResultCache/ResultCacheManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index f7db40235c..5d7d225f6c 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -616,7 +616,7 @@ private function getCacheFilePath(?string $resultCacheName, ?array $projectConfi (!array_key_exists('tmpDir', $projectConfig['parameters']) || $projectConfig['parameters']['tmpDir'] === null) ) { - return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, $this->getPhpStanVersion()])) . '.php'; + return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, ComposerHelper::getPhpStanVersion()])) . '.php'; } return $this->cacheFilePath;