Skip to content
Merged
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
53 changes: 50 additions & 3 deletions src/Internal/Io/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use TypePHP\Internal\Util\Config;
use TypePHP\Internal\Util\FileFilter;
use TypePHP\Internal\Util\StubManager;

/**
* @internal
Expand All @@ -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.
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
{
Expand All @@ -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() : '')
);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Internal/Util/StubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> $globs
*/
Expand Down
54 changes: 54 additions & 0 deletions tests/Internal/Io/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<?php');
touch($stubFile, 1_700_000_000);

try {
$file = __FILE__;

Config::set(['stubs' => [$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);
}
});
});
Loading