diff --git a/src/Metadata/MetadataCache.php b/src/Metadata/MetadataCache.php index 8d992cd..2d7bcd8 100644 --- a/src/Metadata/MetadataCache.php +++ b/src/Metadata/MetadataCache.php @@ -13,7 +13,7 @@ final class MetadataCache { public function __construct( - private readonly ?CacheInterface $cache, + private readonly CacheInterface $cache, private readonly bool $detectChanges, ) {} @@ -25,10 +25,6 @@ public function __construct( */ public function get(string $class, string $name): ?object { - if ($this->cache === null) { - return null; - } - /** @psalm-suppress MixedAssignment */ $metadata = $this->cache->get($this->key($class, $name)); @@ -49,10 +45,6 @@ public function get(string $class, string $name): ?object */ public function setMultiple(iterable $metadata): void { - if ($this->cache === null) { - return; - } - $metadataByKey = []; foreach ($metadata as $item) { diff --git a/src/ReflectionSession.php b/src/ReflectionSession.php index a976014..d77ef13 100644 --- a/src/ReflectionSession.php +++ b/src/ReflectionSession.php @@ -19,6 +19,8 @@ */ final class ReflectionSession implements ClassExistenceChecker, ClassReflector { + private readonly MetadataLazyCollection $metadata; + /** * @var array */ @@ -29,17 +31,15 @@ final class ReflectionSession implements ClassExistenceChecker, ClassReflector */ private array $reflectedClasses = []; - private readonly MetadataLazyCollection $metadata; - /** * @internal * @psalm-internal Typhoon\Reflection */ public function __construct( - private readonly MetadataCache $cache, private readonly PhpParserReflector $phpParserReflector, private readonly NativeReflector $nativeReflector, private readonly ClassLocator $classLocator, + private readonly ?MetadataCache $cache, ) { $this->metadata = new MetadataLazyCollection(); } @@ -70,7 +70,7 @@ public function classExists(string $name): bool return true; } - $metadata = $this->cache->get(ClassMetadata::class, $name); + $metadata = $this->cache?->get(ClassMetadata::class, $name); if ($metadata !== null) { $this->reflectedClasses[$name] = new ClassReflection($this, $metadata); @@ -136,7 +136,7 @@ public function reflectClass(string|object $nameOrObject): ClassReflection return $this->reflectedClasses[$name] = new ClassReflection($this, $metadata); } - $metadata = $this->cache->get(ClassMetadata::class, $name); + $metadata = $this->cache?->get(ClassMetadata::class, $name); if ($metadata !== null) { return $this->reflectedClasses[$name] = new ClassReflection($this, $metadata); @@ -168,7 +168,7 @@ public function reflectClass(string|object $nameOrObject): ClassReflection public function flush(): void { - $this->cache->setMultiple($this->metadata); + $this->cache?->setMultiple($this->metadata); $this->metadata->clear(); $this->reflectedResources = []; $this->reflectedClasses = []; diff --git a/src/TyphoonReflector.php b/src/TyphoonReflector.php index ee93485..e78461e 100644 --- a/src/TyphoonReflector.php +++ b/src/TyphoonReflector.php @@ -25,10 +25,10 @@ final class TyphoonReflector { private function __construct( - private readonly MetadataCache $cache, private readonly PhpParserReflector $phpParserReflector, private readonly NativeReflector $nativeReflector, private readonly ClassLocator $classLocator, + private readonly ?MetadataCache $cache, ) {} /** @@ -42,13 +42,13 @@ public static function build( ?PhpParser $phpParser = null, ): self { return new self( - cache: new MetadataCache($cache, $detectChanges), phpParserReflector: new PhpParserReflector( phpParser: $phpParser ?? (new ParserFactory())->createForNewestSupportedVersion(), phpDocParser: new PhpDocParser($tagPrioritizer), ), nativeReflector: new NativeReflector(), classLocator: new ClassLocatorChain($classLocators ?? self::defaultClassLocators()), + cache: $cache === null ? null : new MetadataCache($cache, $detectChanges), ); }