Completed
Pull Request — master (#7)
by Steve
03:26
created

RangeDifference::getLeftLength()   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
 * 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
    public function __construct(
81
        int $kind = 0,
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
        $this->kind           = $kind;
90
        $this->rightStart     = $rightStart;
91
        $this->rightLength    = $rightLength;
92
        $this->leftStart      = $leftStart;
93
        $this->leftLength     = $leftLength;
94
        $this->ancestorStart  = $ancestorStart;
95
        $this->ancestorLength = $ancestorLength;
96
    }
97
98
    /**
99
     * Returns the kind of difference.
100
     *
101
     * @return int
102
     */
103
    public function getKind(): int
104
    {
105
        return $this->kind;
106
    }
107
108
    /**
109
     * @param int $kind
110
     * @return self
111
     */
112
    public function setKind(int $kind): self
113
    {
114
        $this->kind = $kind;
115
        return $this;
116
    }
117
118
    /**
119
     * Returns the start index of the entity range on the ancestor side.
120
     *
121
     * @return int
122
     */
123
    public function getAncestorStart(): int
124
    {
125
        return $this->ancestorStart;
126
    }
127
128
    /**
129
     * @param int $ancestorStart
130
     * @return self
131
     */
132
    public function setAncestorStart(int $ancestorStart): self
133
    {
134
        $this->ancestorStart = $ancestorStart;
135
        return $this;
136
    }
137
138
    /**
139
     * Returns the number of entities on the ancestor side.
140
     *
141
     * @return int
142
     */
143
    public function getAncestorLength(): int
144
    {
145
        return $this->ancestorLength;
146
    }
147
148
    /**
149
     * @param int $ancestorLength
150
     * @return self
151
     */
152
    public function setAncestorLength(int $ancestorLength): self
153
    {
154
        $this->ancestorLength = $ancestorLength;
155
        return $this;
156
    }
157
158
    /**
159
     * Returns the end index of the entity range on the ancestor side.
160
     *
161
     * @return int
162
     */
163
    public function getAncestorEnd(): int
164
    {
165
        return $this->ancestorStart + $this->ancestorLength;
166
    }
167
168
    /**
169
     * Returns the start index of the entity range on the right side.
170
     *
171
     * @return int
172
     */
173
    public function getRightStart(): int
174
    {
175
        return $this->rightStart;
176
    }
177
178
    /**
179
     * @param int $rightStart
180
     * @return self
181
     */
182
    public function setRightStart(int $rightStart): self
183
    {
184
        $this->rightStart = $rightStart;
185
        return $this;
186
    }
187
188
    /**
189
     * Returns the number of entities on the right side.
190
     *
191
     * @return int
192
     */
193
    public function getRightLength(): int
194
    {
195
        return $this->rightLength;
196
    }
197
198
    /**
199
     * @param int $rightLength
200
     * @return self
201
     */
202
    public function setRightLength(int $rightLength): self
203
    {
204
        $this->rightLength = $rightLength;
205
        return $this;
206
    }
207
208
    /**
209
     * Returns the end index of the entity range on the right side.
210
     *
211
     * @return int
212
     */
213
    public function getRightEnd(): int
214
    {
215
        return $this->rightStart + $this->rightLength;
216
    }
217
218
    /**
219
     * Returns the start index of the entity range on the left side.
220
     *
221
     * @return int
222
     */
223
    public function getLeftStart(): int
224
    {
225
        return $this->leftStart;
226
    }
227
228
    /**
229
     * @param int $leftStart
230
     * @return self
231
     */
232
    public function setLeftStart(int $leftStart): self
233
    {
234
        $this->leftStart = $leftStart;
235
        return $this;
236
    }
237
238
    /**
239
     * Returns the number of entities on the left side.
240
     *
241
     * @return int
242
     */
243
    public function getLeftLength(): int
244
    {
245
        return $this->leftLength;
246
    }
247
248
    /**
249
     * @param int $leftLength
250
     * @return self
251
     */
252
    public function setLeftLength(int $leftLength): self
253
    {
254
        $this->leftLength = $leftLength;
255
        return $this;
256
    }
257
258
    /**
259
     * Returns the end index of the entity range on the left side.
260
     *
261
     * @return int
262
     */
263
    public function getLeftEnd(): int
264
    {
265
        return $this->leftStart + $this->leftLength;
266
    }
267
268
    /**
269
     * Returns the maximum number of entities in the left, right, and ancestor sides of this range.
270
     *
271
     * @return int
272
     */
273
    public function getMaxLength(): int
274
    {
275
        return \max($this->rightLength, $this->leftLength, $this->ancestorLength);
276
    }
277
278
    /**
279
     * @param RangeDifference $other
280
     * @return bool
281
     */
282
    public function equals(RangeDifference $other): bool
283
    {
284
        return
285
            $this->kind           === $other->getKind() &&
286
            $this->leftStart      === $other->getLeftStart() &&
287
            $this->leftLength     === $other->getLeftLength() &&
288
            $this->rightStart     === $other->getRightStart() &&
289
            $this->rightLength    === $other->getRightLength() &&
290
            $this->ancestorStart  === $other->getAncestorStart() &&
291
            $this->ancestorLength === $other->getAncestorLength();
292
    }
293
294
    /**
295
     * @return string
296
     */
297
    public function __toString(): string
298
    {
299
        $str = sprintf('Left: (%d, %d) Right: (%d, %d)',
300
            $this->getLeftStart(), $this->getLeftLength(),
301
            $this->getRightStart(), $this->getRightLength());
302
303
        if ($this->ancestorLength > 0 || $this->ancestorStart > 0) {
304
            $str .= sprintf(' Ancestor: (%d, %d)', $this->getAncestorStart(), $this->getAncestorLength());
305
        }
306
307
        return $str;
308
    }
309
}
310