34 lines
941 B
PHP
34 lines
941 B
PHP
<?php
|
|
/**
|
|
* Copyright 2026 Kacper Donat
|
|
*
|
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
|
*
|
|
* 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;
|
|
}
|