Completed
Push — v4.0-dev ( 3b7e85...e5c633 )
by
unknown
15s
created

util.php ➔ joinPath()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 9.6666
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 12
    };
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 32
            $args[] = $next;
32 32
            return $func(...$args);
33 40
        };
34
    }, function() {
35 4
        throw new ComposeException('No handler was able to return a result.');
36 44
    });
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 joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) {
52
    return array_reduce($parts, function($acc, $part) use ($sep) {
53 16
        if ($acc === null) {
54 16
            return rtrim($part, $sep);
55
        }
56
57 16
        return $acc . $sep . ltrim($part, $sep);
58 16
    }, null);
59
}
60