Repository navigation
Expand file tree
/
Copy pathChangedFilesDetector.php
More file actions
219 lines (181 loc) · 7.41 KB
/
Copy pathChangedFilesDetector.php
File metadata and controls
219 lines (181 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Rector\Caching\Detector;
use Rector\Caching\Cache;
use Rector\Caching\Config\FileHashComputer;
use Rector\Caching\Enum\CacheKey;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Util\FileHasher;
/**
* Inspired by https://github.com/symplify/symplify/pull/90/files#diff-72041b2e1029a08930e13d79d298ef11
*
* @see \Rector\Tests\Caching\Detector\ChangedFilesDetectorTest
*/
final class ChangedFilesDetector
{
/**
* @var array<string, true>
*/
private array $cacheableFiles = [];
// scopes the per-file cache key to the active --only / --only-suffix selection (empty = full run)
private string $scopeSuffix = '';
public function __construct(
private readonly FileHashComputer $fileHashComputer,
private readonly Cache $cache,
private readonly FileHasher $fileHasher
) {
}
public function setActiveScope(?string $onlyRule, ?string $onlySuffix): void
{
// each selection gets its own cache key, so --only and full runs coexist without clearing or poisoning
$this->scopeSuffix = ($onlyRule === null && $onlySuffix === null)
? ''
: '|only:' . ($onlyRule ?? '') . '|suffix:' . ($onlySuffix ?? '');
}
public function cacheFile(string $filePath): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
if (! isset($this->cacheableFiles[$filePathCacheKey])) {
return;
}
$hash = $this->hashFile($filePath);
$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $hash);
}
public function addCacheableFile(string $filePath): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$this->cacheableFiles[$filePathCacheKey] = true;
}
public function hasFileChanged(string $filePath): bool
{
$cachedValue = $this->cache->load($this->getFilePathCacheKey($filePath), CacheKey::FILE_HASH_KEY);
// a scoped (--only) run reuses the full-run cache: a file left clean by all rules stays
// clean under a single rule too, and the content is still compared below
if ($cachedValue === null && $this->scopeSuffix !== '') {
$unscopedCacheKey = $this->fileHasher->hash($this->resolvePath($filePath));
$cachedValue = $this->cache->load($unscopedCacheKey, CacheKey::FILE_HASH_KEY);
}
if ($cachedValue !== null) {
$currentFileHash = $this->hashFile($filePath);
return $currentFileHash !== $cachedValue;
}
// we don't have a value to compare against. Be defensive and assume its changed
return true;
}
public function invalidateFile(string $filePath): void
{
$fileInfoCacheKey = $this->getFilePathCacheKey($filePath);
$this->cache->clean($fileInfoCacheKey);
unset($this->cacheableFiles[$fileInfoCacheKey]);
}
public function clear(): void
{
$this->cache->clear();
}
/**
* @api
*/
public function setFirstResolvedConfigFileInfo(string $filePath): void
{
// the first config is core to all → if it was changed, just invalidate it
$configurationSnapshot = $this->createConfigurationSnapshot($filePath);
$this->storeConfigurationDataHash($filePath, $configurationSnapshot);
}
private function resolvePath(string $filePath): string
{
$realPath = realpath($filePath);
if ($realPath === false) {
return $filePath;
}
return $realPath;
}
private function getFilePathCacheKey(string $filePath): string
{
return $this->fileHasher->hash($this->resolvePath($filePath) . $this->scopeSuffix);
}
private function hashFile(string $filePath): string
{
return $this->fileHasher->hashFiles([$this->resolvePath($filePath)]);
}
/**
* @return array{hash: string, rules: string[], sets: string[], skip: string[]}
*/
private function createConfigurationSnapshot(string $filePath): array
{
$directionalParameters = SimpleParameterProvider::provideCacheDirectionalParameters();
return [
'hash' => $this->fileHashComputer->compute($filePath),
'rules' => $this->hashEach($directionalParameters['rules']),
'sets' => $this->hashEach($directionalParameters['sets']),
'skip' => $this->hashEach($directionalParameters['skip']),
];
}
/**
* @param array{hash: string, rules: string[], sets: string[], skip: string[]} $configurationSnapshot
*/
private function storeConfigurationDataHash(string $filePath, array $configurationSnapshot): void
{
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . $this->getFilePathCacheKey($filePath);
$this->invalidateCacheIfConfigurationChanged($key, $configurationSnapshot);
$this->cache->save($key, CacheKey::CONFIGURATION_HASH_KEY, $configurationSnapshot);
}
/**
* @param array{hash: string, rules: string[], sets: string[], skip: string[]} $configurationSnapshot
*/
private function invalidateCacheIfConfigurationChanged(string $key, array $configurationSnapshot): void
{
$oldCachedValue = $this->cache->load($key, CacheKey::CONFIGURATION_HASH_KEY);
// first run, nothing to compare against
if ($oldCachedValue === null) {
return;
}
// legacy string format from an older Rector version → be safe and reset once
if (! is_array($oldCachedValue)) {
$this->clear();
return;
}
if ($this->shouldInvalidateCache($oldCachedValue, $configurationSnapshot)) {
// should be unique per getcwd()
$this->clear();
}
}
/**
* @param array<string, mixed> $oldSnapshot
* @param array{hash: string, rules: string[], sets: string[], skip: string[]} $newSnapshot
*/
private function shouldInvalidateCache(array $oldSnapshot, array $newSnapshot): bool
{
// an output-affecting parameter changed (php version, import names, indent, configured rule value, ...)
if (($oldSnapshot['hash'] ?? null) !== $newSnapshot['hash']) {
return true;
}
// a rule or set was added → files clean so far may now be refactored
if ($this->hasAddedEntry($oldSnapshot['rules'] ?? [], $newSnapshot['rules'])) {
return true;
}
if ($this->hasAddedEntry($oldSnapshot['sets'] ?? [], $newSnapshot['sets'])) {
return true;
}
// a skip was removed → previously skipped transformations may now apply
return $this->hasAddedEntry($newSnapshot['skip'], $oldSnapshot['skip'] ?? []);
}
/**
* Is there any entry present in $comparedEntries but missing from $baseEntries?
* A non-array on either side means an unexpected shape, treated as changed to stay safe.
*/
private function hasAddedEntry(mixed $baseEntries, mixed $comparedEntries): bool
{
if (! is_array($baseEntries) || ! is_array($comparedEntries)) {
return true;
}
return array_any($comparedEntries, fn ($comparedEntry): bool => ! in_array($comparedEntry, $baseEntries, true));
}
/**
* @param mixed[] $values
* @return string[]
*/
private function hashEach(array $values): array
{
return array_map(static fn (mixed $value): string => sha1(serialize($value)), array_values($values));
}
}