* * Full license available in separate LICENSE file */ namespace Kadet\Functional\Collections\Internal; use function Kadet\Functional\compile; /** * Compiles a collection callback, adapted to its arity. * * The returned closure is called with ($value, $key). Internal functions (including first-class callables like * `trim(...)`) and non-variadic closures declaring exactly one parameter receive only the value, so the key is * never mistaken for an optional argument. * * @internal */ function callback(callable $callable): \Closure { $compiled = compile($callable); $reflection = new \ReflectionFunction($compiled); if ($reflection->isInternal() || (!$reflection->isVariadic() && $reflection->getNumberOfParameters() === 1)) { return static fn ($value) => $compiled($value); } return $compiled; }