22
33namespace 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}
0 commit comments