Skip to content

Commit 087ff34

Browse files
naxvogclaude
andcommitted
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. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 77920b2 commit 087ff34

3 files changed

Lines changed: 130 additions & 3 deletions

File tree

‎src/Internal/Io/CacheManager.php‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TypePHP\Internal\Util\Config;
88
use TypePHP\Internal\Util\FileFilter;
9+
use TypePHP\Internal\Util\StubManager;
910

1011
/**
1112
* @internal
@@ -17,6 +18,23 @@ final class CacheManager
1718
*/
1819
public const VERSION_PREFIX = 'v0.13.0_';
1920

21+
/**
22+
* Config keys that only steer runtime behaviour and never change the transformed source.
23+
* Every other key is part of the cache key, so new options invalidate the cache by default.
24+
*/
25+
private const RUNTIME_ONLY_CONFIG_KEYS = [
26+
'enabled' => true,
27+
'auto_boot' => true,
28+
'on_violation' => true,
29+
'report_file' => true,
30+
'fail_on_report' => true,
31+
'fail_on_zero_files' => true,
32+
'redact_values' => true,
33+
'cache' => true,
34+
'cache_dir' => true,
35+
'cache_check_mtime' => true,
36+
];
37+
2038
/**
2139
* Cached absolute cache directory path.
2240
*/
@@ -34,11 +52,17 @@ final class CacheManager
3452
*/
3553
private static array $cacheKeyMap = [];
3654

55+
/**
56+
* Memoized fingerprint of everything besides the source file that shapes the transformed output.
57+
*/
58+
private static ?string $transformFingerprint = null;
59+
3760
public static function reset(): void
3861
{
3962
self::$resolvedCacheDir = null;
4063
self::$secureDirVerified = null;
4164
self::$cacheKeyMap = [];
65+
self::$transformFingerprint = null;
4266
}
4367

4468
/**
@@ -76,7 +100,8 @@ public static function getCacheDir(): string
76100
}
77101

78102
/**
79-
* Generates a unique cache key for a given file path based on its mtime and version prefix.
103+
* Generates a unique cache key for a given file path based on its mtime, version prefix,
104+
* and the configuration the transformation depends on.
80105
*/
81106
public static function getCacheKey(string $resolvedPath): string
82107
{
@@ -88,10 +113,32 @@ public static function getCacheKey(string $resolvedPath): string
88113
$mtime = @filemtime($resolvedPath);
89114
$mtimeStr = $mtime !== false ? (string) $mtime : '0';
90115

91-
return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . $resolvedPath . $mtimeStr);
116+
return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . self::getTransformFingerprint() . $resolvedPath . $mtimeStr);
117+
}
118+
119+
return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . self::getTransformFingerprint() . $resolvedPath);
120+
}
121+
122+
/**
123+
* Hashes the transform-relevant configuration and stub files, so a cached file is never reused
124+
* after a setting that changes the injected checks (ignore tags, inline vars, stubs, ...) was edited.
125+
*/
126+
private static function getTransformFingerprint(): string
127+
{
128+
if (self::$transformFingerprint !== null) {
129+
return self::$transformFingerprint;
92130
}
93131

94-
return self::$cacheKeyMap[$resolvedPath] = hash('xxh128', self::VERSION_PREFIX . $resolvedPath);
132+
$config = array_diff_key(Config::get(), self::RUNTIME_ONLY_CONFIG_KEYS);
133+
ksort($config);
134+
135+
return self::$transformFingerprint = hash(
136+
'xxh128',
137+
var_export($config, true)
138+
// Env override, so the config array alone does not reflect the effective value.
139+
. (Config::isEliminateStaticallyProvenChecksEnabled() ? '1' : '0')
140+
. (Config::isCacheCheckMtimeEnabled() ? StubManager::getFilesFingerprint() : '')
141+
);
95142
}
96143

97144
/**

‎src/Internal/Util/StubManager.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@ public static function hasFunctionStub(string $functionName): bool
134134
return isset(self::$functionStubs[$functionName]);
135135
}
136136

137+
/**
138+
* Returns the path and mtime of every configured stub file, without parsing them.
139+
* Stubs are baked into transformed files, so the transform cache keys on this.
140+
*/
141+
public static function getFilesFingerprint(): string
142+
{
143+
$rawStubs = Config::get()['stubs'] ?? [];
144+
$projectRoot = Config::getProjectRoot();
145+
$fingerprint = '';
146+
147+
foreach (\is_array($rawStubs) ? $rawStubs : [] as $pattern) {
148+
if (! \is_string($pattern)) {
149+
continue;
150+
}
151+
152+
$files = self::resolveStubFiles($pattern, $projectRoot);
153+
sort($files);
154+
155+
foreach ($files as $file) {
156+
$fingerprint .= $file . ':' . (int) @filemtime($file) . ';';
157+
}
158+
}
159+
160+
return $fingerprint;
161+
}
162+
137163
/**
138164
* @param array<int, string> $globs
139165
*/

‎tests/Internal/Io/CacheManagerTest.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,58 @@
5757
Config::reset();
5858
}
5959
});
60+
61+
test('cache key follows settings that change the transformed source', function () {
62+
try {
63+
$file = __FILE__;
64+
65+
Config::set(['respect_ignore_tags' => true, 'on_violation' => 'throw']);
66+
$key = CacheManager::getCacheKey($file);
67+
68+
Config::set(['on_violation' => 'report', 'report_file' => '/tmp/report.json']);
69+
expect(CacheManager::getCacheKey($file))->toBe($key);
70+
71+
Config::set(['respect_ignore_tags' => false]);
72+
expect(CacheManager::getCacheKey($file))->not()->toBe($key);
73+
74+
Config::set(['respect_ignore_tags' => true]);
75+
expect(CacheManager::getCacheKey($file))->toBe($key);
76+
77+
putenv('TYPEPHP_ELIMINATE_STATICALLY_PROVEN_CHECKS=false');
78+
CacheManager::reset();
79+
expect(CacheManager::getCacheKey($file))->not()->toBe($key);
80+
} finally {
81+
putenv('TYPEPHP_ELIMINATE_STATICALLY_PROVEN_CHECKS');
82+
Config::reset();
83+
}
84+
});
85+
86+
test('cache key follows edits to configured stub files', function () {
87+
$stubFile = sys_get_temp_dir() . '/typephp_stub_' . uniqid() . '.stub';
88+
file_put_contents($stubFile, '<?php');
89+
touch($stubFile, 1_700_000_000);
90+
91+
try {
92+
$file = __FILE__;
93+
94+
Config::set(['stubs' => [$stubFile]]);
95+
$key = CacheManager::getCacheKey($file);
96+
97+
touch($stubFile, 1_700_000_100);
98+
clearstatcache();
99+
CacheManager::reset();
100+
expect(CacheManager::getCacheKey($file))->not()->toBe($key);
101+
102+
Config::set(['cache_check_mtime' => false]);
103+
$keyWithoutMtime = CacheManager::getCacheKey($file);
104+
105+
touch($stubFile, 1_700_000_200);
106+
clearstatcache();
107+
CacheManager::reset();
108+
expect(CacheManager::getCacheKey($file))->toBe($keyWithoutMtime);
109+
} finally {
110+
Config::reset();
111+
@unlink($stubFile);
112+
}
113+
});
60114
});

0 commit comments

Comments
 (0)