Skip to content

Commit

Permalink
fix phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Oct 30, 2023
1 parent 3393f47 commit 7612464
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/Resources/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Iterator;
use IteratorAggregate;

/**
* @template TKey of array-key
* @template TValue
*/
class LazyCollection implements IteratorAggregate
{
/**
Expand Down Expand Up @@ -33,7 +37,7 @@ public function all(): array
/**
* Get an item from the collection by key.
*
* @param int|string $key
* @param int|string $key
* @return mixed|null
*/
public function get($key)
Expand All @@ -50,12 +54,12 @@ public function get($key)
/**
* Run a filter over each of the items.
*
* @param (callable(value, key): bool) $callback
* @return static
* @param (callable(TValue, TKey): bool) $callback
* @return self
*/
public function filter(callable $callback): self
{
return new static(function () use ($callback) {
return new self(function () use ($callback) {
foreach ($this as $key => $value) {
if ($callback($value, $key)) {
yield $key => $value;
Expand All @@ -67,15 +71,15 @@ public function filter(callable $callback): self
/**
* Get the first item from the collection passing the given truth test.
*
* @param (callable(value): bool)|null $callback
* @return mixed|null
* @param (callable(TValue, TKey): bool)|null $callback
* @return TValue|null
*/
public function first(callable $callback = null)
{
$iterator = $this->getIterator();

if (is_null($callback)) {
if (! $iterator->valid()) {
if (!$iterator->valid()) {
return null;
}

Expand All @@ -94,12 +98,14 @@ public function first(callable $callback = null)
/**
* Run a map over each of the items.
*
* @param callable(value, key): mixed $callback
* @return static<key, mixed>
* @template TMapValue
*
* @param callable(TValue, TKey): TMapValue $callback
* @return static<TKey, TMapValue>
*/
public function map(callable $callback): self
{
return new static(function () use ($callback) {
return new self(function () use ($callback) {
foreach ($this as $key => $value) {
yield $key => $callback($value, $key);
}
Expand All @@ -109,16 +115,16 @@ public function map(callable $callback): self
/**
* Take the first or last {$limit} items.
*
* @param int $limit
* @param int $limit
* @return static
*/
public function take(int $limit): self
{
return new static(function () use ($limit) {
return new self(function () use ($limit) {
$iterator = $this->getIterator();

while ($limit--) {
if (! $iterator->valid()) {
if (!$iterator->valid()) {
break;
}

Expand All @@ -134,15 +140,15 @@ public function take(int $limit): self
/**
* Determine if all items pass the given truth test.
*
* @param callable $callback
* @param (callable(TValue, TKey): bool) $callback
* @return bool
*/
public function every(callable $callback): bool
{
$iterator = $this->getIterator();

foreach ($iterator as $key => $value) {
if (! $callback($value, $key)) {
if (!$callback($value, $key)) {
return false;
}
}
Expand All @@ -163,7 +169,7 @@ public function count(): int
/**
* Get an iterator for the items.
*
* @return Iterator
* @return Iterator<TKey, TValue>
*/
public function getIterator(): Iterator
{
Expand All @@ -173,8 +179,11 @@ public function getIterator(): Iterator
/**
* Get an iterator for the given value.
*
* @param callable|IteratorAggregate $source
* @return Iterator
* @template TIteratorKey of array-key
* @template TIteratorValue
*
* @param IteratorAggregate<TIteratorValue>|(callable(): \Generator<TIteratorKey, TIteratorValue>) $source
* @return Iterator<TIteratorValue>
*/
protected function makeIterator($source): Iterator
{
Expand Down

0 comments on commit 7612464

Please sign in to comment.