From ce97ef3b3fb170c9d1c9277190d8e989e5314b17 Mon Sep 17 00:00:00 2001 From: Naxvog Date: Tue, 10 Nov 2026 22:56:13 +0100 Subject: [PATCH] Invalidate the transform cache when config or stub files change The cache key was built from the version prefix, the file path and its mtime only. The transformed source also depends on configuration (respect_ignore_tags, inline_vars, eliminate_statically_proven_checks, stubs, ...), so editing typephp.php or a stub file kept serving files transformed under the old settings until `cache:clear` was run. A file cached as ignored stayed unchecked after respect_ignore_tags was turned off for an audit run. Add a fingerprint of the configuration to the cache key, minus the keys that only steer runtime behaviour (on_violation, report_file, ...), plus the effective eliminate_statically_proven_checks value (it has an env override) and the path and mtime of every configured stub file. Stub mtimes follow cache_check_mtime, like source mtimes. --- src/Internal/Io/CacheManager.php | 53 +++++++++++++++++++++++-- src/Internal/Util/StubManager.php | 26 +++++++++++++ tests/Internal/Io/CacheManagerTest.php | 54 ++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 3 deletions(-) diff --git a/src/Internal/Io/CacheManager.php b/src/Internal/Io/CacheManager.php index 7cc7dc9d..fb3d0fe4 100644 --- a/src/Internal/Io/CacheManager.php +++ b/src/Internal/Io/CacheManager.php @@ -6,6 +6,7 @@ use TypePHP\Internal\Util\Config; use TypePHP\Internal\Util\FileFilter; +use TypePHP\Internal\Util\StubManager; /** * @internal @@ -17,6 +18,23 @@ final class CacheManager */ public const VERSION_PREFIX = 'v0.13.0_'; + /** + * Config keys that only steer runtime behaviour and never change the transformed source. + * Every other key is part of the cache key, so new options invalidate the cache by default. + */ + private const RUNTIME_ONLY_CONFIG_KEYS = [ + 'enabled' => true, + 'auto_boot' => true, + 'on_violation' => true, + 'report_file' => true, + 'fail_on_report' => true, + 'fail_on_zero_files' => true, + 'redact_values' => true, + 'cache' => true, + 'cache_dir' => true, + 'cache_check_mtime' => true, + ]; + /** * Cached absolute cache directory path. */ @@ -34,11 +52,17 @@ final class CacheManager */ private static array $cacheKeyMap = []; + /** + * Memoized fingerprint of everything besides the source file that shapes the transformed output. + */ + private static ?string $transformFingerprint = null; + public static function reset(): void { self::$resolvedCacheDir = null; self::$secureDirVerified = null; self::$cacheKeyMap = []; + self::$transformFingerprint = null; } /** @@ -76,7 +100,8 @@ public static function getCacheDir(): string } /** - * Generates a unique cache key for a given file path based on its mtime and version prefix. + * Generates a unique cache key for a given file path based on its mtime, version prefix, + * and the configuration the transformation depends on. */ public static function getCacheKey(string $resolvedPath): string { @@ -88,10 +113,32 @@ public static function getCacheKey(string $resolvedPath): string $mtime = @filemtime($resolvedPath); $mtimeStr = $mtime !== false ? (string) $mtime : '0'; - return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . $resolvedPath . $mtimeStr); + return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . self::getTransformFingerprint() . $resolvedPath . $mtimeStr); + } + + return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . self::getTransformFingerprint() . $resolvedPath); + } + + /** + * Hashes the transform-relevant configuration and stub files, so a cached file is never reused + * after a setting that changes the injected checks (ignore tags, inline vars, stubs, ...) was edited. + */ + private static function getTransformFingerprint(): string + { + if (self::$transformFingerprint !== null) { + return self::$transformFingerprint; } - return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . $resolvedPath); + $config = array_diff_key(Config::get(), self::RUNTIME_ONLY_CONFIG_KEYS); + ksort($config); + + return self::$transformFingerprint = hash( + 'xxh128', + var_export($config, true) + // Env override, so the config array alone does not reflect the effective value. + . (Config::isEliminateStaticallyProvenChecksEnabled() ? '1' : '0') + . (Config::isCacheCheckMtimeEnabled() ? StubManager::getFilesFingerprint() : '') + ); } /** diff --git a/src/Internal/Util/StubManager.php b/src/Internal/Util/StubManager.php index 2b0fab07..33d623b1 100644 --- a/src/Internal/Util/StubManager.php +++ b/src/Internal/Util/StubManager.php @@ -134,6 +134,32 @@ public static function hasFunctionStub(string $functionName): bool return isset(self::$functionStubs[$functionName]); } + /** + * Returns the path and mtime of every configured stub file, without parsing them. + * Stubs are baked into transformed files, so the transform cache keys on this. + */ + public static function getFilesFingerprint(): string + { + $rawStubs = Config::get()['stubs'] ?? []; + $projectRoot = Config::getProjectRoot(); + $fingerprint = ''; + + foreach (\is_array($rawStubs) ? $rawStubs : [] as $pattern) { + if (! \is_string($pattern)) { + continue; + } + + $files = self::resolveStubFiles($pattern, $projectRoot); + sort($files); + + foreach ($files as $file) { + $fingerprint .= $file . ':' . (int) @filemtime($file) . ';'; + } + } + + return $fingerprint; + } + /** * @param array $globs */ diff --git a/tests/Internal/Io/CacheManagerTest.php b/tests/Internal/Io/CacheManagerTest.php index 1583a4a6..7f5d905d 100644 --- a/tests/Internal/Io/CacheManagerTest.php +++ b/tests/Internal/Io/CacheManagerTest.php @@ -57,4 +57,58 @@ Config::reset(); } }); + + test('cache key follows settings that change the transformed source', function () { + try { + $file = __FILE__; + + Config::set(['respect_ignore_tags' => true, 'on_violation' => 'throw']); + $key = CacheManager::getCacheKey($file); + + Config::set(['on_violation' => 'report', 'report_file' => '/tmp/report.json']); + expect(CacheManager::getCacheKey($file))->toBe($key); + + Config::set(['respect_ignore_tags' => false]); + expect(CacheManager::getCacheKey($file))->not()->toBe($key); + + Config::set(['respect_ignore_tags' => true]); + expect(CacheManager::getCacheKey($file))->toBe($key); + + putenv('TYPEPHP_ELIMINATE_STATICALLY_PROVEN_CHECKS=false'); + CacheManager::reset(); + expect(CacheManager::getCacheKey($file))->not()->toBe($key); + } finally { + putenv('TYPEPHP_ELIMINATE_STATICALLY_PROVEN_CHECKS'); + Config::reset(); + } + }); + + test('cache key follows edits to configured stub files', function () { + $stubFile = sys_get_temp_dir() . '/typephp_stub_' . uniqid() . '.stub'; + file_put_contents($stubFile, ' [$stubFile]]); + $key = CacheManager::getCacheKey($file); + + touch($stubFile, 1_700_000_100); + clearstatcache(); + CacheManager::reset(); + expect(CacheManager::getCacheKey($file))->not()->toBe($key); + + Config::set(['cache_check_mtime' => false]); + $keyWithoutMtime = CacheManager::getCacheKey($file); + + touch($stubFile, 1_700_000_200); + clearstatcache(); + CacheManager::reset(); + expect(CacheManager::getCacheKey($file))->toBe($keyWithoutMtime); + } finally { + Config::reset(); + @unlink($stubFile); + } + }); });