GeneratorCollection::merge()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 1
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Pitchart\Collection;
4
5
use Pitchart\Collection\Mixin\CallableUnifierTrait;
6
7
/**
8
 * A collection using generators to perform transformations
9
 *
10
 * @author Julien VITTE <[email protected]>
11
 */
12
class GeneratorCollection extends \IteratorIterator implements CollectionInterface
13
{
14
15
    use CallableUnifierTrait;
16
17
    /**
18
     * Builder for GeneratorCollection objects
19
     *
20
     * @param  iterable $iterable
21
     *
22
     * @return self
23
     */
24 21 View Code Duplication
    public static function from($iterable)
25
    {
26 21
        if (is_array($iterable)
27
            || $iterable instanceof \IteratorAggregate
28 21
        ) {
29 16
            return new static(new \ArrayIterator($iterable));
30
        }
31 6
        if ($iterable instanceof \Iterator) {
32 1
            return new static($iterable);
33
        }
34
35 5
        throw new \InvalidArgumentException(
36 5
            sprintf(
37 5
                'Argument 1 must be an instance of Traversable or an array, %s given',
38 5
                is_object($iterable) ? get_class($iterable) : gettype($iterable)
39 5
            )
40 5
        );
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 20
    public function toArray()
47
    {
48 20
        return iterator_to_array($this->getInnerIterator());
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 6
    public function values()
55
    {
56 6
        return array_values($this->toArray());
57
    }
58
59
    /**
60
     * @return Collection
61
     */
62 1
    public function persist($class = Collection::class)
63
    {
64 1
        return new $class($this->toArray());
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 6 View Code Duplication
    public function map(callable $callable)
71
    {
72 6
        $function = $this->normalizeAsCallables($callable);
73
        $mapping = function ($iterator) use ($function) {
74 6
            foreach ($iterator as $key => $item) {
75 4
                yield $key => $function($item);
76 6
            }
77 6
        };
78 6
        return new static($mapping($this->getInnerIterator()));
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 5 View Code Duplication
    public function filter(callable $callable)
85
    {
86 5
        $function = $this->normalizeAsCallables($callable);
87
        $filter = function ($iterator) use ($function) {
88 5
            foreach ($iterator as $key => $item) {
89 4
                if ($function($item)) {
90 4
                    yield $item;
91 4
                }
92 5
            }
93 5
        };
94 5
        return new static($filter($this->getInnerIterator()));
95
    }
96
97
    /**
98
     * Alias for filter()
99
     *
100
     * @param callable $callable
101
     *
102
     * @return GeneratorCollection
103
     */
104 3
    public function select(callable $callable)
105
    {
106 3
        return $this->filter($callable);
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 4 View Code Duplication
    public function reject(callable $callable)
113
    {
114 4
        $function = $this->normalizeAsCallables($callable);
115
        $rejection = function ($iterator) use ($function) {
116 4
            foreach ($iterator as $key => $item) {
117 3
                if (!$function($item)) {
118 3
                    yield $item;
119 3
                }
120 4
            }
121 4
        };
122 4
        return new static($rejection($this->getInnerIterator()));
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128
    public function merge(...$collections)
129
    {
130 3
        $merging = function (...$iterators) {
131 3
            foreach ($iterators as $iterator) {
132 3
                foreach ($iterator as $item) {
133 3
                    yield $item;
134 3
                }
135 3
            }
136 3
        };
137 3
        return new static($merging($this->getInnerIterator(), ...$collections));
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143 4 View Code Duplication
    public function reduce(callable $callable, $initial)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145 4
        $accumulator = $initial;
146 4
        $function = $this->normalizeAsCallables($callable);
147
148 4
        foreach ($this->getInnerIterator() as $item) {
149 3
            $accumulator = $function($accumulator, $item);
150 4
        }
151
152 4
        return $accumulator;
153
    }
154
}
155