RangeDifference::getAncestorStart()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
 * Description of a change between two or three ranges of comparable entities.
15
 *
16
 * RangeDifference objects are the elements of a compare result returned from the RangeDifferencer find* methods.
17
 * Clients use these objects as they are returned from the differencer. This class is not intended to be instantiated
18
 * outside of the Compare framework.
19
 *
20
 * Note: A range in the RangeDifference object is given as a start index and length in terms of comparable entities.
21
 * However, these entity indices and counts are not necessarily character positions. For example, if an entity
22
 * represents a line in a document, the start index would be a line number and the count would be in lines.
23
 */
24
class RangeDifference
25
{
26
    /** Two-way change constant indicating no change. */
27
    const NOCHANGE = 0;
28
29
    /** Two-way change constant indicating two-way change (same as RIGHT) */
30
    const CHANGE = 2;
31
32
    /** Three-way change constant indicating a change in both right and left. */
33
    const CONFLICT = 1;
34
35
    /** Three-way change constant indicating a change in right. */
36
    const RIGHT = 2;
37
38
    /** Three-way change constant indicating a change in left. */
39
    const LEFT = 3;
40
41
    /**
42
     * Three-way change constant indicating the same change in both right and left, that is only the ancestor is
43
     * different.
44
     */
45
    const ANCESTOR = 4;
46
47
    /** Constant indicating an unknown change kind. */
48
    const ERROR = 5;
49
50
    /** @var int */
51
    protected $kind = 0;
52
53
    /** @var int */
54
    protected $leftStart = 0;
55
56
    /** @var int */
57
    protected $leftLength = 0;
58
59
    /** @var int */
60
    protected $rightStart = 0;
61
62
    /** @var int */
63
    protected $rightLength = 0;
64
65
    /** @var int */
66
    protected $ancestorStart = 0;
67
68
    /** @var int */
69
    protected $ancestorLength = 0;
70
71
    /**
72
     * @param int $kind
73
     * @param int $rightStart
74
     * @param int $rightLength
75
     * @param int $leftStart
76
     * @param int $leftLength
77
     * @param int $ancestorStart
78
     * @param int $ancestorLength
79
     */
80 91
    public function __construct(
81
        int $kind = self::NOCHANGE,
82
        int $rightStart = 0,
83
        int $rightLength = 0,
84
        int $leftStart = 0,
85
        int $leftLength = 0,
86
        int $ancestorStart = 0,
87
        int $ancestorLength = 0
88
    ) {
89 91
        $this->kind = $kind;
90 91
        $this->rightStart = $rightStart;
91 91
        $this->rightLength = $rightLength;
92 91
        $this->leftStart = $leftStart;
93 91
        $this->leftLength = $leftLength;
94 91
        $this->ancestorStart = $ancestorStart;
95 91
        $this->ancestorLength = $ancestorLength;
96 91
    }
97
98
    /**
99
     * Returns the kind of difference.
100
     *
101
     * @return int
102
     */
103 68
    public function getKind(): int
104
    {
105 68
        return $this->kind;
106
    }
107
108
    /**
109
     * @param int $kind
110
     * @return self
111
     *
112
     * @codeCoverageIgnore
113
     */
114
    public function setKind(int $kind): self
115
    {
116
        $this->kind = $kind;
117
        return $this;
118
    }
119
120
    /**
121
     * Returns the start index of the entity range on the ancestor side.
122
     *
123
     * @return int
124
     */
125 37
    public function getAncestorStart(): int
126
    {
127 37
        return $this->ancestorStart;
128
    }
129
130
    /**
131
     * @param int $ancestorStart
132
     * @return self
133
     *
134
     * @codeCoverageIgnore
135
     */
136
    public function setAncestorStart(int $ancestorStart): self
137
    {
138
        $this->ancestorStart = $ancestorStart;
139
        return $this;
140
    }
141
142
    /**
143
     * Returns the number of entities on the ancestor side.
144
     *
145
     * @return int
146
     */
147 6
    public function getAncestorLength(): int
148
    {
149 6
        return $this->ancestorLength;
150
    }
151
152
    /**
153
     * @param int $ancestorLength
154
     * @return self
155
     *
156
     * @codeCoverageIgnore
157
     */
158
    public function setAncestorLength(int $ancestorLength): self
159
    {
160
        $this->ancestorLength = $ancestorLength;
161
        return $this;
162
    }
163
164
    /**
165
     * Returns the end index of the entity range on the ancestor side.
166
     *
167
     * @return int
168
     */
169 33
    public function getAncestorEnd(): int
170
    {
171 33
        return $this->ancestorStart + $this->ancestorLength;
172
    }
173
174
    /**
175
     * Returns the start index of the entity range on the right side.
176
     *
177
     * @return int
178
     */
179 78
    public function getRightStart(): int
180
    {
181 78
        return $this->rightStart;
182
    }
183
184
    /**
185
     * @param int $rightStart
186
     * @return self
187
     */
188 63
    public function setRightStart(int $rightStart): self
189
    {
190 63
        $this->rightStart = $rightStart;
191 63
        return $this;
192
    }
193
194
    /**
195
     * Returns the number of entities on the right side.
196
     *
197
     * @return int
198
     */
199 72
    public function getRightLength(): int
200
    {
201 72
        return $this->rightLength;
202
    }
203
204
    /**
205
     * @param int $rightLength
206
     * @return self
207
     */
208 50
    public function setRightLength(int $rightLength): self
209
    {
210 50
        $this->rightLength = $rightLength;
211 50
        return $this;
212
    }
213
214
    /**
215
     * Returns the end index of the entity range on the right side.
216
     *
217
     * @return int
218
     */
219 55
    public function getRightEnd(): int
220
    {
221 55
        return $this->rightStart + $this->rightLength;
222
    }
223
224
    /**
225
     * Returns the start index of the entity range on the left side.
226
     *
227
     * @return int
228
     */
229 78
    public function getLeftStart(): int
230
    {
231 78
        return $this->leftStart;
232
    }
233
234
    /**
235
     * @param int $leftStart
236
     * @return self
237
     */
238 63
    public function setLeftStart(int $leftStart): self
239
    {
240 63
        $this->leftStart = $leftStart;
241 63
        return $this;
242
    }
243
244
    /**
245
     * Returns the number of entities on the left side.
246
     *
247
     * @return int
248
     */
249 73
    public function getLeftLength(): int
250
    {
251 73
        return $this->leftLength;
252
    }
253
254
    /**
255
     * @param int $leftLength
256
     * @return self
257
     */
258 63
    public function setLeftLength(int $leftLength): self
259
    {
260 63
        $this->leftLength = $leftLength;
261 63
        return $this;
262
    }
263
264
    /**
265
     * Returns the end index of the entity range on the left side.
266
     *
267
     * @return int
268
     */
269 55
    public function getLeftEnd(): int
270
    {
271 55
        return $this->leftStart + $this->leftLength;
272
    }
273
274
    /**
275
     * Returns the maximum number of entities in the left, right, and ancestor sides of this range.
276
     *
277
     * @return int
278
     */
279 5
    public function getMaxLength(): int
280
    {
281 5
        return (int) \max($this->rightLength, $this->leftLength, $this->ancestorLength);
282
    }
283
284
    /**
285
     * @param RangeDifference $other
286
     * @return bool
287
     */
288 1
    public function equals(RangeDifference $other): bool
289
    {
290
        return
291 1
            $this->kind === $other->getKind() &&
292 1
            $this->leftStart === $other->getLeftStart() &&
293 1
            $this->leftLength === $other->getLeftLength() &&
294 1
            $this->rightStart === $other->getRightStart() &&
295 1
            $this->rightLength === $other->getRightLength() &&
296 1
            $this->ancestorStart === $other->getAncestorStart() &&
297 1
            $this->ancestorLength === $other->getAncestorLength();
298
    }
299
300
    /**
301
     * @return string
302
     */
303 18
    public function __toString(): string
304
    {
305 18
        $str = \sprintf('Left: (%d, %d) Right: (%d, %d)',
306 18
            $this->getLeftStart(), $this->getLeftLength(),
307 18
            $this->getRightStart(), $this->getRightLength());
308
309 18
        if ($this->ancestorLength > 0 || $this->ancestorStart > 0) {
310 3
            $str .= \sprintf(' Ancestor: (%d, %d)', $this->getAncestorStart(), $this->getAncestorLength());
311
        }
312
313 18
        return $str;
314
    }
315
}
316