Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Analyser/ResultCache/ResultCacheClearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Symfony\Component\Finder\Finder;
use function dirname;
use function is_file;
use function unlink;

class ResultCacheClearer
Expand All @@ -17,11 +16,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;
}
Expand Down
37 changes: 28 additions & 9 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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(
Expand All @@ -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';
}
Comment on lines +609 to +611
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this condition is covered by #1475


if ($projectConfig !== null &&
array_key_exists('parameters', $projectConfig) &&
!array_key_exists('resultCachePath', $projectConfig['parameters']) &&
(!array_key_exists('tmpDir', $projectConfig['parameters']) || $projectConfig['parameters']['tmpDir'] === null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tmpDir case is already covered by an existing test which uses a tmpDir

) {

return dirname($this->cacheFilePath) . '/resultCache-' . md5(Neon::encode([$this->analysedPaths, ComposerHelper::getPhpStanVersion()])) . '.php';
}

return $this->cacheFilePath;
}

/**
* @param mixed[]|null $projectConfig
* @param array<string, mixed> $dependencies
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/ResultCacheEndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Copy link
Contributor Author

@staabm staabm Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since checking exactly which hashes are generated is a bit too much IMO, I just verified phpstan will create result-cache files including some suffix (aka. the hash), which it did not do before this change.

$this->assertNotFalse($resultCacheFiles);
$this->assertTrue(count($resultCacheFiles) > 0);
}

/**
* @return mixed[]
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/phpstan_separate_cache.neon
Original file line number Diff line number Diff line change
@@ -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