DifferencesIterator::next()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 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 SN\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|null */
31
    private $fDifference;
32
33
    /**
34
     * Default values.
35
     *
36
     * @param RangeDifference[] $differenceRanges
37
     */
38 16
    public function __construct(array $differenceRanges)
39
    {
40 16
        $this->fArray = $differenceRanges;
41 16
        $this->fIndex = 0;
42 16
        $this->fRange = [];
43
44 16
        if ($this->fIndex < \count($this->fArray)) {
45 12
            $this->fDifference = $this->fArray[$this->fIndex++];
46
        } else {
47 5
            $this->fDifference = null;
48
        }
49 16
    }
50
51
    /**
52
     * @return int
53
     */
54 12
    public function getCount(): int
55
    {
56 12
        return \count($this->fRange);
57
    }
58
59
    /**
60
     * @return RangeDifference|null
61
     */
62 9
    public function getDifference(): ?RangeDifference
63
    {
64 9
        return $this->fDifference;
65
    }
66
67
    /**
68
     * @return int
69
     */
70 3
    public function getIndex(): int
71
    {
72 3
        return $this->fIndex;
73
    }
74
75
    /**
76
     * @return array
77
     */
78 7
    public function getRange(): array
79
    {
80 7
        return $this->fRange;
81
    }
82
83
    /**
84
     * Appends the edit to its list and moves to the next RangeDifference.
85
     */
86 11
    public function next(): void
87
    {
88 11
        $this->fRange[] = $this->fDifference;
89
90 11
        if (null !== $this->fDifference) {
91 9
            if ($this->fIndex < \count($this->fArray)) {
92 3
                $this->fDifference = $this->fArray[$this->fIndex++];
93
            } else {
94 9
                $this->fDifference = null;
95
            }
96
        }
97 11
    }
98
99
    /**
100
     * @param DifferencesIterator $right
101
     * @param DifferencesIterator $left
102
     * @return DifferencesIterator
103
     */
104 7
    public function other(DifferencesIterator $right, DifferencesIterator $left): DifferencesIterator
105
    {
106 7
        if ($this === $right) {
107 7
            return $left;
108
        }
109
110 6
        return $right;
111
    }
112
113
    /**
114
     * Removes all RangeDifferences.
115
     */
116 8
    public function removeAll(): void
117
    {
118 8
        $this->fRange = [];
119 8
    }
120
}
121
122