Completed
Push — master ( 2e5c62...f0f58a )
by Sérgio
06:30
created

hold.php ➔ hold()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 2
eloc 11
c 3
b 0
f 2
nc 1
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 2
rs 9.4285
1
<?php
2
3
namespace Sergiors\Functional;
4
5
const hold = '\Sergiors\Functional\hold';
6
7
/**
8
 * @author Sérgio Rafael Siqueira <[email protected]>
9
 *
10
 * @param callable $fn
11
 *
12
 * @return mixed
13
 */
14
function hold(callable $fn /* ...$args */)
15
{
16
    $placeholder = pipe(filter(function ($x) {
17 2
        return _ === $x;
18 2
    }))->pipe('array_keys');
19
20 2
    $args = array_slice(func_get_args(), 1);
21 2
    $ks = $placeholder($args);
22
23
    return function ($x) use ($fn, $args, $ks) {
24 2
        if ([] === $ks) {
25 2
            return call_user_func_array($fn, array_merge($args, [$x]));
26
        }
27
28 2
        return call_user_func_array($fn, array_replace($args, [
29 2
            $ks[0] => $x
30 2
        ]));
31 2
    };
32
}
33