Skip to content

Commit 119840d

Browse files
authored
[10.x] Update ensure() collection method to correctly work with Interfaces and object inheritance (#47934)
* Update ensure() collection method to correctly work with Interfaces and object inheritance. * corrections to style
1 parent 56d59a7 commit 119840d

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ public function value($key, $default = null)
315315
/**
316316
* Ensure that every item in the collection is of the expected type.
317317
*
318-
* @template TEnforceIntoValue
318+
* @template TEnsureOfType
319319
*
320-
* @param class-string<TEnforceIntoValue> $type
321-
* @return static<mixed, TEnforceIntoValue>
320+
* @param class-string<TEnsureOfType> $type
321+
* @return static<mixed, TEnsureOfType>
322322
*
323323
* @throws \UnexpectedValueException
324324
*/
@@ -327,8 +327,10 @@ public function ensure($type)
327327
return $this->each(function ($item) use ($type) {
328328
$itemType = get_debug_type($item);
329329

330-
if ($itemType !== $type) {
331-
throw new UnexpectedValueException("Collection should only include '{$type}' items, but '{$itemType}' found.");
330+
if ($itemType !== $type && ! $item instanceof $type) {
331+
throw new UnexpectedValueException(
332+
sprintf("Collection should only include '%s' items, but '%s' found.", $type, $itemType)
333+
);
332334
}
333335
});
334336
}

tests/Support/SupportCollectionTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,17 +5598,40 @@ public function testDot($collection)
55985598
/**
55995599
* @dataProvider collectionClassProvider
56005600
*/
5601-
public function testEnsure($collection)
5601+
public function testEnsureForScalar($collection)
56025602
{
56035603
$data = $collection::make([1, 2, 3]);
5604-
56055604
$data->ensure('int');
56065605

56075606
$data = $collection::make([1, 2, 3, 'foo']);
5607+
$this->expectException(UnexpectedValueException::class);
5608+
$data->ensure('int');
5609+
}
5610+
5611+
/**
5612+
* @dataProvider collectionClassProvider
5613+
*/
5614+
public function testEnsureForObjects($collection)
5615+
{
5616+
$data = $collection::make([new stdClass, new stdClass, new stdClass]);
5617+
$data->ensure(stdClass::class);
56085618

5619+
$data = $collection::make([new stdClass, new stdClass, new stdClass, $collection]);
56095620
$this->expectException(UnexpectedValueException::class);
5621+
$data->ensure(stdClass::class);
5622+
}
56105623

5611-
$data->ensure('int');
5624+
/**
5625+
* @dataProvider collectionClassProvider
5626+
*/
5627+
public function testEnsureForInheritance($collection)
5628+
{
5629+
$data = $collection::make([new \Error, new \Error]);
5630+
$data->ensure(\Throwable::class);
5631+
5632+
$data = $collection::make([new \Error, new \Error, new $collection]);
5633+
$this->expectException(UnexpectedValueException::class);
5634+
$data->ensure(\Throwable::class);
56125635
}
56135636

56145637
/**

0 commit comments

Comments
 (0)