diff --git a/src/Types/TypeRegistry.php b/src/Types/TypeRegistry.php index 15fddeb20be..ee9aba6386c 100644 --- a/src/Types/TypeRegistry.php +++ b/src/Types/TypeRegistry.php @@ -6,7 +6,7 @@ use Doctrine\DBAL\Exception; -use function array_search; +use function spl_object_id; /** * The type registry is responsible for holding a map of all known DBAL types. @@ -16,6 +16,8 @@ final class TypeRegistry { /** @var array Map of type names and their corresponding flyweight objects. */ private array $instances; + /** @var array|null */ + private ?array $instancesReverseIndex = null; /** @param array $instances */ public function __construct(array $instances = []) @@ -77,6 +79,9 @@ public function register(string $name, Type $type): void } $this->instances[$name] = $type; + if ($this->instancesReverseIndex !== null) { + $this->instancesReverseIndex[spl_object_id($type)] = $name; + } } /** @@ -95,6 +100,9 @@ public function override(string $name, Type $type): void } $this->instances[$name] = $type; + if ($this->instancesReverseIndex !== null) { + $this->instancesReverseIndex[spl_object_id($type)] = $name; + } } /** @@ -111,12 +119,20 @@ public function getMap(): array private function findTypeName(Type $type): ?string { - $name = array_search($type, $this->instances, true); + if ($this->instancesReverseIndex === null) { + $reverseIndex = []; + foreach ($this->instances as $name => $v) { + $reverseIndex[spl_object_id($v)] = $name; + } - if ($name === false) { - return null; + $this->instancesReverseIndex = $reverseIndex; } - return $name; + $name = $this->instancesReverseIndex[spl_object_id($type)] ?? null; + if ($name !== null && $this->instances[$name] === $type) { + return $name; + } + + return null; } }