for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace loophp\fpt;
use Closure;
use Generator;
use ReflectionFunction;
use function count;
* @psalm-immutable
*
* phpcs:disable Generic.Files.LineLength.TooLong
final class Curry
{
* @template T
* @psalm-pure
public static function of(): Closure
return
* @psalm-param positive-int|null $arity
static fn (callable $callable, ?int $arity = null): Closure =>
* @psalm-param mixed ...$args
* @return mixed
static fn (...$args): mixed => self::curryN(
($arity ?? (new ReflectionFunction($callable))->getNumberOfRequiredParameters()) - count($args),
$callable,
...self::getArguments([], $args)
);
}
private static function curryN(int $numberOfArguments, callable $function, ...$args): mixed
return (0 >= $numberOfArguments)
? $function(...$args)
:
* @psalm-param mixed ...$argsNext
static fn (...$argsNext): mixed => self::curryN($numberOfArguments - count($argsNext), $function, ...self::getArguments($args, $argsNext));
* @psalm-param list<mixed> $args
* @psalm-param list<mixed> $argsNext
* @psalm-return Generator<int, mixed>
private static function getArguments(array $args = [], array $argsNext = []): Generator
return yield from array_merge($args, $argsNext);