Skip to content

Correct parameter type of Collection::diffKeys() and Collection::diffKeysUsing()#53441

Merged
taylorotwell merged 1 commit into
laravel:11.xfrom
AJenbo:diffkeys
Nov 7, 2024
Merged

Correct parameter type of Collection::diffKeys() and Collection::diffKeysUsing()#53441
taylorotwell merged 1 commit into
laravel:11.xfrom
AJenbo:diffkeys

Conversation

@AJenbo
Copy link
Copy Markdown
Contributor

@AJenbo AJenbo commented Nov 7, 2024

Corrected the parameter type in the PHPDoc for Collection::diffKeys() and Collection::diffKeysUsing() from array<TKey, TValue> to array<TKey, mixed>, as these methods only operate on the keys of the given array, making the value type irrelevant.

This lets you do something like:

$animals = Animal::pluck('name', 'id');
$animalFood = Food::pluck('id', 'animal_id');

if ($animals->diffKeys($animalFood) as $id) {
    throw new Exception('Missing food for ' . $animals[$id]);
}

Tools previously flagged this usage as illegal due to the differing value types (food.id as int vs. animal.name as string), despite only the keys being compared.

@taylorotwell taylorotwell merged commit 9c86dd9 into laravel:11.x Nov 7, 2024
@AJenbo AJenbo deleted the diffkeys branch November 7, 2024 22:57
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