-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathKaizenRulesDetector.php
More file actions
48 lines (38 loc) · 1.18 KB
/
KaizenRulesDetector.php
File metadata and controls
48 lines (38 loc) · 1.18 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
<?php
declare(strict_types=1);
namespace Rector\Caching\Detector;
use Rector\Caching\Cache;
use Rector\Caching\Enum\CacheKey;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Util\FileHasher;
final readonly class KaizenRulesDetector
{
public function __construct(
private Cache $cache,
private FileHasher $fileHasher
) {
}
public function addRule(string $rectorClass): void
{
$cachedValue = $this->loadRules();
$appliedRectorClasses = array_unique(array_merge($cachedValue, [$rectorClass]));
$this->cache->save($this->getCacheKey(), CacheKey::KAIZEN_RULES, $appliedRectorClasses);
}
/**
* @return array<class-string<RectorInterface>>
*/
public function loadRules(): array
{
$key = $this->getCacheKey();
$rules = $this->cache->load($key, CacheKey::KAIZEN_RULES) ?? [];
if (! is_array($rules)) {
throw new ShouldNotHappenException();
}
return array_unique($rules);
}
private function getCacheKey(): string
{
return CacheKey::KAIZEN_RULES . '_' . $this->fileHasher->hash(getcwd());
}
}