Completed
Push — v4.0-dev ( 6660e2...b14a8f )
by
unknown
11s
created

util.php ➔ phpEcho()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 1
cts 2
cp 0.5
crap 1.125
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Util;
4
5
use League\Plates\Exception\ComposeException;
6
7
function id($multi = false) {
8 20
    if ($multi) {
9
        return function(...$args) {
10 4
            return $args;
11 4
        };
12
    } else {
13
        return function($arg) {
14 16
            return $arg;
15 16
        };
16
    }
17
}
18
19
/** simple utility that wraps php echo which allows for stubbing out the
20
    echo func for testing */
21
function phpEcho() {
22
    return function($v) {
23
        echo $v;
24 8
    };
25
}
26
27
/** stack a set of functions into each other and returns the stacked func */
28
function stack(array $funcs) {
29
    return array_reduce(array_reverse($funcs), function($next, $func) {
30
        return function(...$args) use ($next, $func) {
31 28
            $args[] = $next;
32 28
            return $func(...$args);
33 36
        };
34
    }, function() {
35 4
        throw new ComposeException('No handler was able to return a result.');
36 40
    });
37
}
38
39
function stackGroup(array $funcs) {
40 12
    $end_next = null;
41
    $funcs[] = function(...$args) use (&$end_next) {
42 4
        return $end_next(...array_slice($args, 0, -1));
43
    };
44 12
    $next = stack($funcs);
45
    return function(...$args) use ($next, &$end_next) {
46 4
        $end_next = end($args);
47 4
        return $next(...array_slice($args, 0, -1));
48 12
    };
49
}
50
51
function compose(array $funcs, $context_args = []) {
52
    return function($arg) use ($funcs, $context_args) {
53
        return array_reduce($funcs, function($acc, $func) use ($context_args) {
54
            return $func($acc, ...$context_args);
55
        }, $arg);
56
    };
57
}
58
59
function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) {
60
    return array_reduce($parts, function($acc, $part) use ($sep) {
61 16
        if ($acc === null) {
62 16
            return rtrim($part, $sep);
63
        }
64
65 16
        return $acc . $sep . ltrim($part, $sep);
66 16
    }, null);
67
}
68