Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"doctrine/doctrine-bundle": "^2.7",
"doctrine/orm": "^2.15|^3.0",
"pagerfanta/pagerfanta": "^1.0|^2.0|^3.0|^4.0",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan": "^2.1.17",
"phpunit/phpunit": "^9.6.21",
"symfony/expression-language": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
Expand Down
21 changes: 13 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @template K
* @template V
* @template K = array-key
* @extends \IteratorAggregate<K,V>
*
* @method static dump()
Expand All @@ -31,7 +31,7 @@ interface Collection extends \IteratorAggregate, \Countable
/**
* @param mixed|callable(V,K):bool $specification
*
* @return self<K,V>
* @return self<V,K>
*
* @throws InvalidSpecification if $specification is not valid
*/
Expand All @@ -42,7 +42,7 @@ public function filter(mixed $specification): self;
*
* @param callable(V,K):T $function
*
* @return self<K,T>
* @return self<T,K>
*/
public function map(callable $function): self;

Expand All @@ -51,12 +51,12 @@ public function map(callable $function): self;
*
* @param callable(V,K):T $function
*
* @return self<T,V>
* @return self<V,T>
*/
public function keyBy(callable $function): self;

/**
* @return self<K,V>
* @return self<V,K>
*/
public function take(int $limit, int $offset = 0): self;

Expand Down Expand Up @@ -94,17 +94,22 @@ public function reduce(callable $function, mixed $initial = null): mixed;
public function isEmpty(): bool;

/**
* @return ArrayCollection<K&array-key,V>
* @return ArrayCollection<V,K&array-key>
*/
public function eager(): ArrayCollection;

/**
* @return Page<K,V>
* @param positive-int $page
* @param positive-int $limit
*
* @return Page<V,K>
*/
public function paginate(int $page = 1, int $limit = Page::DEFAULT_LIMIT): Page;

/**
* @return Pages<K,V>
* @param positive-int $limit
*
* @return Pages<V,K>
*/
public function pages(int $limit = Page::DEFAULT_LIMIT): Pages;
}
68 changes: 34 additions & 34 deletions src/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
*
* @immutable
*
* @template K of array-key
* @template V
* @implements Collection<K,V>
* @template K of array-key = array-key
* @implements Collection<V,K>
*/
final class ArrayCollection implements Collection
{
/** @use IterableCollection<K,V> */
/** @use IterableCollection<V,K> */
use IterableCollection;

/** @var array<K,V> */
Expand All @@ -50,15 +50,15 @@ public function __construct(iterable|callable|null $source = null)
/**
* @param null|iterable<K,V>|callable():iterable<K,V> $source
*
* @return self<K,V>
* @return self<V,K>
*/
public static function for(iterable|callable|null $source = null): self
{
return new self($source);
}

/**
* @return self<array-key,mixed>
* @return self<mixed>
*/
public static function wrap(mixed $value): self
{
Expand All @@ -76,7 +76,7 @@ public static function wrap(mixed $value): self
*
* @param non-empty-string $separator
*
* @return self<int,string>
* @return self<string,int>
*/
public static function explode(string $separator, string $string, ?int $limit = null): self
{
Expand All @@ -93,7 +93,7 @@ public static function explode(string $separator, string $string, ?int $limit =
* @param T $start
* @param T $end
*
* @return self<int,T>
* @return self<T,int>
*/
public static function range(int|string|float $start, int|string|float $end, int|float $step = 1): self
{
Expand All @@ -107,7 +107,7 @@ public static function range(int|string|float $start, int|string|float $end, int
*
* @param T $value
*
* @return self<int,T>
* @return self<T,int>
*/
public static function fill(int $start, int $count, mixed $value): self
{
Expand All @@ -120,7 +120,7 @@ public function first(mixed $default = null): mixed
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function take(int $limit, int $offset = 0): self
{
Expand All @@ -136,31 +136,31 @@ public function all(): array
}

/**
* @return self<int,K>
* @return self<K,int>
*/
public function keys(): self
{
return new self(\array_keys($this->source));
}

/**
* @return self<int,V>
* @return self<V,int>
*/
public function values(): self
{
return new self(\array_values($this->source));
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function reverse(): self
{
return new self(\array_reverse($this->source, true));
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function slice(int $offset, ?int $length = null): self
{
Expand All @@ -170,7 +170,7 @@ public function slice(int $offset, ?int $length = null): self
/**
* @param iterable<K,V> ...$with
*
* @return self<K,V>
* @return self<V,K>
*/
public function merge(iterable ...$with): self
{
Expand All @@ -182,7 +182,7 @@ public function merge(iterable ...$with): self
/**
* @param null|callable(V,K):bool $specification
*
* @return self<K,V>
* @return self<V,K>
*/
public function filter(mixed $specification = null): self
{
Expand All @@ -198,7 +198,7 @@ public function filter(mixed $specification = null): self
*
* @param callable(V,K):T $function
*
* @return self<array-key,V>
* @return self<V>
*/
public function keyBy(callable $function): self
{
Expand All @@ -218,7 +218,7 @@ public function keyBy(callable $function): self
*
* @param callable(V,K):T $function
*
* @return self<K,T>
* @return self<T,K>
*/
public function map(callable $function): self
{
Expand All @@ -228,7 +228,7 @@ public function map(callable $function): self
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function sort(int|callable $flags = \SORT_REGULAR): self
{
Expand All @@ -239,7 +239,7 @@ public function sort(int|callable $flags = \SORT_REGULAR): self
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function sortDesc(int|callable $flags = \SORT_REGULAR): self
{
Expand All @@ -249,7 +249,7 @@ public function sortDesc(int|callable $flags = \SORT_REGULAR): self
/**
* @param callable(V,K):mixed $function
*
* @return self<K,V>
* @return self<V,K>
*/
public function sortBy(callable $function, int $flags = \SORT_REGULAR): self
{
Expand All @@ -272,15 +272,15 @@ public function sortBy(callable $function, int $flags = \SORT_REGULAR): self
/**
* @param callable(V,K):mixed $function
*
* @return self<K,V>
* @return self<V,K>
*/
public function sortByDesc(callable $function, int $flags = \SORT_REGULAR): self
{
return $this->sortBy($function, $flags)->reverse();
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function sortKeys(int $flags = \SORT_REGULAR): self
{
Expand All @@ -292,7 +292,7 @@ public function sortKeys(int $flags = \SORT_REGULAR): self
}

/**
* @return self<K,V>
* @return self<V,K>
*/
public function sortKeysDesc(int $flags = \SORT_REGULAR): self
{
Expand All @@ -308,15 +308,15 @@ public function sortKeysDesc(int $flags = \SORT_REGULAR): self
*
* @param iterable<array-key,T> $values
*
* @return self<V&array-key,T>
* @return self<T,V&array-key>
*/
public function combine(iterable $values): self
{
return new self(\array_combine($this->source, self::for($values)->source)); // @phpstan-ignore-line
return new self(\array_combine($this->source, self::for($values)->source)); // @phpstan-ignore return.type, argument.type
}

/**
* @return self<V&array-key,V>
* @return self<V,V&array-key>
*/
public function combineWithSelf(): self
{
Expand All @@ -328,7 +328,7 @@ public function combineWithSelf(): self
*
* @param callable(V,K):T $function
*
* @return self<array-key,non-empty-array<int,V>>
* @return self<non-empty-list<V>>
*/
public function groupBy(callable $function): self
{
Expand Down Expand Up @@ -360,27 +360,27 @@ public function get(int|string $key, mixed $default = null): mixed
* @param K $key
* @param V $value
*
* @return self<K,V>
* @return self<V,K>
*/
public function set(int|string $key, mixed $value): self
{
$clone = clone $this;
$clone->source[$key] = $value;
$clone->source[$key] = $value; // @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor

return $clone;
}

/**
* @param K ...$keys
*
* @return self<K,V>
* @return self<V,K>
*/
public function unset(int|string ...$keys): self
{
$clone = clone $this;

foreach ($keys as $key) {
unset($clone->source[$key]);
unset($clone->source[$key]); // @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor
}

return $clone;
Expand All @@ -389,7 +389,7 @@ public function unset(int|string ...$keys): self
/**
* @param K ...$keys
*
* @return self<K,V>
* @return self<V,K>
*/
public function only(int|string ...$keys): self
{
Expand All @@ -399,14 +399,14 @@ public function only(int|string ...$keys): self
/**
* @param V ...$values
*
* @return self<K,V>
* @return self<V,K>
*/
public function push(mixed ...$values): self
{
$clone = clone $this;

foreach ($values as $value) {
$clone->source[] = $value;
$clone->source[] = $value; // @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor
}

return $clone;
Expand Down
8 changes: 4 additions & 4 deletions src/Collection/CallbackCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @template K
* @template V
* @implements Collection<K,V>
* @template K = array-key
* @implements Collection<V,K>
*/
final class CallbackCollection implements Collection
{
/** @use IterableCollection<K,V> */
/** @use IterableCollection<V,K> */
use IterableCollection;

/** @var LazyCollection<K,V> */
/** @var LazyCollection<V,K> */
private LazyCollection $iterator;
private \Closure $count;

Expand Down
Loading
Loading