Passed
Push — master ( e1804d...801b1b )
by Sérgio
05:26 queued 02:17
created

cond.php ➔ cond()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 1
dl 0
loc 20
ccs 8
cts 9
cp 0.8889
crap 2.0054
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)
13
{
14
    return function ($x) use ($pairs) {
15 2
        $pair = head($pairs);
16
17 2
        if (!isset($pair[1])) {
18
            throw new \InvalidArgumentException();
19
        }
20
21
        $lazyfn = function ($x) use ($pairs) {
22 2
            $xs = tail($pairs);
23 2
            return cond($xs)($x);
24 2
        };
25
26 2
        return ifElse($pair[0])
27 2
            ($pair[1])
28
            ($lazyfn)
29
            ($x);
30 2
    };
31
}
32