Skip to content

Commit

Permalink
Made MetadataCache nullable, not PSR cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 9, 2024
1 parent 5d69410 commit 33ecb33
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
10 changes: 1 addition & 9 deletions src/Metadata/MetadataCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class MetadataCache
{
public function __construct(
private readonly ?CacheInterface $cache,
private readonly CacheInterface $cache,
private readonly bool $detectChanges,
) {}

Expand All @@ -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));

Expand All @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions src/ReflectionSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
final class ReflectionSession implements ClassExistenceChecker, ClassReflector
{
private readonly MetadataLazyCollection $metadata;

/**
* @var array<non-empty-string, true>
*/
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];
Expand Down
4 changes: 2 additions & 2 deletions src/TyphoonReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}

/**
Expand All @@ -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),
);
}

Expand Down

0 comments on commit 33ecb33

Please sign in to comment.