99 */
1010namespace SebastianBergmann \CodeCoverage \StaticAnalysis ;
1111
12+ use function assert ;
1213use function crc32 ;
1314use function file_get_contents ;
1415use function file_put_contents ;
1516use function is_file ;
1617use function serialize ;
18+ use GlobIterator ;
1719use SebastianBergmann \CodeCoverage \Util \Filesystem ;
20+ use SplFileInfo ;
1821
1922/**
2023 * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
2124 */
2225final class CachingFileAnalyser implements FileAnalyser
2326{
24- private const CACHE_FORMAT_VERSION = 2 ;
27+ /**
28+ * @var ?string
29+ */
30+ private static $ cacheVersion ;
2531
2632 /**
2733 * @var FileAnalyser
@@ -44,6 +50,10 @@ public function __construct(string $directory, FileAnalyser $analyser)
4450
4551 $ this ->analyser = $ analyser ;
4652 $ this ->directory = $ directory ;
53+
54+ if (self ::$ cacheVersion === null ) {
55+ $ this ->calculateCacheVersion ();
56+ }
4757 }
4858
4959 public function classesIn (string $ filename ): array
@@ -155,6 +165,19 @@ private function write(string $filename, $data): void
155165
156166 private function cacheFile (string $ filename ): string
157167 {
158- return $ this ->directory . DIRECTORY_SEPARATOR . hash ('sha256 ' , $ filename . crc32 (file_get_contents ($ filename )) . self ::CACHE_FORMAT_VERSION );
168+ return $ this ->directory . DIRECTORY_SEPARATOR . hash ('sha256 ' , $ filename . crc32 (file_get_contents ($ filename )) . self ::$ cacheVersion );
169+ }
170+
171+ private function calculateCacheVersion (): void
172+ {
173+ $ buffer = '' ;
174+
175+ foreach (new GlobIterator (__DIR__ . '/*.php ' ) as $ file ) {
176+ assert ($ file instanceof SplFileInfo);
177+
178+ $ buffer .= file_get_contents ($ file ->getPathname ());
179+ }
180+
181+ self ::$ cacheVersion = (string ) crc32 ($ buffer );
159182 }
160183}
0 commit comments