Skip to content

Commit 2922575

Browse files
nunomaduroRocksheepStyleCIBot
authored
[10.x] Uses @template-covariant in collections (#46872)
* docs: update collection docs to use template-covariant for the values This allows developers to use classes with inheritance on collections. See types/Support/Collection.php for an example * Revert `@template-covariant` on Arrayable * Apply fixes from StyleCI --------- Co-authored-by: rvanvelzen <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
1 parent 1793066 commit 2922575

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
/**
1414
* @template TKey of array-key
15-
* @template TValue
15+
*
16+
* @template-covariant TValue
1617
*
1718
* @implements \ArrayAccess<TKey, TValue>
1819
* @implements \Illuminate\Support\Enumerable<TKey, TValue>

src/Illuminate/Collections/Enumerable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
/**
1414
* @template TKey of array-key
15-
* @template TValue
15+
*
16+
* @template-covariant TValue
1617
*
1718
* @extends \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
1819
* @extends \IteratorAggregate<TKey, TValue>

src/Illuminate/Collections/LazyCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
/**
1818
* @template TKey of array-key
19-
* @template TValue
19+
*
20+
* @template-covariant TValue
2021
*
2122
* @implements \Illuminate\Support\Enumerable<TKey, TValue>
2223
*/

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
/**
2121
* @template TKey of array-key
22-
* @template TValue
22+
*
23+
* @template-covariant TValue
2324
*
2425
* @property-read HigherOrderCollectionProxy $average
2526
* @property-read HigherOrderCollectionProxy $avg

types/Support/Collection.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,3 +1055,45 @@ class CustomCollection extends Collection
10551055
assertType('int', $int);
10561056
assertType('User', $user);
10571057
}
1058+
1059+
class Animal
1060+
{
1061+
}
1062+
class Tiger extends Animal
1063+
{
1064+
}
1065+
class Lion extends Animal
1066+
{
1067+
}
1068+
class Zebra extends Animal
1069+
{
1070+
}
1071+
1072+
class Zoo
1073+
{
1074+
/**
1075+
* @var \Illuminate\Support\Collection<int, Animal>
1076+
*/
1077+
private Collection $animals;
1078+
1079+
public function __construct()
1080+
{
1081+
$this->animals = collect([
1082+
new Tiger,
1083+
new Lion,
1084+
new Zebra,
1085+
]);
1086+
}
1087+
1088+
/**
1089+
* @return \Illuminate\Support\Collection<int, Animal>
1090+
*/
1091+
public function getWithoutZebras(): Collection
1092+
{
1093+
return $this->animals->filter(fn (Animal $animal) => ! $animal instanceof Zebra);
1094+
}
1095+
}
1096+
1097+
$zoo = new Zoo();
1098+
1099+
assertType('Illuminate\Support\Collection<int, Animal>', $zoo->getWithoutZebras());

0 commit comments

Comments
 (0)