Skip to content

Track whether Metadata has changed - #6

Merged
loevgaard merged 2 commits into
masterfrom
track-metadata-changes
Nov 23, 2026
Merged

loevgaard merged 2 commits into
masterfrom
track-metadata-changes

Conversation

@loevgaard

Copy link
Copy Markdown
Member

Summary

Metadata::hasChanged() returns true once a call has actually altered the values or expiry times. Constructing never counts, and nothing resets it.

 set(key, value, ttl)
   reject reserved key
+  reject ttl <= 0                         # moved up: a rejected call stores nothing
+  if key is new or value !== current
+    changed = true
   store value
   if ttl given
-    reject ttl <= 0
+    if expiry moves
+      changed = true
     store expiry

 remove(key)
+  if key has a value or an expiry
+    changed = true
   drop value and expiry

Pruning in has()/get()/ArrayAccess reads/count()/iteration/toArray() still goes through the overridable remove(), so dropping an expired key counts as a change. That routing is kept on purpose, and a test fixes it in place: released setono/client-bundle versions (^1.1.1) override remove() to notice dropped keys.

The README's Metadata section has a new "Tracking changes" part listing what counts as a change.

Evidence

  • Before (origin/master):
    set(a, 1, ttl: 0) threw: The ttl must be greater than 0
    has(a) after the rejected set(): true
    Error: Call to undefined method Setono\Client\Metadata::hasChanged()
    
    After (this branch):
    set(a, 1, ttl: 0) threw: The ttl must be greater than 0
    has(a) after the rejected set(): false
    hasChanged() after setting the same value: false
    hasChanged() after setting a different value: true
    
  • PHPUnit: 67 tests pass, 32 of them new, with one case per call that drops an expired key. PHPStan (max), ECS and Rector are clean. Infection: no escaped mutants in Metadata. The 4 that escape are the known Client.php UUID match arms.

Merge Danger

Door: two-way until 1.2.0 is tagged

After the tag, hasChanged() is public API and the bundle will rely on it, so taking it out would be a BC break.

Blast Radius: narrow

  • set() with a TTL of 0 or less no longer stores the value before throwing. Only code that catches that exception and then reads the key would notice.
  • A subclass that already declares its own hasChanged() with a different signature would fatal. client-bundle's ChangeAwareMetadata uses isDirty(), so it doesn't clash.
  • How a null TTL treats an existing expiry is unchanged (set() without a TTL keeps the key's old expiry #4).

Closes #5

Metadata::hasChanged() returns true once a call has altered the values or
expiry times: set() of a new key, a different value or a moved expiry,
remove()/unset() of an existing key, and dropping an expired key on any
read that prunes. Constructing, reading live keys and jsonSerialize() are
not changes, and the flag is never reset.

set() now validates the TTL before storing anything, so a rejected call
leaves the metadata as it was.

Pruning still goes through the overridable remove(), because released
setono/client-bundle versions override it to notice dropped keys.

Closes #5
Rector 2.8 adds IfToNullCoalescingAssignRector, which fails the dry-run in the highest-dependencies Coding Standards job.
@loevgaard
loevgaard merged commit 4a9c585 into master Nov 23, 2026
27 checks passed
@loevgaard
loevgaard deleted the track-metadata-changes branch November 23, 2026 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Track whether Metadata has changed

1 participant