Passed
Push — master ( bef855...6fcd14 )
by Sérgio
07:04
created

cond.php ➔ cond()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 20
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Prelude;
6
7
const cond = __NAMESPACE__.'\cond';
8
9
/**
10
 * @see http://elixir-lang.org/getting-started/case-cond-and-if.html#cond
11
 */
12
function cond(array $pairs): \Closure
13
{
14
    return function ($x) use ($pairs) {
15 3
        $pair = head($pairs);
16
17
        $lazyfn = function ($x) use ($pairs) {
18 2
            $xs = tail($pairs);
19 2
            return cond($xs)($x);
20 3
        };
21
22
        $throw = function() {
23 1
            throw new \InvalidArgumentException();
24 3
        };
25
26 3
        return ifElse($pair[0] ?? $throw())
27 3
            ($pair[1] ?? $throw())
28
            ($lazyfn)
29
            ($x);
30 3
    };
31
}
32