Skip to content

Commit 1f1c3c0

Browse files
Slamdunksebastianbergmann
authored andcommitted
Static analysis cache: rely on content hash
1 parent 2a8a602 commit 1f1c3c0

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/StaticAnalysis/CachingFileAnalyser.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
final class CachingFileAnalyser implements FileAnalyser
1818
{
19-
private const CACHE_FORMAT_VERSION = 2;
19+
private const CACHE_FORMAT_VERSION = 3;
2020

2121
/**
2222
* @var FileAnalyser
@@ -41,6 +41,15 @@ public function __construct(string $directory, FileAnalyser $analyser)
4141
$this->directory = $directory;
4242
}
4343

44+
public function hash(string $filename): int
45+
{
46+
if (!isset($this->cache[$filename])) {
47+
$this->process($filename);
48+
}
49+
50+
return $this->cache[$filename]['hash'];
51+
}
52+
4453
public function classesIn(string $filename): array
4554
{
4655
if (!isset($this->cache[$filename])) {
@@ -100,13 +109,18 @@ public function ignoredLinesFor(string $filename): array
100109

101110
public function process(string $filename): void
102111
{
103-
if ($this->has($filename)) {
104-
$this->cache[$filename] = $this->read($filename);
112+
if (false !== ($cache = $this->read($filename))) {
113+
$hash = ParsingFileAnalyser::computeHashForSource(file_get_contents($filename));
114+
115+
if ($hash === $cache['hash']) {
116+
$this->cache[$filename] = $cache;
105117

106-
return;
118+
return;
119+
}
107120
}
108121

109122
$this->cache[$filename] = [
123+
'hash' => $this->analyser->hash($filename),
110124
'classesIn' => $this->analyser->classesIn($filename),
111125
'traitsIn' => $this->analyser->traitsIn($filename),
112126
'functionsIn' => $this->analyser->functionsIn($filename),
@@ -118,29 +132,20 @@ public function process(string $filename): void
118132
$this->write($filename, $this->cache[$filename]);
119133
}
120134

121-
private function has(string $filename): bool
135+
/**
136+
* @return mixed
137+
*/
138+
private function read(string $filename)
122139
{
123140
$cacheFile = $this->cacheFile($filename);
124141

125142
if (!is_file($cacheFile)) {
126143
return false;
127144
}
128145

129-
if (filemtime($cacheFile) < filemtime($filename)) {
130-
return false;
131-
}
132-
133-
return true;
134-
}
135-
136-
/**
137-
* @return mixed
138-
*/
139-
private function read(string $filename)
140-
{
141146
return unserialize(
142147
file_get_contents(
143-
$this->cacheFile($filename)
148+
$cacheFile
144149
),
145150
['allowed_classes' => false]
146151
);

src/StaticAnalysis/FileAnalyser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
interface FileAnalyser
1616
{
17+
public function hash(string $filename): int;
18+
1719
public function classesIn(string $filename): array;
1820

1921
public function traitsIn(string $filename): array;

src/StaticAnalysis/ParsingFileAnalyser.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
*/
3232
final class ParsingFileAnalyser implements FileAnalyser
3333
{
34+
/**
35+
* @var array
36+
*/
37+
private $hash = [];
38+
3439
/**
3540
* @var array
3641
*/
@@ -71,12 +76,24 @@ final class ParsingFileAnalyser implements FileAnalyser
7176
*/
7277
private $ignoreDeprecatedCode;
7378

79+
public static function computeHashForSource(string $source): int
80+
{
81+
return crc32($source);
82+
}
83+
7484
public function __construct(bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode)
7585
{
7686
$this->useAnnotationsForIgnoringCode = $useAnnotationsForIgnoringCode;
7787
$this->ignoreDeprecatedCode = $ignoreDeprecatedCode;
7888
}
7989

90+
public function hash(string $filename): int
91+
{
92+
$this->analyse($filename);
93+
94+
return $this->hash[$filename];
95+
}
96+
8097
public function classesIn(string $filename): array
8198
{
8299
$this->analyse($filename);
@@ -177,6 +194,7 @@ private function analyse(string $filename): void
177194
}
178195
// @codeCoverageIgnoreEnd
179196

197+
$this->hash[$filename] = self::computeHashForSource($source);
180198
$this->classes[$filename] = $codeUnitFindingVisitor->classes();
181199
$this->traits[$filename] = $codeUnitFindingVisitor->traits();
182200
$this->functions[$filename] = $codeUnitFindingVisitor->functions();

0 commit comments

Comments
 (0)