From 71f1411ee5f7870e731b33bfbd87c0e87c490a6b 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;