-
Notifications
You must be signed in to change notification settings - Fork 11.8k
[12.x] Add Collection::containsManyItems() method
#58312
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
Conversation
| if ($callback) { | ||
| return $this->filter($callback)->count() > 1; | ||
| } | ||
|
|
||
| return $this->count() > 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the goal here is to just see if there are at least two items in the collection matching the filter, wouldn't it be better to call each() and then break the loop once you've met the limit of 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically identical to the existing containsOneItem method to maintain consistency between the two:
framework/src/Illuminate/Collections/Collection.php
Lines 729 to 736 in cc03bd4
| public function containsOneItem(?callable $callback = null): bool | |
| { | |
| if ($callback) { | |
| return $this->filter($callback)->count() === 1; | |
| } | |
| return $this->count() === 1; | |
| } |
| * Determine if the collection contains multiple items. If a callback is provided, determine if multiple items match the condition. | ||
| * | ||
| * @param (callable(TValue, TKey): bool)|null $callback | ||
| * @return bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would make sense to add an assertion here and in the two corresponding methods
| * @return bool | |
| * @return bool | |
| * @phpstan-assert-if-true non-empty-array<TKey, TValue> $this->items |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed suit with the containsOneItem method doc block:
framework/src/Illuminate/Collections/Collection.php
Lines 723 to 736 in cc03bd4
| /** | |
| * Determine if the collection contains exactly one item. If a callback is provided, determine if exactly one item matches the condition. | |
| * | |
| * @param (callable(TValue, TKey): bool)|null $callback | |
| * @return bool | |
| */ | |
| public function containsOneItem(?callable $callback = null): bool | |
| { | |
| if ($callback) { | |
| return $this->filter($callback)->count() === 1; | |
| } | |
| return $this->count() === 1; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nobody said, there wasn't room for improvement. With this argument, we'd be stuck in the stone age forever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol easy -- its a doc block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more than a comment. It improves static analysis.
This PR adds the
containsManyItemsmethod to collections, like thecontainsOneItemmethod that already exists.This assists in keeping a uniform syntax in situations where you need to take a code path depending on the collection being empty, having one item, or having many: