Completed
Push — master ( 503969...73f5bc )
by Sérgio
08:08
created

hold.php ➔ hold()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

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