Skip to content

Commit 4d345ec

Browse files
committed
writes: name the snapshot-clearing rule as invalidateSourceRows()
The two-line staleness pair was copied in setElement, __unset, and the deprecated offsetUnset, with the rationale comment repeated at each site. One private helper now holds both lines and the why; a future write path calls it instead of having to notice and copy the pattern. Missing it means derived sets serve pre-write data from their carried snapshots.
1 parent 139b0e4 commit 4d345ec

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/Deprecations.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ public function offsetUnset(mixed $offset): void
535535
{
536536
$offset = self::coerceOffset($offset);
537537
self::triggerArrayAccessDeprecation($offset, 'unset');
538-
$this->sourceRows = null; // same staleness rule as setElement()
539-
$this->root->sourceRows = null;
538+
$this->invalidateSourceRows();
540539
unset($this->data[$offset]);
541540
}
542541

src/SmartArrayBase.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,25 @@ public function at(int|SmartString|SmartNull $index): static|SmartNull|SmartStri
374374
};
375375
}
376376

377+
/**
378+
* A write makes kept source rows stale: clears this array's snapshot and its result
379+
* set's ($this->root is the result set on rows, $this on top-level arrays). Every
380+
* write path must call this, or toArray() can serve pre-write data (see $sourceRows).
381+
*/
382+
private function invalidateSourceRows(): void
383+
{
384+
$this->sourceRows = null;
385+
$this->root->sourceRows = null;
386+
}
387+
377388
/**
378389
* Stores an element with automatic type conversion.
379390
* Scalars and nulls are stored as-is; arrays are converted to SmartArray instances.
380391
* Smart values are unwrapped first, so `$a->key = $b->key` works in any mode.
381392
*/
382393
private function setElement(int|string|null $key, mixed $value): void
383394
{
384-
// A write makes kept source rows stale: this array's own copy, and its result
385-
// set's when this is a row ($this->root is the result set on rows, $this on
386-
// top-level arrays)
387-
$this->sourceRows = null;
388-
$this->root->sourceRows = null;
395+
$this->invalidateSourceRows();
389396

390397
// Unwrap Smart values (SmartString, SmartArray, SmartNull) to their raw
391398
// equivalents; nested arrays then convert to this array's mode below.
@@ -1436,8 +1443,7 @@ public function __isset(string $name): bool
14361443
*/
14371444
public function __unset(string $name): void
14381445
{
1439-
$this->sourceRows = null; // same staleness rule as setElement()
1440-
$this->root->sourceRows = null;
1446+
$this->invalidateSourceRows();
14411447
unset($this->data[$name]);
14421448
}
14431449

0 commit comments

Comments
 (0)