Allow PHP-8.2 and up Compatibility instead of just PHP-8.4

This commit is contained in:
johnnyq
2026-06-12 17:06:10 -04:00
parent 2204bd52f4
commit d3a93652f3
220 changed files with 7198 additions and 2635 deletions

View File

@@ -77,7 +77,9 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* @param int $from
* @param int $to
* @param int $step
* @return static<int, int>
* @return ($step is zero ? never : static<int, int>)
*
* @throws \InvalidArgumentException
*/
public static function range($from, $to, $step = 1)
{
@@ -303,23 +305,18 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Cross join the given iterables, returning all possible permutations.
*
* @template TCrossJoinKey
* @template TCrossJoinValue
*
* @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$arrays
* @return static<int, array<int, TValue|TCrossJoinValue>>
* {@inheritDoc}
*/
#[\Override]
public function crossJoin(...$arrays)
{
return $this->passthru('crossJoin', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Count the number of items in the collection by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key|\UnitEnum)|string|null $countBy
* @param (callable(TValue, TKey): (array-key|\UnitEnum))|string|null $countBy
* @return static<array-key, int>
*/
public function countBy($countBy = null)
@@ -346,110 +343,84 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Get the items that are not present in the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @return static<TKey, TValue>
* {@inheritDoc}
*/
#[\Override]
public function diff($items)
{
return $this->passthru('diff', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items that are not present in the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffUsing($items, callable $callback)
{
return $this->passthru('diffUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys and values are not present in the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffAssoc($items)
{
return $this->passthru('diffAssoc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys and values are not present in the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @param callable(TKey, TKey): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffAssocUsing($items, callable $callback)
{
return $this->passthru('diffAssocUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys are not present in the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffKeys($items)
{
return $this->passthru('diffKeys', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys are not present in the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
* @param callable(TKey, TKey): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffKeysUsing($items, callable $callback)
{
return $this->passthru('diffKeysUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Retrieve duplicate items.
*
* @template TMapValue
*
* @param (callable(TValue): TMapValue)|string|null $callback
* @param bool $strict
* @return static
* {@inheritDoc}
*/
#[\Override]
public function duplicates($callback = null, $strict = false)
{
return $this->passthru('duplicates', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Retrieve duplicate items using strict comparison.
*
* @template TMapValue
*
* @param (callable(TValue): TMapValue)|string|null $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function duplicatesStrict($callback = null)
{
return $this->passthru('duplicatesStrict', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get all items except for those with the specified keys.
*
* @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys
* @return static
* {@inheritDoc}
*/
#[\Override]
public function except($keys)
{
return $this->passthru('except', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -565,26 +536,31 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Group an associative array by a field or using a callback.
* {@inheritDoc}
*
* @template TGroupKey of array-key
* @template TGroupKey of array-key|\UnitEnum|\Stringable
*
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
* @param bool $preserveKeys
* @return static<($groupBy is string ? array-key : ($groupBy is array ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>>
* @return static<
* ($groupBy is (array|string)
* ? array-key
* : (TGroupKey is \UnitEnum ? array-key : (TGroupKey is \Stringable ? string : TGroupKey))),
* static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>
* >
*/
#[\Override]
public function groupBy($groupBy, $preserveKeys = false)
{
return $this->passthru('groupBy', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Key an associative array by a field or using a callback.
*
* @template TNewKey of array-key
* @template TNewKey of array-key|\UnitEnum
*
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
* @return static<($keyBy is string ? array-key : ($keyBy is array ? array-key : TNewKey)), TValue>
* @return static<($keyBy is (array|string) ? array-key : (TNewKey is \UnitEnum ? array-key : TNewKey)), TValue>
*/
public function keyBy($keyBy)
{
@@ -655,60 +631,48 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Intersect the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersect($items)
{
return $this->passthru('intersect', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Intersect the collection with the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersectUsing($items, callable $callback)
{
return $this->passthru('intersectUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Intersect the collection with the given items with additional index check.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersectAssoc($items)
{
return $this->passthru('intersectAssoc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Intersect the collection with the given items with additional index check, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersectAssocUsing($items, callable $callback)
{
return $this->passthru('intersectAssocUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Intersect the collection with the given items by key.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersectByKeys($items)
{
return $this->passthru('intersectByKeys', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -724,11 +688,26 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Determine if the collection contains a single item.
*
* @param (callable(TValue, TKey): bool)|null $callback
* @return bool
*
* @deprecated 12.49.0 Use the `hasSole()` method instead.
*/
public function containsOneItem()
public function containsOneItem(?callable $callback = null): bool
{
return $this->take(2)->count() === 1;
return $this->hasSole($callback);
}
/**
* Determine if the collection contains multiple items.
*
* @return bool
*
* @deprecated 12.50.0 Use the `hasMany()` method instead.
*/
public function containsManyItems(): bool
{
return $this->hasMany();
}
/**
@@ -831,19 +810,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Run a dictionary map over the items.
*
* The callback should return an associative array with a single key/value pair.
*
* @template TMapToDictionaryKey of array-key
* @template TMapToDictionaryValue
*
* @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback
* @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>>
* {@inheritDoc}
*/
#[\Override]
public function mapToDictionary(callable $callback)
{
return $this->passthru('mapToDictionary', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -867,27 +839,21 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Merge the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function merge($items)
{
return $this->passthru('merge', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Recursively merge the collection with the given items.
*
* @template TMergeRecursiveValue
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items
* @return static<TKey, TValue|TMergeRecursiveValue>
* {@inheritDoc}
*/
#[\Override]
public function mergeRecursive($items)
{
return $this->passthru('mergeRecursive', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -898,7 +864,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*/
public function multiply(int $multiplier)
{
return $this->passthru('multiply', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -935,14 +901,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Union the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function union($items)
{
return $this->passthru('union', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -950,10 +914,16 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*
* @param int $step
* @param int $offset
* @return static
* @return ($step is positive-int ? static : never)
*
* @throws \InvalidArgumentException
*/
public function nth($step, $offset = 0)
{
if ($step < 1) {
throw new InvalidArgumentException('Step value must be at least 1.');
}
return new static(function () use ($step, $offset) {
$position = 0;
@@ -1058,11 +1028,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Get one or a specified number of items randomly from the collection.
*
* @param int|null $number
* @param bool $preserveKeys
* @return static<int, TValue>|TValue
*
* @throws \InvalidArgumentException
*/
public function random($number = null)
public function random($number = null, $preserveKeys = false)
{
$result = $this->collect()->random(...func_get_args());
@@ -1097,24 +1068,21 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Recursively replace the collection items with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function replaceRecursive($items)
{
return $this->passthru('replaceRecursive', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Reverse items order.
*
* @return static<TKey, TValue>
* {@inheritDoc}
*/
#[\Override]
public function reverse()
{
return $this->passthru('reverse', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -1203,24 +1171,31 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Shuffle the items in the collection.
*
* @return static<TKey, TValue>
* {@inheritDoc}
*/
#[\Override]
public function shuffle()
{
return $this->passthru('shuffle', []);
return $this->passthru(__FUNCTION__, []);
}
/**
* Create chunks representing a "sliding window" view of the items in the collection.
*
* @param int $size
* @param int $step
* @param positive-int $size
* @param positive-int $step
* @return static<int, static>
*
* @throws \InvalidArgumentException
*/
public function sliding($size = 2, $step = 1)
{
if ($size < 1) {
throw new InvalidArgumentException('Size value must be at least 1.');
} elseif ($step < 1) {
throw new InvalidArgumentException('Step value must be at least 1.');
}
return new static(function () use ($size, $step) {
$iterator = $this->getIterator();
@@ -1313,16 +1288,13 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Get a slice of items from the enumerable.
*
* @param int $offset
* @param int|null $length
* @return static
* {@inheritDoc}
*/
#[\Override]
public function slice($offset, $length = null)
{
if ($offset < 0 || $length < 0) {
return $this->passthru('slice', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
$instance = $this->skip($offset);
@@ -1331,20 +1303,24 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Split a collection into a certain number of groups.
* {@inheritDoc}
*
* @param int $numberOfGroups
* @return static<int, static>
* @throws \InvalidArgumentException
*/
#[\Override]
public function split($numberOfGroups)
{
return $this->passthru('split', func_get_args());
if ($numberOfGroups < 1) {
throw new InvalidArgumentException('Number of groups must be at least 1.');
}
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
*
* @param (callable(TValue, TKey): bool)|string $key
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
* @return TValue
@@ -1366,10 +1342,31 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
->sole();
}
/**
* Determine if the collection contains a single item or a single item matching the given criteria.
*
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function hasSole($key = null, $operator = null, $value = null): bool
{
$filter = func_num_args() > 1
? $this->operatorForWhere(...func_get_args())
: $key;
return $this
->unless($filter == null)
->filter($filter)
->take(2)
->count() === 1;
}
/**
* Get the first item in the collection but throw an exception if no matching items exist.
*
* @param (callable(TValue, TKey): bool)|string $key
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
* @return TValue
@@ -1439,10 +1436,16 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Split a collection into a certain number of groups, and fill the first groups completely.
*
* @param int $numberOfGroups
* @return static<int, static>
* @return ($numberOfGroups is positive-int ? static<int, static> : never)
*
* @throws \InvalidArgumentException
*/
public function splitIn($numberOfGroups)
{
if ($numberOfGroups < 1) {
throw new InvalidArgumentException('Number of groups must be at least 1.');
}
return $this->chunk((int) ceil($this->count() / $numberOfGroups));
}
@@ -1484,84 +1487,66 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Sort through each item with a callback.
*
* @param (callable(TValue, TValue): int)|null|int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sort($callback = null)
{
return $this->passthru('sort', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort items in descending order.
*
* @param int $options
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortDesc($options = SORT_REGULAR)
{
return $this->passthru('sortDesc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection using the given callback.
*
* @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @param bool $descending
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
{
return $this->passthru('sortBy', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection in descending order using the given callback.
*
* @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->passthru('sortByDesc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection keys.
*
* @param int $options
* @param bool $descending
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortKeys($options = SORT_REGULAR, $descending = false)
{
return $this->passthru('sortKeys', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection keys in descending order.
*
* @param int $options
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortKeysDesc($options = SORT_REGULAR)
{
return $this->passthru('sortKeysDesc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection keys using a callback.
*
* @param callable(TKey, TKey): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortKeysUsing(callable $callback)
{
return $this->passthru('sortKeysUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -1630,17 +1615,22 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Take items in the collection until a given point in time.
* Take items in the collection until a given point in time, with an optional callback on timeout.
*
* @param \DateTimeInterface $timeout
* @param callable(TValue|null, TKey|null): mixed|null $callback
* @return static<TKey, TValue>
*/
public function takeUntilTimeout(DateTimeInterface $timeout)
public function takeUntilTimeout(DateTimeInterface $timeout, ?callable $callback = null)
{
$timeout = $timeout->getTimestamp();
return new static(function () use ($timeout) {
return new static(function () use ($timeout, $callback) {
if ($this->now() >= $timeout) {
if ($callback) {
$callback(null, null);
}
return;
}
@@ -1648,6 +1638,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
yield $key => $value;
if ($this->now() >= $timeout) {
if ($callback) {
$callback($value, $key);
}
break;
}
}
@@ -1710,21 +1704,21 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Flatten a multi-dimensional associative array with dots.
*
* @param int $depth
* @return static
*/
public function dot()
public function dot($depth = INF)
{
return $this->passthru('dot', []);
return $this->passthru(__FUNCTION__, [$depth]);
}
/**
* Convert a flatten "dot" notation array into an expanded array.
*
* @return static
* {@inheritDoc}
*/
#[\Override]
public function undot()
{
return $this->passthru('undot', []);
return $this->passthru(__FUNCTION__, []);
}
/**
@@ -1830,18 +1824,13 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Pad collection to the specified length with a value.
*
* @template TPadValue
*
* @param int $size
* @param TPadValue $value
* @return static<int, TValue|TPadValue>
* {@inheritDoc}
*/
#[\Override]
public function pad($size, $value)
{
if ($size < 0) {
return $this->passthru('pad', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
return new static(function () use ($size, $value) {