diff --git a/docs/pages/code/gamma.php b/docs/pages/code/gamma.php index 29a35c210..20d058ed5 100644 --- a/docs/pages/code/gamma.php +++ b/docs/pages/code/gamma.php @@ -6,9 +6,7 @@ use loophp\collection\Collection; -$addition = static function (float $value1, float $value2): float { - return $value1 + $value2; -}; +$addition = static fn (float $value1, float $value2): float => $value1 + $value2; $listInt = static function (int $init, callable $succ): Generator { yield $init; @@ -18,19 +16,11 @@ } }; -$N = $listInt(1, static function (int $n): int { - return $n + 1; -}); +$N = $listInt(1, static fn (int $n): int => $n + 1); -$Y = static function (float $n): Closure { - return static function (int $x) use ($n): float { - return ($x ** ($n - 1)) * (\M_E ** (-$x)); - }; -}; +$Y = static fn (float $n): Closure => static fn (int $x): float => ($x ** ($n - 1)) * (\M_E ** (-$x)); -$e = static function (float $value): bool { - return 10 ** -12 > $value; -}; +$e = static fn (float $value): bool => 10 ** -12 > $value; // Find the factorial of this number. This is not bounded to integers! // $number = 3; // 2 * 2 => 4 diff --git a/docs/pages/code/monte-carlo.php b/docs/pages/code/monte-carlo.php index 8750b3538..d38bea691 100644 --- a/docs/pages/code/monte-carlo.php +++ b/docs/pages/code/monte-carlo.php @@ -7,8 +7,8 @@ use loophp\collection\Collection; $monteCarloMethod = static function ($in = 0, $total = 1): array { - $randomNumber1 = mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(); - $randomNumber2 = mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(); + $randomNumber1 = random_int(0, mt_getrandmax() - 1) / mt_getrandmax(); + $randomNumber2 = random_int(0, mt_getrandmax() - 1) / mt_getrandmax(); if (1 >= (($randomNumber1 ** 2) + ($randomNumber2 ** 2))) { ++$in; diff --git a/docs/pages/code/operations/associate.php b/docs/pages/code/operations/associate.php index d01a67f40..e0436aaaf 100644 --- a/docs/pages/code/operations/associate.php +++ b/docs/pages/code/operations/associate.php @@ -13,12 +13,8 @@ Collection::fromIterable($input) ->associate( - static function ($key, $value) { - return $key * 2; - }, - static function ($value, $key) { - return $value * 3; - } + static fn ($key, $value) => $key * 2, + static fn ($value, $key) => $value * 3 ); // [ @@ -34,9 +30,7 @@ static function ($value, $key) { Collection::fromIterable($input) ->associate( - static function ($key, $value) { - return $key * 2; - } + static fn ($key, $value) => $key * 2 ); // [ @@ -53,9 +47,7 @@ static function ($key, $value) { Collection::fromIterable($input) ->associate( null, - static function ($value, $key) { - return $value * 3; - } + static fn ($value, $key) => $value * 3 ); // [ diff --git a/docs/pages/code/operations/distinct.php b/docs/pages/code/operations/distinct.php index c9b82ff5a..d20b83cd7 100644 --- a/docs/pages/code/operations/distinct.php +++ b/docs/pages/code/operations/distinct.php @@ -16,11 +16,8 @@ // Example 2 -> Using a custom comparator callback, with object values final class User { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function name(): string @@ -44,11 +41,8 @@ public function name(): string // Example 3 -> Using a custom accessor callback, with object values final class Person { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function name(): string @@ -73,11 +67,8 @@ public function name(): string // Example 4 -> Using both accessor and comparator callbacks, with object values final class Cat { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function name(): string diff --git a/docs/pages/code/operations/duplicate.php b/docs/pages/code/operations/duplicate.php index 0aa0d33cf..c7e23ac7b 100644 --- a/docs/pages/code/operations/duplicate.php +++ b/docs/pages/code/operations/duplicate.php @@ -16,11 +16,8 @@ // Example 2 -> Using a custom comparator callback, with object values final class User { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function name(): string @@ -44,11 +41,8 @@ public function name(): string // Example 3 -> Using a custom accessor callback, with object values final class Person { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function name(): string @@ -73,11 +67,8 @@ public function name(): string // Example 4 -> Using both accessor and comparator callbacks, with object values final class Cat { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function name(): string diff --git a/docs/pages/code/operations/since.php b/docs/pages/code/operations/since.php index 15e51ee93..6eadd4403 100644 --- a/docs/pages/code/operations/since.php +++ b/docs/pages/code/operations/since.php @@ -18,15 +18,15 @@ // Implode characters to create a line string ->map(static fn (array $characters): string => implode('', $characters)) // Skip items until the string "require-dev" is found. - ->since(static fn ($line): bool => false !== strpos($line, 'require-dev')) + ->since(static fn ($line): bool => str_contains($line, 'require-dev')) // Skip items after the string "}" is found. - ->until(static fn ($line): bool => false !== strpos($line, '}')) + ->until(static fn ($line): bool => str_contains($line, '}')) // Re-index the keys ->normalize() // Filter out the first line and the last line. ->filter( static fn ($line, $index): bool => 0 !== $index, - static fn ($line): bool => false === strpos($line, '}') + static fn ($line): bool => !str_contains($line, '}') ) // Trim remaining results and explode the string on ':'. ->map( diff --git a/docs/pages/code/operations/squash.php b/docs/pages/code/operations/squash.php index 2ac274dcf..bfb8eab6a 100644 --- a/docs/pages/code/operations/squash.php +++ b/docs/pages/code/operations/squash.php @@ -26,7 +26,5 @@ static function (string $doc): string { // If no exception, you can continue the processing... $results = $results ->filter( - static function (string $document): bool { - return false !== strpos($document, 'foobar'); - } + static fn (string $document): bool => str_contains($document, 'foobar') ); diff --git a/docs/pages/code/parse-git-log.php b/docs/pages/code/parse-git-log.php index 2264d7114..a9a82349b 100644 --- a/docs/pages/code/parse-git-log.php +++ b/docs/pages/code/parse-git-log.php @@ -19,30 +19,24 @@ fclose($fh); }; -$buildIfThenElseCallbacks = static function (string $lineStart): array { - return [ - static function ($line) use ($lineStart): bool { - return is_string($line) && 0 === mb_strpos($line, $lineStart); - }, - static function ($line) use ($lineStart): array { - [, $line] = explode($lineStart, $line); +$buildIfThenElseCallbacks = static fn (string $lineStart): array => [ + static fn ($line): bool => is_string($line) && 0 === mb_strpos($line, $lineStart), + static function ($line) use ($lineStart): array { + [, $line] = explode($lineStart, $line); - return [ - sprintf( - '%s:%s', - mb_strtolower(str_replace(':', '', $lineStart)), - trim($line) - ), - ]; - }, - ]; -}; + return [ + sprintf( + '%s:%s', + mb_strtolower(str_replace(':', '', $lineStart)), + trim($line) + ), + ]; + }, +]; $c = Collection::fromIterable($commandStream('git log')) ->map( - static function (string $value): string { - return trim($value); - } + static fn (string $value): string => trim($value) ) ->compact('', ' ', "\n") ->ifThenElse(...$buildIfThenElseCallbacks('commit')) @@ -52,73 +46,55 @@ static function (string $value): string { ->ifThenElse(...$buildIfThenElseCallbacks('Signed-off-by:')) ->split( Splitable::BEFORE, - static function ($value): bool { - return is_array($value) ? - (1 === preg_match('/^commit:\b[0-9a-f]{5,40}\b/', $value[0])) : - false; - } + static fn ($value): bool => is_array($value) ? + (1 === preg_match('/^commit:\b[0-9a-f]{5,40}\b/', $value[0])) : + false ) ->map( - static function (array $value): CollectionInterface { - return Collection::fromIterable($value); - } + static fn (array $value): CollectionInterface => Collection::fromIterable($value) ) ->map( - static function (CollectionInterface $collection): CollectionInterface { - return $collection - ->groupBy( - static function ($value): ?string { - return is_array($value) ? 'headers' : null; - } - ) - ->groupBy( - static function ($value): ?string { - return is_string($value) ? 'log' : null; - } - ) - ->ifThenElse( - static function ($value, $key): bool { - return 'headers' === $key; - }, - static function ($value, $key): array { - return Collection::fromIterable($value) - ->unwrap() - ->associate( - static function ($carry, $key, string $value): string { - [$key, $line] = explode(':', $value, 2); + static fn (CollectionInterface $collection): CollectionInterface => $collection + ->groupBy( + static fn ($value): ?string => is_array($value) ? 'headers' : null + ) + ->groupBy( + static fn ($value): ?string => is_string($value) ? 'log' : null + ) + ->ifThenElse( + static fn ($value, $key): bool => 'headers' === $key, + static fn ($value, $key): array => Collection::fromIterable($value) + ->unwrap() + ->associate( + static function ($carry, $key, string $value): string { + [$key, $line] = explode(':', $value, 2); - return $key; - }, - static function ($carry, $key, string $value): string { - [$key, $line] = explode(':', $value, 2); + return $key; + }, + static function ($carry, $key, string $value): string { + [$key, $line] = explode(':', $value, 2); - return trim($line); - } - ) - ->all(); - } - ); - } + return trim($line); + } + ) + ->all() + ) ) ->map( - static function (CollectionInterface $collection): CollectionInterface { - return $collection - ->flatten() - ->groupBy( - static function ($value, $key): ?string { - if (is_numeric($key)) { - return 'log'; - } - - return null; + static fn (CollectionInterface $collection): CollectionInterface => $collection + ->flatten() + ->groupBy( + static function ($value, $key): ?string { + if (is_numeric($key)) { + return 'log'; } - ); - } + + return null; + } + ) ) ->map( - static function (CollectionInterface $collection): array { - return $collection->all(); - } + static fn (CollectionInterface $collection): array => $collection->all() ) ->limit(100); diff --git a/docs/pages/code/primes.php b/docs/pages/code/primes.php index 4a3455e95..1fc098ef4 100644 --- a/docs/pages/code/primes.php +++ b/docs/pages/code/primes.php @@ -11,9 +11,7 @@ $iterator = new \CallbackFilterIterator( $iterator, - static function (int $a) use ($primeNumber): bool { - return 0 !== $a % $primeNumber; - } + static fn (int $a): bool => 0 !== $a % $primeNumber ); $iterator->next(); @@ -29,14 +27,12 @@ static function (int $a) use ($primeNumber): bool { return yield from $integerGenerator($succ($init), $succ); }; -$limit = 1000000; +$limit = 1_000_000; $primes = $primesGenerator( $integerGenerator( 2, - static function (int $n): int { - return $n + 1; - } + static fn (int $n): int => $n + 1 ) ); @@ -45,9 +41,7 @@ static function (int $n): int { $primesGenerator( $integerGenerator( 2, - static function (int $n): int { - return $n + 1; - } + static fn (int $n): int => $n + 1 ) ) ); @@ -62,9 +56,7 @@ static function (int $n): int { $primesGenerator( $integerGenerator( 2, - static function (int $n): int { - return $n + 1; - } + static fn (int $n): int => $n + 1 ) ) ); @@ -73,9 +65,7 @@ static function (int $n): int { $lazyTwinPrimeNumbersCollection = Collection::fromIterable($lazyPrimeNumbersCollection) ->zip($lazyPrimeNumbersCollection->tail()) ->filter( - static function (array $chunk): bool { - return 2 === $chunk[1] - $chunk[0]; - } + static fn (array $chunk): bool => 2 === $chunk[1] - $chunk[0] ); foreach ($lazyTwinPrimeNumbersCollection->limit($limit) as $prime) { diff --git a/docs/pages/code/random-generator.php b/docs/pages/code/random-generator.php index 815e2a2cc..4f0f5f6a3 100644 --- a/docs/pages/code/random-generator.php +++ b/docs/pages/code/random-generator.php @@ -7,7 +7,7 @@ use loophp\collection\Collection; // Generate 300 distinct random numbers between 0 and 1000 -$random = static fn (): array => [mt_rand() / mt_getrandmax()]; +$random = static fn (): array => [random_int(0, mt_getrandmax()) / mt_getrandmax()]; $random_numbers = Collection::unfold($random) ->unwrap() diff --git a/docs/pages/code/random-numbers-distribution.php b/docs/pages/code/random-numbers-distribution.php index 356370e61..384e2b686 100644 --- a/docs/pages/code/random-numbers-distribution.php +++ b/docs/pages/code/random-numbers-distribution.php @@ -28,14 +28,10 @@ static function (int $value) use ($max, $groups): string { } ) ->groupBy( - static function ($value, $key) { - return $value; - } + static fn ($value, $key) => $value ) ->map( - static function (array $value): int { - return count($value); - } + static fn (array $value): int => count($value) ) ->sort( Sortable::BY_KEYS, diff --git a/docs/pages/code/serialization.php b/docs/pages/code/serialization.php index e8aef123c..157da5995 100644 --- a/docs/pages/code/serialization.php +++ b/docs/pages/code/serialization.php @@ -9,30 +9,30 @@ // Example 1 -> using `json_encode` // a) with list, ordered keys -json_encode(Collection::fromIterable([1, 2, 3])); // JSON: '[1, 2, 3]' +json_encode(Collection::fromIterable([1, 2, 3]), \JSON_THROW_ON_ERROR); // JSON: '[1, 2, 3]' // b) with list, missing keys $col = Collection::fromIterable([1, 2, 3]) ->filter(static fn (int $val): bool => $val % 2 !== 0); // [0 => 1, 2 => 3] -json_encode($col); // JSON: '{"0": 1, "2": 3}' +json_encode($col, \JSON_THROW_ON_ERROR); // JSON: '{"0": 1, "2": 3}' // c) with list, missing keys, with `normalize` $col = Collection::fromIterable([1, 2, 3]) ->filter(static fn (int $val): bool => $val % 2 !== 0) ->normalize(); // [0 => 1, 1 => 3] -json_encode($col); // JSON: '[1, 3]' +json_encode($col, \JSON_THROW_ON_ERROR); // JSON: '[1, 3]' // d) with associative array -json_encode(Collection::fromIterable(['foo' => 1, 'bar' => 2])); // JSON: '{"foo": 1, "bar": 2}' +json_encode(Collection::fromIterable(['foo' => 1, 'bar' => 2]), \JSON_THROW_ON_ERROR); // JSON: '{"foo": 1, "bar": 2}' // e) with associative array, with `normalize` $col = Collection::fromIterable(['foo' => 1, 'bar' => 2]) ->normalize(); // [0 => 1, 1 => 2] -json_encode($col); // JSON: '[1, 2]' +json_encode($col, \JSON_THROW_ON_ERROR); // JSON: '[1, 2]' // Example 2 -> using custom serializer (all previous behaviours apply) diff --git a/docs/pages/code/simple.php b/docs/pages/code/simple.php index 6f00fe20b..1f877d5e0 100644 --- a/docs/pages/code/simple.php +++ b/docs/pages/code/simple.php @@ -40,9 +40,7 @@ // Map data $collection ->map( - static function ($value, $key): string { - return sprintf('%s.%s', $value, $value); - } + static fn ($value, $key): string => sprintf('%s.%s', $value, $value) ) ->all(); // ['A.A', 'B.B', 'C.C', 'D.D', 'E.E'] @@ -104,7 +102,7 @@ static function ($value, $key): bool { ); // Generate 300 distinct random numbers between 0 and 1000 -$random = static fn (): array => [mt_rand() / mt_getrandmax()]; +$random = static fn (): array => [random_int(0, mt_getrandmax()) / mt_getrandmax()]; Collection::unfold($random) ->unwrap() @@ -146,15 +144,11 @@ static function ($value, $key): bool { Collection::fromIterable($readFileLineByLine($hugeFile)) // Add the line number at the end of the line, as comment. ->map( - static function ($value, $key): string { - return str_replace(\PHP_EOL, ' // line ' . $key . \PHP_EOL, $value); - } + static fn ($value, $key): string => str_replace(\PHP_EOL, ' // line ' . $key . \PHP_EOL, $value) ) // Find public static fields or methods among the results. ->filter( - static function ($value, $key): bool { - return false !== mb_strpos(trim($value), 'public static'); - } + static fn ($value, $key): bool => false !== mb_strpos(trim($value), 'public static') ) // Drop the first result. ->drop(1)