Skip to content

Commit

Permalink
fix types & phpdoc for RemovalHelper and reference arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 2, 2024
1 parent e8e7bcf commit 08bb9ff
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Repository/RemovalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
class RemovalHelper
{
/**
* @param array<string, IEntity|IRelationshipCollection<IEntity>> $queuePersist
* @param array<int, IEntity|bool> $queueRemove
* @param array<int, IEntity|IRelationshipContainer<IEntity>|IRelationshipCollection<IEntity>> $queuePersist
* @param array<int, IEntity> $queueRemove
*/
public static function getCascadeQueueAndSetNulls(
IEntity $entity,
Expand Down Expand Up @@ -51,7 +51,7 @@ public static function getCascadeQueueAndSetNulls(
foreach ($prePersist as $value) {
$queuePersist[spl_object_id($value)] = $value;
}
$queueRemove[$entityHash] = true;
$queueRemove[$entityHash] = $entity;
foreach ($pre as $value) {
if ($value instanceof IEntity) {
static::getCascadeQueueAndSetNulls($value, $model, true, $queuePersist, $queueRemove);
Expand All @@ -62,6 +62,7 @@ public static function getCascadeQueueAndSetNulls(
$queuePersist[spl_object_id($value)] = $value;
}
}
// re-enqueue to be at the last position
unset($queueRemove[$entityHash]);
$queueRemove[$entityHash] = $entity;
unset($queuePersist[$entityHash]);
Expand Down Expand Up @@ -126,7 +127,7 @@ public static function getRelationships(IEntity $entity): array

/**
* @param PropertyMetadata[] $metadata
* @param array<string, IEntity|IRelationshipCollection<IEntity>> $pre
* @param list<IEntity|IRelationshipContainer<IEntity>|IRelationshipCollection<IEntity>> $pre
* @param array<int, IEntity|bool> $queueRemove
*/
private static function setNulls(
Expand Down Expand Up @@ -154,12 +155,15 @@ private static function setNulls(
: null;

if ($type === Relationship::MANY_HAS_MANY) {
/** @var ManyHasMany<IEntity> $property */
$property = $entity->getProperty($name);
assert($property instanceof ManyHasMany);
$pre[] = $property;
if ($reverseProperty !== null) {
foreach ($property as $reverseEntity) {
$pre[] = $reverseEntity->getProperty($reverseProperty->name);
/** @var ManyHasMany<IEntity> $reverseRelationship */
$reverseRelationship = $reverseEntity->getProperty($reverseProperty->name);
$pre[] = $reverseRelationship;
}
}
$property->set([]);
Expand All @@ -172,7 +176,9 @@ private static function setNulls(
// The reverse side is also being removed, do not set null to this relationship.
continue;
}
$pre[] = $reverseEntity->getProperty($reverseProperty->name);
/** @var HasOne<IEntity> $reverseRelationship */
$reverseRelationship = $reverseEntity->getProperty($reverseProperty->name);
$pre[] = $reverseRelationship;
$pre[] = $reverseEntity;
}
$property->set(null, true);
Expand All @@ -184,6 +190,7 @@ private static function setNulls(
$property = $entity->getProperty($name);
assert($property instanceof IRelationshipCollection);
foreach ($property as $subValue) {
assert($subValue instanceof IEntity);
$pre[] = $subValue;
}
$property->set([]);
Expand Down

0 comments on commit 08bb9ff

Please sign in to comment.