FlatMap::flatMap()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
namespace Cocur\Chain\Link;
4
5
/**
6
 * FlatMap.
7
 *
8
 * @author    Nicolas Reynis
9
 */
10
trait FlatMap
11
{
12
    /**
13
     * @param callable $callback
14
     *
15
     * @return self
16
     */
17 4
    public function flatMap(callable $callback): self
18
    {
19 4
        $flattened = [];
20 4
        foreach ($this->array as $index => $element) {
21 3
            $transformation = $callback($element, $index);
22 3
            if (is_array($transformation)) {
23 3
                array_push($flattened, ...$transformation);
24
            } else {
25 1
                $flattened[] = $transformation;
26
            }
27
        }
28 4
        $this->array = $flattened;
29
30 4
        return $this;
31
    }
32
}
33