Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add Collection::enforce() method #47785

Merged
merged 5 commits into from
Jul 27, 2023

Conversation

inxilpro
Copy link
Contributor

@inxilpro inxilpro commented Jul 19, 2023

This adds a new Collection::enforce() method that enforces the type of all items in the collection (and also applies type annotations). This allows for the following code:

// $user is guaranteed to be a User or null
$user = collect($users)
  ->enforce(User::class)
  ->first();

It also means that for developers who use an IDE that parses type annotations, calling enforce will also enable code intelligence:

image

@cosmastech
Copy link
Contributor

The way this is worded, I would expect enforce() would guard against future additions to the collection from being of the wrong type, or maybe that it would cast them to the proper type where possible:

// guarding against incorrect additions
$intCollection = collect()->enforce('int');
$int->push('hello world');
// coercion
$users = User::get();
$usersArray = $users->enforce('array');

Not proposing that either needs to be added, but maybe it being named something other than "enforce" would provide greater specificity. It really is type assertion, not enforcement.

@driesvints
Copy link
Member

driesvints commented Jul 25, 2023

@inxilpro can you rebase against 10.x?

@inxilpro
Copy link
Contributor Author

The way this is worded, I would expect enforce() would guard against future additions to the collection from being of the wrong type

@cosmastech my hope is that it will eventually be possible to attach a type to the Collection and have it be enforced for future items as well (like the current Eloquent Collection implementation). This is a first step towards that. Baby steps :)

@inxilpro
Copy link
Contributor Author

It's also worth mentioning that this PR was the brainchild of @vanere at Laracon. We were talking about it during the afterparty and decided to just write the code then-and-there. It was pretty fun—a few other Laracon attendees peeked at the code and gave feedback on the implementation :)

@timacdonald
Copy link
Member

timacdonald commented Jul 25, 2023

Perhaps to not make it feel like it will continue to only allow a particular type, ensure might be a little "softer" and point in time.

collect($users)->ensure(User::class)?

@inxilpro
Copy link
Contributor Author

I like ensure! I’ll update tonight or tomorrow.

@hebertcisco
Copy link

❤️

@taylorotwell taylorotwell merged commit 45b61f5 into laravel:10.x Jul 27, 2023
16 checks passed
@taylorotwell
Copy link
Member

Thanks

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.

None yet

6 participants