Skip to content

Commit

Permalink
TypeRegistry registry constructor must check for duplicate instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 1, 2023
1 parent 611f495 commit 5029656
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/Types/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ final class TypeRegistry
{
/** @var array<string, Type> Map of type names and their corresponding flyweight objects. */
private array $instances;
/** @var array<int, string>|null */
private ?array $instancesReverseIndex = null;
/** @var array<int, string> */
private array $instancesReverseIndex;

/** @param array<string, Type> $instances */
public function __construct(array $instances = [])
{
$this->instances = $instances;
$this->instances = [];
$this->instancesReverseIndex = [];

foreach ($instances as $name => $type) {
$this->register($name, $type);
}
}

/**
Expand Down Expand Up @@ -78,10 +83,8 @@ public function register(string $name, Type $type): void
throw Exception::typeAlreadyRegistered($type);
}

$this->instances[$name] = $type;
if ($this->instancesReverseIndex !== null) {
$this->instancesReverseIndex[spl_object_id($type)] = $name;
}
$this->instances[$name] = $type;
$this->instancesReverseIndex[spl_object_id($type)] = $name;
}

/**
Expand All @@ -99,10 +102,8 @@ public function override(string $name, Type $type): void
throw Exception::typeAlreadyRegistered($type);
}

$this->instances[$name] = $type;
if ($this->instancesReverseIndex !== null) {
$this->instancesReverseIndex[spl_object_id($type)] = $name;
}
$this->instances[$name] = $type;
$this->instancesReverseIndex[spl_object_id($type)] = $name;
}

/**
Expand All @@ -119,15 +120,6 @@ public function getMap(): array

private function findTypeName(Type $type): ?string
{
if ($this->instancesReverseIndex === null) {
$reverseIndex = [];
foreach ($this->instances as $name => $v) {
$reverseIndex[spl_object_id($v)] = $name;
}

$this->instancesReverseIndex = $reverseIndex;
}

$name = $this->instancesReverseIndex[spl_object_id($type)] ?? null;
if ($name !== null && $this->instances[$name] === $type) {
return $name;
Expand Down
10 changes: 10 additions & 0 deletions tests/Types/TypeRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public function testRegisterWithAlreadyRegisteredInstance(): void
$this->registry->register('other', $newType);
}

public function testConstructorWithDuplicateInstance(): void
{
$newType = new TextType();

new TypeRegistry(['a' => $newType]);

$this->expectException(Exception::class);
new TypeRegistry(['a' => $newType, 'b' => $newType]);
}

public function testOverride(): void
{
$baseType = new TextType();
Expand Down

0 comments on commit 5029656

Please sign in to comment.