Skip to content

[13.x] Fix sum() return type when the key matches a global function name - #61578

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
crishoj:fix/collection-string-callback-inference
Sep 14, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
crishoj:fix/collection-string-callback-inference

Conversation

@crishoj

@crishoj crishoj commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

useAsCallable() is ! is_string($value) && is_callable($value), so a string callback is never invoked — valueRetriever() always turns it into data_get($item, $value).

However, the $callback is callable conditional does not know that, so any key sharing a name with a global function takes the callable branch and binds TReturnType to that function's return type.

E.g.: sum('today') infers CarbonInterface from the today() helper; sum('count') leaves TReturnType unresolved and reports argument.templateType under phpstan 2.2.13+.

Testing the string case first keeps every other inference as it was: sum('string') stays mixed and the callable form still resolves TReturnType.

`useAsCallable()` is `! is_string($value) && is_callable($value)`, so a
string callback is never invoked — `valueRetriever()` always turns it
into `data_get($item, $value)`. The `$callback is callable` conditional
does not know that, so any key sharing a name with a global function
takes the callable branch and binds `TReturnType` to that function's
return type. `sum('today')` infers `CarbonInterface` from the `today()`
helper; `sum('count')` leaves `TReturnType` unresolved and reports
`argument.templateType` under phpstan 2.2.13+.

Testing the string case first keeps every other inference as it was:
`sum('string')` stays `mixed` and the callable form still resolves
`TReturnType`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@taylorotwell taylorotwell reopened this Sep 14, 2026
@taylorotwell
taylorotwell merged commit 2c06ec3 into laravel:13.x Sep 14, 2026
105 of 109 checks passed
binaryfire added a commit to hypervel/components-backup that referenced this pull request Oct 1, 2026
Handle string outer keys in collapseWithKeys and describe receiver types after collection mutation, including nullable keys. Return base collections for Eloquent duplicate attribute or callback values; keep model collections when no callback is supplied.

Correct mapped duplicate and sum types, annotate empty builders and Wayfinder defaults, and preserve explicit null-key behavior without PHP 8.5 deprecations. Consolidate overlapping coverage. Collection tests and full type analysis pass.

Upstream:
laravel/framework#61539
laravel/framework#61571
laravel/framework#61572
laravel/framework#61578
laravel/framework#61597
laravel/framework#61611
laravel/framework#61621

Source: laravel/framework at 7068848dfe48fc3a433598e09ce798799d442a52.
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