Completed
Push — master ( b03e32...44190a )
by Sérgio
03:19
created

each.php ➔ Sergiors\Functional\each()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace Sergiors\Functional;
4
5
/**
6
 * @author Sérgio Rafael Siqueira <[email protected]>
7
 *
8
 * @return mixed
9
 */
10
function each()
11
{
12 3
    $args = func_get_args();
13
14
    $each = function (\Closure $fn, $ls) {
15 3
        if ($ls instanceof \Iterator) {
16
            iterator_apply($ls, function () use ($fn, $ls) {
17
                $args = [
18 1
                    $ls->current(),
19 1
                    $ls->key()
20 1
                ];
21
22 1
                call_user_func_array($fn, $args);
23 1
                return true;
24 1
            });
25
26 1
            return $ls;
27
        }
28
29 2
        array_walk($ls, $fn);
30 2
        return $ls;
31 3
    };
32
33 3
    return call_user_func_array(curry($each), $args);
34
}
35