Completed
Push — master ( 2fe9d7...6a07f9 )
by Florian
01:49 queued 11s
created

FlatMap::flatMap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Cocur\Chain\Link;
4
5
use Cocur\Chain\Chain;
6
7
/**
8
 * FlatMap.
9
 *
10
 * @author    Nicolas Reynis
11
 */
12
trait FlatMap
13
{
14
    /**
15
     * @return Chain
16
     */
17 4
    public function flatMap(callable $callback)
18
    {
19 4
        $flattened = [];
20 4
        foreach ($this->array as $index => $element) {
0 ignored issues
show
Bug introduced by
The property array does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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