Skip to content

Commit

Permalink
refactor: PHP 7.4 upgrade.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
  • Loading branch information
drupol committed Oct 27, 2020
1 parent f84063d commit ef8e8da
Show file tree
Hide file tree
Showing 96 changed files with 1,143 additions and 1,549 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": ">= 7.1.3"
"php": ">= 7.4"
},
"require-dev": {
"drupol/php-conventions": "^1.7.4 || ^1.8.19 || ^2",
Expand Down
22 changes: 11 additions & 11 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,13 +1077,13 @@ public function it_can_group(): void
->group()
->shouldIterateAs([
0 => [0 => 'M'],
1 => [1 => 'i'],
2 => [2 => 's', 3 => 's'],
4 => [4 => 'i'],
5 => [5 => 's', 6 => 's'],
7 => [7 => 'i'],
8 => [8 => 'p', 9 => 'p'],
10 => [10 => 'i'],
1 => [0 => 'i'],
2 => [0 => 's', 1 => 's'],
4 => [0 => 'i'],
5 => [0 => 's', 1 => 's'],
7 => [0 => 'i'],
8 => [0 => 'p', 1 => 'p'],
10 => [0 => 'i'],
]);
}

Expand Down Expand Up @@ -2227,10 +2227,10 @@ public function it_can_tails(): void
->tails()
->shouldIterateAs([
['A', 'B', 'C', 'D', 'E'],
[1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E'],
[2 => 'C', 3 => 'D', 4 => 'E'],
[3 => 'D', 4 => 'E'],
[4 => 'E'],
[0 => 'B', 1 => 'C', 2 => 'D', 3 => 'E'],
[0 => 'C', 1 => 'D', 2 => 'E'],
[0 => 'D', 1 => 'E'],
[0 => 'E'],
[],
]);
}
Expand Down
60 changes: 22 additions & 38 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,22 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*
* @implements \loophp\collection\Contract\Collection<TKey, T>
*/
final class Collection implements CollectionInterface
{
/**
* @var mixed[]
* @var array<int, mixed>
* @psalm-var list<mixed>
*/
private $parameters;
private array $parameters;

/**
* @var Closure
* @psalm-var callable(...mixed): Generator<TKey, T>
*/
private $source;
private Closure $source;

/**
* @param callable|Closure $callable
Expand All @@ -151,7 +152,7 @@ public function __construct(callable $callable, ...$parameters)

public function all(): array
{
return iterator_to_array($this);
return iterator_to_array($this->getIterator());
}

public function append(...$items): CollectionInterface
Expand Down Expand Up @@ -181,12 +182,10 @@ public function associate(
*
* @psalm-return TKey|T
*/
static function ($carry, $key, $value) {
return $carry;
};
static fn ($carry, $key, $value) => $carry;

$callbackForKeys = $callbackForKeys ?? $defaultCallback;
$callbackForValues = $callbackForValues ?? $defaultCallback;
$callbackForKeys ??= $defaultCallback;
$callbackForValues ??= $defaultCallback;

return $this->pipe(Associate::of()($callbackForKeys)($callbackForValues));
}
Expand Down Expand Up @@ -346,7 +345,7 @@ public function frequency(): CollectionInterface
return $this->pipe(Frequency::of());
}

public static function fromCallable(callable $callable, ...$parameters): CollectionInterface
public static function fromCallable(callable $callable, ...$parameters): self
{
return new self(
/**
Expand All @@ -355,40 +354,34 @@ public static function fromCallable(callable $callable, ...$parameters): Collect
*
* @psalm-return Generator<TKey, T>
*/
static function (callable $callable, array $parameters): Generator {
return yield from new ClosureIterator($callable, ...$parameters);
},
static fn (callable $callable, array $parameters): Generator => yield from new ClosureIterator($callable, ...$parameters),
$callable,
$parameters
);
}

public static function fromFile(string $filepath): CollectionInterface
public static function fromFile(string $filepath): self
{
return new self(
static function (string $filepath): Generator {
return yield from new ResourceIterator(fopen($filepath, 'rb'));
},
static fn (string $filepath): Generator => yield from new ResourceIterator(fopen($filepath, 'rb')),
$filepath
);
}

public static function fromIterable(iterable $iterable): CollectionInterface
public static function fromIterable(iterable $iterable): self
{
return new self(
/**
* @psalm-param iterable<TKey, T> $data
*
* @psalm-return Generator<TKey, T>
*/
static function (iterable $iterable): Generator {
return yield from new IterableIterator($iterable);
},
static fn (iterable $iterable): Generator => yield from new IterableIterator($iterable),
$iterable
);
}

public static function fromResource($resource): CollectionInterface
public static function fromResource($resource): self
{
return new self(
/**
Expand All @@ -397,24 +390,20 @@ public static function fromResource($resource): CollectionInterface
*
* @psalm-return Generator<int, string>
*/
static function ($resource): Generator {
return yield from new ResourceIterator($resource);
},
static fn ($resource): Generator => yield from new ResourceIterator($resource),
$resource
);
}

public static function fromString(string $string, string $delimiter = ''): CollectionInterface
public static function fromString(string $string, string $delimiter = ''): self
{
return new self(
/**
* @psalm-param list<string> $parameters
*
* @psalm-return Generator<int, string>
*/
static function (string $string, string $delimiter): Generator {
return yield from new StringIterator($string, $delimiter);
},
static fn (string $string, string $delimiter): Generator => yield from new StringIterator($string, $delimiter),
$string,
$delimiter
);
Expand Down Expand Up @@ -452,19 +441,14 @@ public function head(): CollectionInterface

public function ifThenElse(callable $condition, callable $then, ?callable $else = null): CollectionInterface
{
$else = $else ??
$else ??=
/**
* @psalm-param T $value
* @psalm-param TKey $key
*
* @psalm-return T
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key) {
return $value;
};
static fn ($value, $key) => $value;

return $this->pipe(IfThenElse::of()($condition)($then)($else));
}
Expand Down Expand Up @@ -577,7 +561,7 @@ public function permutate(): CollectionInterface
return $this->pipe(Permutate::of());
}

public function pipe(callable ...$callables): CollectionInterface
public function pipe(callable ...$callables): self
{
return self::fromCallable(
Pipe::of()(...$callables),
Expand Down
26 changes: 6 additions & 20 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,26 +316,19 @@ interface Collection extends
* @psalm-template NewTKey
* @psalm-template NewTKey of array-key
* @psalm-template NewT
*
* @psalm-return \loophp\collection\Contract\Collection<NewTKey, NewT>
*/
public static function empty(): Collection;
public static function empty(): self;

/**
* @psalm-template NewTKey
* @psalm-template NewTKey of array-key
* @psalm-template NewT
*
* @param mixed ...$parameters
*
* @psalm-return \loophp\collection\Contract\Collection<NewTKey, NewT>
*/
public static function fromCallable(callable $callable, ...$parameters): Collection;
public static function fromCallable(callable $callable, ...$parameters): self;

/**
* @psalm-return \loophp\collection\Contract\Collection<int, string>
*/
public static function fromFile(string $filepath): Collection;
public static function fromFile(string $filepath): self;

/**
* @psalm-template NewTKey
Expand All @@ -344,22 +337,15 @@ public static function fromFile(string $filepath): Collection;
*
* @param iterable<mixed> $iterable
* @psalm-param iterable<NewTKey, NewT> $iterable
*
* @psalm-return \loophp\collection\Contract\Collection<NewTKey, NewT>
*/
public static function fromIterable(iterable $iterable): Collection;
public static function fromIterable(iterable $iterable): self;

/**
* @param resource $resource
*
* @psalm-return \loophp\collection\Contract\Collection<int, string>
*/
public static function fromResource($resource): Collection;
public static function fromResource($resource): self;

/**
* @psalm-return \loophp\collection\Contract\Collection<int, string>
*/
public static function fromString(string $string, string $delimiter = ''): Collection;
public static function fromString(string $string, string $delimiter = ''): self;

/**
* @psalm-return \loophp\collection\Iterator\ClosureIterator<TKey, T>
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Operation/Applyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Applyable
* Execute a callback for each element of the collection.
*
* @param callable ...$callables
* @psalm-param callable(TKey, T):(bool) ...$callables
* @psalm-param callable(TKey, T):bool ...$callables
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Operation/Associateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
interface Associateable
{
/**
* @psalm-param null|callable(TKey, T):(TKey) $callbackForKeys
* @psalm-param null|callable(TKey, T):(T) $callbackForValues
* @psalm-param null|callable(TKey, T):TKey $callbackForKeys
* @psalm-param null|callable(TKey, T):T $callbackForValues
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Operation/Keyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface Keyable
{
/**
* @psalm-return TKey
* @psalm-return T
*/
public function key(int $index = 0);
}
2 changes: 0 additions & 2 deletions src/Contract/Operation/Rangeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interface Rangeable
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Sinceable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Sinceable
{
/**
* @param callable ...$callbacks
* @psalm-param callable(T, TKey):(bool) ...$callbacks
* @psalm-param callable(T, TKey):bool ...$callbacks
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Timesable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interface Timesable
* @psalm-template T
*
* @psalm-param null|callable(): T $callback
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public static function times(int $number = 0, ?callable $callback = null): Collection;
}
2 changes: 0 additions & 2 deletions src/Contract/Operation/Unfoldable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ interface Unfoldable
*
* @psalm-param callable(T...): array<TKey, T> $callback
* @psalm-param T ...$parameters
*
* @psalm-return \loophp\collection\Contract\Collection<int, T>
*/
public static function unfold(callable $callback, ...$parameters): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Untilable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Untilable
{
/**
* @param callable ...$callbacks
* @psalm-param callable(T, TKey):(bool) ...$callbacks
* @psalm-param callable(T, TKey):bool ...$callbacks
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
Expand Down
10 changes: 2 additions & 8 deletions src/Iterator/CacheIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@
*/
final class CacheIterator extends ProxyIterator
{
/**
* @var CacheItemPoolInterface
*/
private $cache;
private CacheItemPoolInterface $cache;

/**
* @var int
*/
private $key;
private int $key;

/**
* @psalm-param Iterator<TKey, T> $iterator
Expand Down
12 changes: 5 additions & 7 deletions src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ final class ClosureIterator extends ProxyIterator
* @var array<int, mixed>
* @psalm-var list<mixed>
*/
private $arguments;
private array $arguments;

/**
* @var callable
* @psalm-var callable(mixed...):(Generator<TKey, T>)
* @psalm-var callable(mixed ...):Generator<TKey, T>
*/
private $callable;

/**
* @var Closure
* @psalm-var Closure(callable(mixed...):Generator<TKey, T>, list<T>):(Generator<TKey, T>)
* @psalm-var Closure(callable(mixed ...): Generator<TKey, T>, list<T>):Generator<TKey, T>
*/
private $generator;

/**
* @param mixed ...$arguments
* @psalm-param mixed ...$arguments
* @psalm-param callable(mixed...):(Generator<TKey, T>) $callable
* @psalm-param callable(mixed ...):Generator<TKey, T> $callable
*/
public function __construct(callable $callable, ...$arguments)
{
Expand All @@ -47,9 +47,7 @@ public function __construct(callable $callable, ...$arguments)
* @psalm-param callable(T...):Generator<TKey, T> $callable
* @psalm-param list<T> $arguments
*/
static function (callable $callable, array $arguments): Generator {
return yield from ($callable)(...$arguments);
};
static fn (callable $callable, array $arguments): Generator => yield from ($callable)(...$arguments);

$this->iterator = $this->getGenerator();
}
Expand Down

0 comments on commit ef8e8da

Please sign in to comment.