Skip to content

Fix set() not clearing cached instance on override#9

Merged
ChiragAgg5k merged 1 commit intomainfrom
fix/set-clears-cached-instance
Mar 21, 2026
Merged

Fix set() not clearing cached instance on override#9
ChiragAgg5k merged 1 commit intomainfrom
fix/set-clears-cached-instance

Conversation

@ChiragAgg5k
Copy link
Copy Markdown
Member

Summary

  • When set() was called to override an already-resolved dependency, get() still returned the stale cached value from $concrete
  • Added unset($this->concrete[$id]) in set() to clear the cache when a dependency is re-registered
  • Added test testSetOverridesCachedInstance that resolves a dependency, overrides it via set(), and verifies the new value is returned

Test plan

  • All 31 existing tests pass
  • New test covers the resolve-then-override scenario

When calling set() to override an already-resolved dependency, the stale
cached value in $concrete was still returned by get(). Now set() clears
the cache entry so the new factory is used on the next get() call.
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Mar 20, 2026

Greptile Summary

This PR fixes a bug where calling set() to re-register an already-resolved dependency would leave a stale entry in $this->concrete, causing subsequent get() calls to return the old cached value. The one-line fix (unset($this->concrete[$id])) is correct and minimal, and is accompanied by a focused regression test.

Key changes:

  • src/DI/Container.php: Clears the resolved-instance cache entry for $id whenever set() is called, ensuring the new factory is invoked on the next get().
  • tests/ContainerTest.php: Adds testSetOverridesCachedInstance to cover the resolve-then-override scenario that was previously broken.

Notable limitation: The fix only invalidates the cache for the directly overridden dependency. Dependents that were already resolved and cached using the old value of $id (i.e., entries in $this->concrete whose $this->dependencies list includes $id) will continue to return stale results. This is worth documenting or addressing depending on the intended container semantics.

Confidence Score: 4/5

  • Safe to merge — the fix is correct and well-tested, with only a minor design gap around cascading cache invalidation.
  • The change is a single, targeted line that directly solves the stated bug, all existing tests pass, and a new test covers the exact scenario. The one deduction is for the unaddressed cascading invalidation case (already-cached dependents of an overridden dependency), which could cause subtle bugs in more complex dependency graphs.
  • src/DI/Container.php — review the cascading cache-invalidation limitation noted in the inline comment.

Important Files Changed

Filename Overview
src/DI/Container.php Adds unset($this->concrete[$id]) in set() to clear the stale cached instance on override. The fix is correct for the direct entry, but doesn't cascade invalidation to already-cached dependents that relied on the old value.
tests/ContainerTest.php Adds testSetOverridesCachedInstance which directly exercises the resolve-then-override scenario. Well-structured and targets the exact bug fixed.

Last reviewed commit: "Fix set() not cleari..."

Comment thread src/DI/Container.php
Comment thread src/DI/Container.php Outdated
@ChiragAgg5k ChiragAgg5k force-pushed the fix/set-clears-cached-instance branch from 6f6e323 to 133705f Compare March 21, 2026 06:15
@ChiragAgg5k ChiragAgg5k merged commit 07025d7 into main Mar 21, 2026
13 checks passed
@ChiragAgg5k ChiragAgg5k deleted the fix/set-clears-cached-instance branch March 21, 2026 07:42
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.

2 participants