Completed
Push — master ( 96cbf3...9bcdfc )
by Steve
03:03 queued 01:23
created

DifferencesIterator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 9 3
A getRange() 0 3 1
A getDifference() 0 3 1
A removeAll() 0 3 1
A other() 0 9 2
A getIndex() 0 3 1
A __construct() 0 10 2
A getCount() 0 3 1
1
<?php
2
3
/**
4
 * (c) Steve Nebes <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace SN\RangeDifferencer;
13
14
/**
15
 * A custom iterator to iterate over a List of RangeDifferences. It is used internally by the RangeDifferencer.
16
 *
17
 * @internal
18
 */
19
class DifferencesIterator
20
{
21
    /** @var RangeDifference[] */
22
    private $fRange = [];
23
24
    /** @var int */
25
    private $fIndex = 0;
26
27
    /** @var RangeDifference[] */
28
    private $fArray = [];
29
30
    /** @var RangeDifference */
31
    private $fDifference;
32
33
    /**
34
     * Default values.
35
     *
36
     * @param RangeDifference[] $differenceRanges
37
     */
38
    public function __construct(array $differenceRanges)
39
    {
40
        $this->fArray = $differenceRanges;
41
        $this->fIndex = 0;
42
        $this->fRange = [];
43
44
        if ($this->fIndex < \count($this->fArray)) {
45
            $this->fDifference = $this->fArray[$this->fIndex++];
46
        } else {
47
            $this->fDifference = null;
48
        }
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getCount(): int
55
    {
56
        return \count($this->fRange);
57
    }
58
59
    /**
60
     * @return RangeDifference[]
61
     */
62
    public function getRange(): iterable
63
    {
64
        return $this->fRange;
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getIndex(): int
71
    {
72
        return $this->fIndex;
73
    }
74
75
    /**
76
     * @return RangeDifference|null
77
     */
78
    public function getDifference(): ?RangeDifference
79
    {
80
        return $this->fDifference;
81
    }
82
83
    /**
84
     * Appends the edit to its list and moves to the next RangeDifference.
85
     */
86
    public function next(): void
87
    {
88
        $this->fRange[] = $this->fDifference;
89
90
        if (null !== $this->fDifference) {
91
            if ($this->fIndex < \count($this->fArray)) {
92
                $this->fDifference = $this->fArray[$this->fIndex++];
93
            } else {
94
                $this->fDifference = null;
95
            }
96
        }
97
    }
98
99
    /**
100
     * Difference iterators are used in pairs. This method returns the other iterator.
101
     *
102
     * @param DifferencesIterator $right
103
     * @param DifferencesIterator $left
104
     * @return DifferencesIterator
105
     */
106
    public function other(
107
        DifferencesIterator $right,
108
        DifferencesIterator $left
109
    ): DifferencesIterator {
110
        if ($this === $right) {
111
            return $left;
112
        }
113
114
        return $right;
115
    }
116
117
    /**
118
     * Removes all RangeDifference.
119
     */
120
    public function removeAll(): void
121
    {
122
        $this->fRange = [];
123
    }
124
}
125