Skip to content

Commit

Permalink
Add type tests for HigherOrderCollectionProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltra committed Aug 18, 2024
1 parent 439a8ac commit ec27f7d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/HigherOrderCollectionProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @template TKey of array-key
*
* @template-covariant TValue of mixed
* @template-covariant TValue
*
* @mixin \Illuminate\Support\Enumerable<TKey, TValue>
* @mixin TValue
Expand Down
31 changes: 31 additions & 0 deletions types/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,3 +1112,34 @@ public function getWithoutZebras(): Collection
$zoo = new Zoo();

assertType('Illuminate\Support\Collection<int, Animal>', $zoo->getWithoutZebras());

$coll = $zoo->getWithoutZebras();
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->average);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->avg);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->contains);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->doesntContain);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->each);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->every);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->filter);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->first);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->flatMap);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->groupBy);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->keyBy);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->map);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->max);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->min);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->partition);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->percentage);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->reject);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->skipUntil);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->skipWhile);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->some);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->sortBy);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->sortByDesc);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->sum);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->takeUntil);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->takeWhile);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->unique);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->unless);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->until);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, Animal>', $coll->when);
75 changes: 75 additions & 0 deletions types/Support/LazyCollection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;

use function PHPStan\Testing\assertType;
Expand Down Expand Up @@ -856,3 +857,77 @@ class CustomLazyCollection extends LazyCollection
assertType('int', $int);
assertType('User', $user);
}


class LazyAnimal
{
}
class LazyTiger extends LazyAnimal
{
}
class LazyLion extends LazyAnimal
{
}
class LazyZebra extends LazyAnimal
{
}

class LazyZoo
{
/**
* @var \Illuminate\Support\Collection<int, LazyAnimal>
*/
private Collection $animals;

public function __construct()
{
$this->animals = collect([
new LazyTiger,
new LazyLion,
new LazyZebra,
]);
}

/**
* @return \Illuminate\Support\LazyCollection<int, LazyAnimal>
*/
public function getWithoutZebras(): LazyCollection
{
return $this->animals->lazy()->filter(fn (LazyAnimal $animal) => ! $animal instanceof LazyZebra);
}
}

$zoo = new LazyZoo();

$coll = $zoo->getWithoutZebras();

assertType('Illuminate\Support\LazyCollection<int, LazyAnimal>', $coll);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->average);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->avg);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->contains);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->doesntContain);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->each);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->every);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->filter);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->first);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->flatMap);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->groupBy);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->keyBy);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->map);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->max);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->min);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->partition);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->percentage);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->reject);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->skipUntil);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->skipWhile);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->some);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->sortBy);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->sortByDesc);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->sum);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->takeUntil);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->takeWhile);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->unique);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->unless);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->until);
assertType('Illuminate\Support\HigherOrderCollectionProxy<int, LazyAnimal>', $coll->when);

0 comments on commit ec27f7d

Please sign in to comment.