From 5a032f6f0a7f2ac6eb18bf888c29985b5caadaa3 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 16 Mar 2020 20:02:44 +0100 Subject: [PATCH] Result cache - use lazy callback for errors array in case older PHPStan gets handed newer result cache --- .github/workflows/build.yml | 2 +- phpcs.xml | 1 + .../ResultCache/ResultCacheManager.php | 27 ++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 80f55e5881..672771ad18 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -309,7 +309,7 @@ jobs: uses: actions/cache@v1 with: path: ./tmp - key: "result-cache-v3" + key: "result-cache-v4" - name: "PHPStan with result cache" if: matrix.operating-system == 'ubuntu-latest' diff --git a/phpcs.xml b/phpcs.xml index f70a4cd616..64222d1751 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -8,6 +8,7 @@ + diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index a7ef094a3e..ce409a392e 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -11,7 +11,7 @@ class ResultCacheManager { - private const CACHE_VERSION = 'v3-all-errors'; + private const CACHE_VERSION = 'v4-callback'; /** @var string */ private $cacheFilePath; @@ -98,7 +98,7 @@ public function restore(array $allAnalysedFiles, bool $debug): ResultCache $deletedFiles = array_fill_keys(array_keys($invertedDependencies), true); $filesToAnalyse = []; $invertedDependenciesToReturn = []; - $errors = $data['errors']; + $errors = $data['errorsCallback'](); $filteredErrors = []; $newFileAppeared = false; foreach ($allAnalysedFiles as $analysedFile) { @@ -325,16 +325,25 @@ private function save( $invertedDependencies[$file]['dependentFiles'] = $dependentFiles; } + $template = <<<'php' + %s, + 'meta' => %s, + 'errorsCallback' => static function (): array { return %s; }, + 'dependencies' => %s, +]; +php; + $tmpSuccess = @file_put_contents( $this->cacheFilePath, sprintf( - " $lastFullAnalysisTime, - 'meta' => $this->getMeta(), - 'errors' => $errors, - 'dependencies' => $invertedDependencies, - ], true) + $template, + var_export($lastFullAnalysisTime, true), + var_export($this->getMeta(), true), + var_export($errors, true), + var_export($invertedDependencies, true) ) ); if ($tmpSuccess === false) {