fix(model): never invoke non-attribute methods on property access#32
Merged
Conversation
The model Proxy probed every accessed property by invoking the same-named method to check whether it returned an Attribute (a Laravel-style accessor). That fired the method's side effects on a bare property read: - `model.recordView` ran recordView() before it was called, so `model.recordView(viewer)` (a read followed by a call) inserted twice — the reported 'relation-insert double-inserts' gotcha. - Reading a relation like `model.posts` invoked posts() on every access. resolveAttributeMutator now: - only probes zero-arity methods (accessors take no args; actions/relations like recordView(viewer) never qualify), - caches the accessor determination per class (an arity-0 method is probed at most once), and - on the read path additionally requires the key to be a model attribute (loaded value, append, cast, or mapped column), so even a zero-arity side-effecting method is never fired by a property read. Writes still probe (the key being assigned is an attribute by definition), so Attribute-object set mutators keep working during hydration and on fresh models. Tests: an argument-taking and a zero-arity side-effecting method are never invoked by property reads and run exactly once when called; Attribute-object get/set accessors and appends continue to resolve.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
The model Proxy probed every accessed property by invoking the same-named method to check whether it returned an Attribute (a Laravel-style accessor). That fired the method's side effects on a bare property read:
model.recordViewran recordView() before it was called, somodel.recordView(viewer)(a read followed by a call) inserted twice — the reported 'relation-insert double-inserts' gotcha.model.postsinvoked posts() on every access.resolveAttributeMutator now:
Tests: an argument-taking and a zero-arity side-effecting method are never invoked by property reads and run exactly once when called; Attribute-object get/set accessors and appends continue to resolve.