Skip to content

Conversation

@stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Jan 7, 2026

This PR adds the containsManyItems method to collections, like the containsOneItem method 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:

match (true) {
    $collection->isEmpty() => '...',
    $collection->containsOneItem() => '...',
    $collection->containsManyItems() => '...',
};

Comment on lines 746 to 750
if ($callback) {
return $this->filter($callback)->count() > 1;
}

return $this->count() > 1;
Copy link
Contributor

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?

Copy link
Contributor Author

@stevebauman stevebauman Jan 7, 2026

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:

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
Copy link
Contributor

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

Suggested change
* @return bool
* @return bool
* @phpstan-assert-if-true non-empty-array<TKey, TValue> $this->items

Copy link
Contributor Author

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:

/**
* 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;
}

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

@taylorotwell taylorotwell merged commit 71581d2 into laravel:12.x Jan 7, 2026
70 checks passed
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.

4 participants