ToArrayTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 17 3
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib\Traits;
7
8
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
9
10
trait ToArrayTrait
11
{
12
    /**
13
     * Returns an unsafe array built from this iterator
14
     *
15
     * Note that when the iterator contains overlapping keys, that
16
     * these will be *lost* when converting to an array.
17
     *
18
     * A safer option is to use either
19
     * - $iterable->keys(),
20
     * - $iterable->values(), or
21
     * - $iterable->items()
22
     *
23
     * @return array
24
     */
25 45
    public function toArray()
26
    {
27 45
        if ($this instanceof \Traversable) {
28 44
            $array = iterator_to_array($this);
29 44
            array_walk(
30 44
                $array,
31 44
                function (&$value) {
32 44
                    if ($value instanceof FiniteIterableInterface) {
33 4
                        $value = $value->toArray();
34
                    }
35 44
                }
36
            );
37 44
            return $array;
38
        } else {
39 1
            return [];
40
        }
41
    }
42
}
43