Skip to content

Commit

Permalink
Merge pull request #605 from mabar/spl-object-id
Browse files Browse the repository at this point in the history
Use spl_object_id instead of spl_object_hash
  • Loading branch information
hrach authored Feb 7, 2023
2 parents aff8cc4 + d2f9cb6 commit 94d1d44
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/Collection/HasManyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Dbal/DbalMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 18 additions & 18 deletions src/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -50,19 +50,19 @@ abstract class HasMany implements IRelationshipCollection

/**
* @var IEntity[]
* @phpstan-var array<string, E>
* @phpstan-var array<int, E>
*/
protected $toAdd = [];

/**
* @var IEntity[]
* @phpstan-var array<string,E>
* @phpstan-var array<int,E>
*/
protected $toRemove = [];

/**
* @var IEntity[]
* @phpstan-var array<string, E>
* @phpstan-var array<int, E>
*/
protected $tracked = [];

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Relationships/IRelationshipCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function trackEntity(IEntity $entity): void;
/**
* Returns IEntity for persistence.
* @return IEntity[]
* @phpstan-return array<string, E>
* @phpstan-return array<int, E>
* @ignore
* @internal
*/
Expand Down
18 changes: 9 additions & 9 deletions src/Repository/PersistenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class PersistenceHelper
/** @var array<int, IRelationshipCollection<IEntity>|IRelationshipContainer<IEntity>> */
protected static $inputQueue = [];

/** @var array<string, IEntity|IRelationshipCollection<IEntity>|IRelationshipContainer<IEntity>|true> */
/** @var array<int, IEntity|IRelationshipCollection<IEntity>|IRelationshipContainer<IEntity>|true> */
protected static $outputQueue = [];


/**
* @see https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
* @return array<string, IEntity|IRelationshipCollection<IEntity>|IRelationshipContainer<IEntity>|true>
* @return array<int, IEntity|IRelationshipCollection<IEntity>|IRelationshipContainer<IEntity>|true>
*/
public static function getCascadeQueue(IEntity $entity, IModel $model, bool $withCascade): array
{
Expand All @@ -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) {
Expand All @@ -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;
}


Expand All @@ -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;
}


Expand Down
14 changes: 7 additions & 7 deletions src/Repository/RemovalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RemovalHelper
{
/**
* @param array<string, IEntity|IRelationshipCollection<IEntity>> $queuePersist
* @param array<string, IEntity|bool> $queueRemove
* @param array<int, IEntity|bool> $queueRemove
*/
public static function getCascadeQueueAndSetNulls(
IEntity $entity,
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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]);
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public static function getRelationships(IEntity $entity): array
/**
* @param PropertyMetadata[] $metadata
* @param array<string, IEntity|IRelationshipCollection<IEntity>> $pre
* @param array<string, IEntity|bool> $queueRemove
* @param array<int, IEntity|bool> $queueRemove
*/
private static function setNulls(
IEntity $entity,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 94d1d44

Please # to comment.