Forward calls via ForwardsCalls trait instead of method_exists - #9
Merged
Merged
Conversation
Replace the method_exists() gate in CacheDecorator::callMethod() with Laravel's ForwardsCalls trait. Calls now reach methods the decorated object exposes via its own __call() magic, and self-returning fluent methods return the decorator so chaining stays on the cached surface. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the
method_exists($this->decorated, $method)gate inCacheDecorator::callMethod()with Laravel'sIlluminate\Support\Traits\ForwardsCallstrait, delegating throughforwardDecoratedCallTo().Why
The old gate meant calls were not forwarded to methods the decorated object exposes via its own
__call()/__callStatic()magic — they wrongly threwBadMethodCallException.ForwardsCalls(the same trait Eloquent'sBuilder/Modeluse) invokes the method and only converts a genuine "undefined method" failure into a cleanBadMethodCallException. This removes the limitation, aligns with idiomatic Laravel, and adds no new dependency (the trait ships inilluminate/support, already required).Changes
src/CacheDecorator.phpuse ForwardsCalls;and rewritecallMethod()toreturn $this->forwardDecoratedCallTo($this->decorated, $method, $arguments);.__call-reachable surface (forwardCallTo,forwardDecoratedCallTo,throwBadMethodCallException) toinitExcludes()defaults.BadMethodCallExceptionreadingCall to undefined method {Decorator}::{method}().return $this;,forwardDecoratedCallTo()returns the decorator instead, so chaining stays on the cached surface. Fluent/self-returning methods should be listed in$excludes(they aren't cache candidates and excluding them avoids caching a decorator instance on a miss).README.md: documents transparent forwarding to the inner's__call(), the new exception message, the "exclude fluent methods" caveat, and the shared-static-returning-interface type-coherence convention (per the repo's README-sync rule).StubMagicService+CachedMagicService(regression: a magic-only method is forwarded and cached) andStubFluentService+CachedFluentService+FluentServiceContract(a fluentreturn $this;call returns the decorator, not the inner object).Verification
composer test→ 31 tests, 48 assertions, OKvendor/bin/phpstan analyse→ no errors (level 8)vendor/bin/pint --test→ passed🤖 Generated with Claude Code