Skip to content

Commit

Permalink
Update ensure() collection method to correctly work with Interfaces a…
Browse files Browse the repository at this point in the history
…nd object inheritance (#296)

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Aug 2, 2023
1 parent 2ce8c5a commit 90cf69a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions output/Hyperf/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function doesntContain($key, $operator = null, $value = null)
/**
* Ensure that every item in the collection is of the expected type.
*
* @template TEnforceIntoValue
* @template TEnsureOfType
*
* @param class-string<TEnforceIntoValue> $type
* @return static<mixed, TEnforceIntoValue>
* @param class-string<TEnsureOfType> $type
* @return static<mixed, TEnsureOfType>
*
* @throws UnexpectedValueException
*/
Expand Down
6 changes: 4 additions & 2 deletions src/CollectionMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public function ensure()
return fn ($type) => $this->each(function ($item) use ($type) {
$itemType = get_debug_type($item);

if ($itemType !== $type) {
throw new UnexpectedValueException("Collection should only include '{$type}' items, but '{$itemType}' found.");
if ($itemType !== $type && ! $item instanceof $type) {
throw new UnexpectedValueException(
sprintf("Collection should only include '%s' items, but '%s' found.", $type, $itemType)
);
}
});
}
Expand Down

0 comments on commit 90cf69a

Please sign in to comment.