Completed
Pull Request — master (#108)
by Marco
04:12
created

Changes::mergeWithIterator()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility;
6
7
use ArrayIterator;
8
use Countable;
9
use Generator;
10
use IteratorAggregate;
11
use function count;
12
13
final class Changes implements IteratorAggregate, Countable
14
{
15
    /** @var Change[] */
16
    private $changes;
17
18
    /** Generator|null */
19
    private $generator;
20
21
    private function __construct()
22
    {
23
    }
24
25
    public static function empty() : self
26
    {
27
        static $empty;
28
29
        if ($empty) {
30
            return $empty;
31
        }
32
33
        $empty = new self();
34
35
        $empty->changes = [];
36
37
        return $empty;
38
    }
39
40
    /** @param Change[] $changes */
41
    public static function fromIterator(iterable $changes) : self
42
    {
43
        $instance = new self();
44
45
        $instance->changes   = [];
46
        $instance->generator = (function () use ($changes) : Generator {
47
            foreach ($changes as $change) {
48
                yield $change;
49
            }
50
        })();
51
52
        return $instance;
53
    }
54
55
    /** @param Change[] $changes */
56
    public function mergeWithIterator(iterable $changes) : self
57
    {
58
        $instance = new self();
59
60
        $instance->changes   = [];
61
        $instance->generator = (function () use ($changes) : Generator {
62
            foreach ($this as $change) {
63
                yield $change;
64
            }
65
66
            foreach ($changes as $change) {
67
                yield $change;
68
            }
69
        })();
70
71
        return $instance;
72
    }
73
74
    public static function fromList(Change ...$changes) : self
75
    {
76
        $instance = new self();
77
78
        $instance->changes = $changes;
79
80
        return $instance;
81
    }
82
83
    public function mergeWith(self $other) : self
84
    {
85
        return $this->mergeWithIterator($other);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     *
91
     * @return ArrayIterator|Change[]
92
     */
93
    public function getIterator() : iterable
94
    {
95
        foreach ($this->changes as $change) {
96
            yield $change;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $change returns the type Generator which is incompatible with the documented return type Roave\BackwardCompatibil...\Change[]&ArrayIterator.
Loading history...
97
        }
98
99
        foreach ($this->generator ?? [] as $change) {
100
            $this->changes[] = $change;
101
102
            yield $change;
103
        }
104
105
        $this->generator = null;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function count() : int
112
    {
113
        return count(iterator_to_array($this));
114
    }
115
}
116