From d2f9cb601cdecfeef5859aca871fff22f454d297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Tue, 7 Feb 2023 16:14:31 +0100 Subject: [PATCH] Use spl_object_id instead of spl_object_hash --- src/Collection/HasManyCollection.php | 4 +-- src/Mapper/Dbal/DbalMapper.php | 2 +- src/Relationships/HasMany.php | 36 +++++++++---------- src/Relationships/IRelationshipCollection.php | 2 +- src/Repository/PersistenceHelper.php | 18 +++++----- src/Repository/RemovalHelper.php | 14 ++++---- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Collection/HasManyCollection.php b/src/Collection/HasManyCollection.php index 7a99b585..a930b09d 100644 --- a/src/Collection/HasManyCollection.php +++ b/src/Collection/HasManyCollection.php @@ -15,7 +15,7 @@ use function get_class; use function iterator_count; use function iterator_to_array; -use function spl_object_hash; +use function spl_object_id; /** @@ -193,7 +193,7 @@ public function getIterator(): Iterator $all = []; foreach ($storageCollection as $entity) { - $all[spl_object_hash($entity)] = $entity; + $all[spl_object_id($entity)] = $entity; } foreach ($toAdd as $hash => $entity) { $all[$hash] = $entity; diff --git a/src/Mapper/Dbal/DbalMapper.php b/src/Mapper/Dbal/DbalMapper.php index ded0b49d..808b7285 100644 --- a/src/Mapper/Dbal/DbalMapper.php +++ b/src/Mapper/Dbal/DbalMapper.php @@ -284,7 +284,7 @@ protected function getRelationshipMapper( ?IMapper $sourceMapper = null ): IRelationshipMapper { - $key = $type . spl_object_hash($metadata) . $metadata->name; + $key = $type . spl_object_id($metadata) . $metadata->name; if (!isset($this->cacheRM[$key])) { $this->cacheRM[$key] = $this->createRelationshipMapper($type, $metadata, $sourceMapper); } diff --git a/src/Relationships/HasMany.php b/src/Relationships/HasMany.php index d137d7dd..ea5a4852 100644 --- a/src/Relationships/HasMany.php +++ b/src/Relationships/HasMany.php @@ -18,7 +18,7 @@ use function assert; use function is_array; use function iterator_count; -use function spl_object_hash; +use function spl_object_id; /** @@ -50,19 +50,19 @@ abstract class HasMany implements IRelationshipCollection /** * @var IEntity[] - * @phpstan-var array + * @phpstan-var array */ protected $toAdd = []; /** * @var IEntity[] - * @phpstan-var array + * @phpstan-var array */ protected $toRemove = []; /** * @var IEntity[] - * @phpstan-var array + * @phpstan-var array */ protected $tracked = []; @@ -176,13 +176,13 @@ public function add($entity): ?IEntity return null; } - $entityHash = spl_object_hash($entity); + $entityId = spl_object_id($entity); - if (isset($this->toRemove[$entityHash])) { - unset($this->toRemove[$entityHash]); - $this->tracked[$entityHash] = $entity; + if (isset($this->toRemove[$entityId])) { + unset($this->toRemove[$entityId]); + $this->tracked[$entityId] = $entity; } else { - $this->toAdd[$entityHash] = $entity; + $this->toAdd[$entityId] = $entity; } $this->updateRelationshipAdd($entity); @@ -203,13 +203,13 @@ public function remove($entity): ?IEntity return null; } - $entityHash = spl_object_hash($entity); + $entityId = spl_object_id($entity); - if (isset($this->toAdd[$entityHash])) { - unset($this->toAdd[$entityHash]); + if (isset($this->toAdd[$entityId])) { + unset($this->toAdd[$entityId]); } else { - $this->toRemove[$entityHash] = $entity; - unset($this->tracked[$entityHash]); + $this->toRemove[$entityId] = $entity; + unset($this->tracked[$entityId]); } $this->updateRelationshipRemove($entity); @@ -226,7 +226,7 @@ public function has($entity): bool return false; } - $entityHash = spl_object_hash($entity); + $entityHash = spl_object_id($entity); if (isset($this->toAdd[$entityHash])) { return true; @@ -248,12 +248,12 @@ public function set(array $data): bool foreach ($data as $entry) { $entity = $this->createEntity($entry); if ($entity === null) continue; - $wanted[spl_object_hash($entity)] = $entity; + $wanted[spl_object_id($entity)] = $entity; } $current = []; foreach ($this->getCollection() as $entity) { - $current[spl_object_hash($entity)] = $entity; + $current[spl_object_id($entity)] = $entity; } $toRemove = array_diff_key($current, $wanted); @@ -327,7 +327,7 @@ public function isModified(): bool */ public function trackEntity(IEntity $entity): void { - $this->tracked[spl_object_hash($entity)] = $entity; + $this->tracked[spl_object_id($entity)] = $entity; } diff --git a/src/Relationships/IRelationshipCollection.php b/src/Relationships/IRelationshipCollection.php index 2596bf2f..e29a6415 100644 --- a/src/Relationships/IRelationshipCollection.php +++ b/src/Relationships/IRelationshipCollection.php @@ -88,7 +88,7 @@ public function trackEntity(IEntity $entity): void; /** * Returns IEntity for persistence. * @return IEntity[] - * @phpstan-return array + * @phpstan-return array * @ignore * @internal */ diff --git a/src/Repository/PersistenceHelper.php b/src/Repository/PersistenceHelper.php index a91e25a7..7b2c7d2d 100644 --- a/src/Repository/PersistenceHelper.php +++ b/src/Repository/PersistenceHelper.php @@ -17,13 +17,13 @@ class PersistenceHelper /** @var array|IRelationshipContainer> */ protected static $inputQueue = []; - /** @var array|IRelationshipContainer|true> */ + /** @var array|IRelationshipContainer|true> */ protected static $outputQueue = []; /** * @see https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search - * @return array|IRelationshipContainer|true> + * @return array|IRelationshipContainer|true> */ public static function getCascadeQueue(IEntity $entity, IModel $model, bool $withCascade): array { @@ -45,9 +45,9 @@ public static function getCascadeQueue(IEntity $entity, IModel $model, bool $wit protected static function visitEntity(IEntity $entity, IModel $model, bool $withCascade = true): void { - $entityHash = spl_object_hash($entity); - if (isset(self::$outputQueue[$entityHash])) { - if (self::$outputQueue[$entityHash] === true) { + $entityId = spl_object_id($entity); + if (isset(self::$outputQueue[$entityId])) { + if (self::$outputQueue[$entityId] === true) { $cycle = []; $bt = debug_backtrace(); foreach ($bt as $item) { @@ -68,16 +68,16 @@ protected static function visitEntity(IEntity $entity, IModel $model, bool $with $repository->onBeforePersist($entity); if ($withCascade) { - self::$outputQueue[$entityHash] = true; + self::$outputQueue[$entityId] = true; foreach ($entity->getMetadata()->getProperties() as $propertyMeta) { if ($propertyMeta->relationship !== null && $propertyMeta->relationship->cascade['persist']) { self::addRelationshipToQueue($entity, $propertyMeta, $model); } } - unset(self::$outputQueue[$entityHash]); // reenqueue + unset(self::$outputQueue[$entityId]); // reenqueue } - self::$outputQueue[$entityHash] = $entity; + self::$outputQueue[$entityId] = $entity; } @@ -90,7 +90,7 @@ protected static function visitRelationship($rel, IModel $model): void self::visitEntity($entity, $model); } - self::$outputQueue[spl_object_hash($rel)] = $rel; + self::$outputQueue[spl_object_id($rel)] = $rel; } diff --git a/src/Repository/RemovalHelper.php b/src/Repository/RemovalHelper.php index 3ff8793b..a1be63dd 100644 --- a/src/Repository/RemovalHelper.php +++ b/src/Repository/RemovalHelper.php @@ -20,7 +20,7 @@ class RemovalHelper { /** * @param array> $queuePersist - * @param array $queueRemove + * @param array $queueRemove */ public static function getCascadeQueueAndSetNulls( IEntity $entity, @@ -30,7 +30,7 @@ public static function getCascadeQueueAndSetNulls( array &$queueRemove ): void { - $entityHash = spl_object_hash($entity); + $entityHash = spl_object_id($entity); if (isset($queueRemove[$entityHash])) { return; } @@ -49,7 +49,7 @@ public static function getCascadeQueueAndSetNulls( } foreach ($prePersist as $value) { - $queuePersist[spl_object_hash($value)] = $value; + $queuePersist[spl_object_id($value)] = $value; } $queueRemove[$entityHash] = true; foreach ($pre as $value) { @@ -59,7 +59,7 @@ public static function getCascadeQueueAndSetNulls( foreach ($value->getIterator() as $subValue) { static::getCascadeQueueAndSetNulls($subValue, $model, true, $queuePersist, $queueRemove); } - $queuePersist[spl_object_hash($value)] = $value; + $queuePersist[spl_object_id($value)] = $value; } } unset($queueRemove[$entityHash]); @@ -72,7 +72,7 @@ public static function getCascadeQueueAndSetNulls( foreach ($value->getIterator() as $subValue) { static::getCascadeQueueAndSetNulls($subValue, $model, true, $queuePersist, $queueRemove); } - $queuePersist[spl_object_hash($value)] = $value; + $queuePersist[spl_object_id($value)] = $value; } } } @@ -127,7 +127,7 @@ public static function getRelationships(IEntity $entity): array /** * @param PropertyMetadata[] $metadata * @param array> $pre - * @param array $queueRemove + * @param array $queueRemove */ private static function setNulls( IEntity $entity, @@ -168,7 +168,7 @@ private static function setNulls( assert($property instanceof HasOne); if ($reverseProperty !== null) { $reverseEntity = $property->getEntity(); - if ($reverseEntity === null || isset($queueRemove[spl_object_hash($reverseEntity)])) { + if ($reverseEntity === null || isset($queueRemove[spl_object_id($reverseEntity)])) { // reverse side is also being removed, do not set null to this relationship continue; }