Completed
Push — master ( 8857af...ec0102 )
by Sérgio
04:30
created

curry.php ➔ Sergiors\Functional\curry()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2
Metric Value
cc 2
eloc 9
nc 1
nop 1
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
1
<?php
2
3
namespace Sergiors\Functional;
4
5
/**
6
 * @author Sérgio Rafael Siqueira <[email protected]>
7
 *
8
 * @param \Closure|string $fn
9
 *
10
 * @return mixed
11
 */
12
function curry($fn)
13
{
14 21
    $args = tail(func_get_args());
15 21
    $params = (new \ReflectionFunction($fn))->getNumberOfRequiredParameters();
16
17
    return function () use ($fn, $args, $params) {
18 21
        $args = array_merge($args, func_get_args());
19
20 21
        if ($params > count($args)) {
21 12
            array_unshift($args, $fn);
22
23 12
            return call_user_func_array(__NAMESPACE__.'\curry', $args);
24
        }
25
26 21
        return call_user_func_array($fn, $args);
27 21
    };
28
}
29