Skip to content

Clarify CastsAttributes::set() phpdoc to accept TGet; recommend symmetric casts#10

Open
andreas-pms wants to merge 1 commit into
illuminate:masterfrom
andreas-pms:feat/casts-accept-tget
Open

Clarify CastsAttributes::set() phpdoc to accept TGet; recommend symmetric casts#10
andreas-pms wants to merge 1 commit into
illuminate:masterfrom
andreas-pms:feat/casts-accept-tget

Conversation

@andreas-pms
Copy link
Copy Markdown

Problem

When a custom cast returns an object in get(), Eloquent may later pass that same runtime value into set() (e.g. after reading and re-assigning). Without acknowledging this, casts that accept only “assignment types” (TSet) can fail in common flows like:

$a = $model->attr;   // get(): returns VO (runtime), possibly cached
$model->attr = $a;   // reassign same VO
$model->save();      // set() receives the VO -> TypeError if set() expects only TSet

This is fundamentally about assignment symmetry: developers reasonably expect $a = $a = $b to be safe.

Rationale

  • Matches real call paths where set() can receive the runtime value produced by get().
  • Prevents surprising TypeErrors in userland casts after read→reassign→save cycles.
  • Encourages symmetric casts (TGet == TSet) so assignments remain intuitive.

No runtime behavior changes; this is a phpdoc clarification that improves developer expectations and static analysis.

… symmetric casts

- PhpDoc: explain assignment symmetry; set() can receive runtime value (TGet)
- Change @param to TGet|TSet|null
- Note on object caching; encourage symmetric casts (TGet == TSet)
- No runtime behavior changes
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.

1 participant