DifferencesIterator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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