From a5e6e82bc075f821cd00322972aebbbace41c1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 1 Jul 2023 17:26:14 +0200 Subject: [PATCH] TypeRegistry registry constructor must check for duplicate instances --- src/Types/TypeRegistry.php | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Types/TypeRegistry.php b/src/Types/TypeRegistry.php index 2d575b61ae9..d67216b1323 100644 --- a/src/Types/TypeRegistry.php +++ b/src/Types/TypeRegistry.php @@ -16,13 +16,14 @@ final class TypeRegistry { /** @var array Map of type names and their corresponding flyweight objects. */ private array $instances; - /** @var array|null */ - private ?array $instancesReverseIndex = null; + /** @var array */ + private array $instancesReverseIndex; /** @param array $instances */ public function __construct(array $instances = []) { - $this->instances = []; + $this->instances = []; + $this->instancesReverseIndex = []; foreach ($instances as $name => $type) { $this->register($name, $type); } @@ -81,10 +82,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; } /** @@ -102,10 +101,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; } /** @@ -122,15 +119,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;