Completed
Pull Request — master (#7)
by Steve
04:16
created

DifferencesIterator::getIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace DaisyDiff\RangeDifferencer;
12
13
/**
14
 * A custom iterator to iterate over a List of RangeDifferences.
15
 * 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|null
61
     */
62
    public function getDifference(): ?RangeDifference
63
    {
64
        return $this->fDifference;
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getIndex(): int
71
    {
72
        return $this->fIndex;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getRange(): array
79
    {
80
        return $this->fRange;
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
     * @param DifferencesIterator $right
101
     * @param DifferencesIterator $left
102
     * @return DifferencesIterator
103
     */
104
    public function other(DifferencesIterator $right, DifferencesIterator $left): DifferencesIterator
105
    {
106
        if ($this === $right) {
107
            return $left;
108
        }
109
110
        return $right;
111
    }
112
113
    /**
114
     * @return void
115
     */
116
    public function removeAll(): void
117
    {
118
        $this->fRange = [];
119
    }
120
}
121
122