From eac27c9e63a79697c956018c0d39964ef1a3d2a0 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Tue, 27 Oct 2026 23:00:38 +0100 Subject: [PATCH] move applyWithArity to internal namespace --- composer.json | 1 + phpstan-baseline.neon | 12 ------------ src/Internal/functions.php | 39 +++++++++++++++++++++++++++++++++++++ src/Utils/FixedFunction.php | 2 +- src/Utils/index.php | 26 ++----------------------- 5 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 src/Internal/functions.php diff --git a/composer.json b/composer.json index eea8789..0d2d1b2 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ }, "files": [ "./src/functions.php", + "./src/Internal/functions.php", "./src/Predicates/index.php", "./src/Collections/Internal/functions.php", "./src/Collections/Internal/containers.php", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 34a5bfd..aacbef3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -498,18 +498,6 @@ parameters: count: 1 path: src/Utils/index.php - - - message: '#^Function Kadet\\Functional\\applyWithArity\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Utils/index.php - - - - message: '#^Function Kadet\\Functional\\applyWithArity\(\) has parameter \$args with no value type specified in iterable type array\.$#' - identifier: missingType.iterableValue - count: 1 - path: src/Utils/index.php - - message: '#^Function Kadet\\Functional\\curry\(\) has parameter \$args with no type specified\.$#' identifier: missingType.parameter diff --git a/src/Internal/functions.php b/src/Internal/functions.php new file mode 100644 index 0000000..125ed5c --- /dev/null +++ b/src/Internal/functions.php @@ -0,0 +1,39 @@ + + * + * Full license available in separate LICENSE file + */ + +namespace Kadet\Functional\Internal; + +use function Kadet\Functional\apply; +use function Kadet\Functional\partial; + +/** + * The argument dispatch of apply(), with the arity of $callable already known: it calls $call when there are enough + * arguments and partially applies $callable otherwise. + * + * @internal + * + * @param array $args + */ +function applyWithArity(callable $callable, callable $call, bool $variadic, int $required, int $all, array $args): mixed +{ + switch (true) { + case $variadic: + return partial($callable, ...$args); + + case count($args) >= $all: + $applied = $call(...array_slice($args, 0, $all)); + return is_callable($applied) ? apply($applied, ...array_slice($args, $all)) : $applied; + + case count($args) >= $required: + return $call(...$args); + + default: + return partial($callable, ...$args); + } +} diff --git a/src/Utils/FixedFunction.php b/src/Utils/FixedFunction.php index 8eddfed..b3356d6 100644 --- a/src/Utils/FixedFunction.php +++ b/src/Utils/FixedFunction.php @@ -11,7 +11,7 @@ namespace Kadet\Functional\Utils; use function Kadet\Functional\apply; -use function Kadet\Functional\applyWithArity; +use function Kadet\Functional\Internal\applyWithArity; use Kadet\Functional\Compilable; use function Kadet\Functional\compile; use Kadet\Functional\Decorator; diff --git a/src/Utils/index.php b/src/Utils/index.php index 5ade686..b04ee1a 100644 --- a/src/Utils/index.php +++ b/src/Utils/index.php @@ -15,6 +15,8 @@ use Kadet\Functional\Utils\FunctionPipeline; use Kadet\Functional\Utils\MemoizedFunction; use Kadet\Functional\Utils\PartiallyAppliedFunction; +use function Kadet\Functional\Internal\applyWithArity; + function partial(callable $callable, ...$args): PartiallyAppliedFunction { return new PartiallyAppliedFunction($callable, ...$args); @@ -39,30 +41,6 @@ function apply(callable $callable, ...$args) } } -/** - * The argument dispatch of apply(), with the arity of $callable already known: it calls $call when there are enough - * arguments and partially applies $callable otherwise. - * - * @internal - */ -function applyWithArity(callable $callable, callable $call, bool $variadic, int $required, int $all, array $args) -{ - switch (true) { - case $variadic: - return partial($callable, ...$args); - - case count($args) >= $all: - $applied = $call(...array_slice($args, 0, $all)); - return is_callable($applied) ? apply($applied, ...array_slice($args, $all)) : $applied; - - case count($args) >= $required: - return $call(...$args); - - default: - return partial($callable, ...$args); - } -} - function curry(callable $callable, ?int $threshold = null, ...$args): CurriedFunction { return new CurriedFunction($callable, $threshold, ...$args);