Skip to content

[13.x] Improve the value types of the Collection put method - #61611

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
dfinchenko:improve-collection-put-types
Sep 16, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
dfinchenko:improve-collection-put-types

Conversation

@dfinchenko

Copy link
Copy Markdown
Contributor

Collection::getOrPut() gives its value its own template and tells PHPStan how the collection changes once the value is stored:

 * @template TGetOrPutValue
 *
 * @param  TKey  $key
 * @param  TGetOrPutValue|(\Closure(): TGetOrPutValue)  $value
 * @return TValue|TGetOrPutValue
 *
 * @phpstan-this-out static<TKey, TValue|TGetOrPutValue>

put(), the plainest way to do the same thing, is still typed against the value type the collection already has, so storing anything else is reported as an error even though the call is perfectly valid:

$users = collect([new User]);

$users->put(1, 'string');
Parameter #2 $value of method Illuminate\Support\Collection<int,User>::put() expects User, string given.

The collection's own type is not updated either, so everything read from it afterwards still looks like the original value type.

This gives put() the same treatment getOrPut() already has: its own template for the stored value, and a @phpstan-this-out describing the widened collection. The assertions added to types/Support/Collection.php cover both the returned collection and the receiver.

@taylorotwell
taylorotwell merged commit 59991e4 into laravel:13.x Sep 16, 2026
56 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