Skip to content

Commit a709c2a

Browse files
committed
Refactored "isFresh" logic for "CacheState" class
1 parent 890a20e commit a709c2a

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/Service/Cache/CacheState.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Okapi\CodeTransformer\Service\Cache;
44

5+
use Okapi\CodeTransformer\Service\TransformerContainer;
6+
57
/**
68
* # Cache State
79
*
@@ -13,12 +15,14 @@ class CacheState
1315
* CacheState constructor.
1416
*
1517
* @param string $originalFilePath
18+
* @param string $className
1619
* @param string|null $cachedFilePath
1720
* @param int $transformedTime
1821
* @param string[] $transformerFilePaths
1922
*/
2023
public function __construct(
2124
public string $originalFilePath,
25+
public string $className,
2226
public ?string $cachedFilePath,
2327
public int $transformedTime,
2428
public array $transformerFilePaths,
@@ -32,6 +36,7 @@ public function __construct(
3236
public function toArray(): array
3337
{
3438
return [
39+
'className' => $this->className,
3540
'cachedFilePath' => $this->cachedFilePath,
3641
'transformedTime' => $this->transformedTime,
3742
'transformerFilePaths' => $this->transformerFilePaths,
@@ -45,12 +50,14 @@ public function toArray(): array
4550
*/
4651
public function isFresh(): bool
4752
{
48-
// Prevent infinite recursion
49-
if ($this->originalFilePath === $this->cachedFilePath) {
50-
// @codeCoverageIgnoreStart
51-
// This should only happen if the project is misconfigured
52-
return false;
53-
// @codeCoverageIgnoreEnd
53+
if ($this->cachedFilePath !== null) {
54+
// Prevent infinite recursion
55+
if ($this->originalFilePath === $this->cachedFilePath) {
56+
// @codeCoverageIgnoreStart
57+
// This should only happen if the project is misconfigured
58+
return false;
59+
// @codeCoverageIgnoreEnd
60+
}
5461
}
5562

5663
$allFiles = array_merge(
@@ -64,13 +71,25 @@ public function isFresh(): bool
6471
return false;
6572
}
6673

74+
if ($this->cachedFilePath !== null) {
75+
$allFiles[] = $this->cachedFilePath;
76+
}
77+
6778
// Check if the cache file exists
68-
foreach (array_merge($allFiles, [$this->cachedFilePath]) as $file) {
79+
foreach ($allFiles as $file) {
6980
if (!file_exists($file)) {
7081
return false;
7182
}
7283
}
7384

85+
// Check if the transformer count is the same
86+
// Checking the count alone should be enough
87+
$cachedTransformerCount = count($this->transformerFilePaths);
88+
$currentTransformerCount = count(TransformerContainer::matchTransformers($this->className));
89+
if ($cachedTransformerCount !== $currentTransformerCount) {
90+
return false;
91+
}
92+
7493
return true;
7594
}
7695
}

src/Service/CacheStateManager.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,27 @@ private function loadCacheState(): void
105105
// Unserialize
106106
$cacheState = eval($cacheStateContent);
107107

108+
// Filter out invalid items
109+
$cacheState = array_filter(
110+
$cacheState,
111+
function ($cacheStateItem) {
112+
return key_exists('className', $cacheStateItem)
113+
&& key_exists('cachedFilePath', $cacheStateItem)
114+
&& key_exists('transformedTime', $cacheStateItem)
115+
&& key_exists('transformerFilePaths', $cacheStateItem);
116+
},
117+
);
118+
108119
// Convert to array of CacheState objects
109120
array_walk(
110121
$cacheState,
111122
function (&$cacheStateItem, $originalFilePath) {
112123
$cacheStateItem = new CacheState(
113-
$originalFilePath,
114-
$cacheStateItem['cachedFilePath'],
115-
$cacheStateItem['transformedTime'],
116-
$cacheStateItem['transformerFilePaths']
124+
originalFilePath: $originalFilePath,
125+
className: $cacheStateItem['className'],
126+
cachedFilePath: $cacheStateItem['cachedFilePath'],
127+
transformedTime: $cacheStateItem['transformedTime'],
128+
transformerFilePaths: $cacheStateItem['transformerFilePaths']
117129
);
118130
},
119131
);

src/Service/TransformerContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public static function transform(Metadata $metadata)
169169
$transformerFilePaths = $instance->getTransformerFilePaths($transformers);
170170
$cacheState = new CacheState(
171171
originalFilePath: $originalFilePath,
172+
className: $fullClassName,
172173
cachedFilePath: $transformed ? $cacheFilePath : null,
173174
transformedTime: $fileModificationTime,
174175
transformerFilePaths: $transformerFilePaths,

0 commit comments

Comments
 (0)