Skip to content

[13.x] Improve types of InteractsWithData when* helpers#60536

Open
dfinchenko wants to merge 1 commit into
laravel:13.xfrom
dfinchenko:improve-types-interactswithdata-when-helpers
Open

[13.x] Improve types of InteractsWithData when* helpers#60536
dfinchenko wants to merge 1 commit into
laravel:13.xfrom
dfinchenko:improve-types-interactswithdata-when-helpers

Conversation

@dfinchenko

@dfinchenko dfinchenko commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

The InteractsWithData::whenEnum() method already carries full generic type information for its callback/default and return type, so static analysis can infer the precise result of a whenEnum(...) call. Its sibling conditional helpers in the same trait - whenHas(), whenFilled() and whenMissing() - share the identical shape (run $callback($value) when the condition holds, otherwise run $default() or return $this) but are still typed @return $this|mixed, so a call collapses to mixed.

This PR brings those three helpers in line by giving them the same generics:

@template TReturn
@template TReturnDefault = never

@param  string  $key
@param  callable(mixed): TReturn  $callback
@param  (callable(): TReturnDefault)|null  $default
@return $this|TReturn|TReturnDefault

The callback receives the resolved value (data_get($this->all(), $key)), which is mixed, hence callable(mixed): TReturn.

assertType coverage for all three helpers is added under types/. No 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