Skip to content

Commit

Permalink
Use Version::id() instead of "MD5 over source code" for encoding stat…
Browse files Browse the repository at this point in the history
…ic analysis cache format in cache keys for static analysis cache

This means that cached static analysis results will be treated as
outdated when a new/different version of php-code-coverage is used even
if the static analysis code has not changed. Optimizing this edge case
for performance is not worth risking errors in the code for calculating
the "MD5 over source code" that we previously used for this.
  • Loading branch information
sebastianbergmann committed Feb 8, 2025
1 parent bac9f42 commit 89e6947
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-10.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [10.1.17] - 2025-MM-DD

### Changed

* Changed version identifier for static analysis cache from "MD5 over source code" to `Version::id()`

## [10.1.16] - 2024-08-22

### Changed
Expand Down Expand Up @@ -113,6 +119,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt

* The `SebastianBergmann\CodeCoverage\Filter::includeDirectory()`, `SebastianBergmann\CodeCoverage\Filter::excludeDirectory()`, and `SebastianBergmann\CodeCoverage\Filter::excludeFile()` methods are now deprecated

[10.1.17]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.16...10.1
[10.1.16]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.15...10.1.16
[10.1.15]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.14...10.1.15
[10.1.14]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.13...10.1.14
Expand Down
23 changes: 2 additions & 21 deletions src/StaticAnalysis/CachingFileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use function serialize;
use function unserialize;
use SebastianBergmann\CodeCoverage\Util\Filesystem;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
use SebastianBergmann\CodeCoverage\Version;

/**
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
Expand All @@ -26,7 +26,6 @@
*/
final class CachingFileAnalyser implements FileAnalyser
{
private static ?string $cacheVersion = null;
private readonly string $directory;
private readonly FileAnalyser $analyser;
private readonly bool $useAnnotationsForIgnoringCode;
Expand Down Expand Up @@ -152,7 +151,7 @@ private function cacheFile(string $filename): string
[
$filename,
file_get_contents($filename),
self::cacheVersion(),
Version::id(),
$this->useAnnotationsForIgnoringCode,
$this->ignoreDeprecatedCode,
],
Expand All @@ -161,22 +160,4 @@ private function cacheFile(string $filename): string

return $this->directory . DIRECTORY_SEPARATOR . $cacheKey;
}

private static function cacheVersion(): string
{
if (self::$cacheVersion !== null) {
return self::$cacheVersion;
}

$buffer = [];

foreach ((new FileIteratorFacade)->getFilesAsArray(__DIR__, '.php') as $file) {
$buffer[] = $file;
$buffer[] = file_get_contents($file);
}

self::$cacheVersion = md5(implode("\0", $buffer));

return self::$cacheVersion;
}
}

0 comments on commit 89e6947

Please sign in to comment.