Repository navigation
Expand file tree
/
Copy pathTextDiffOptions.php
More file actions
238 lines (190 loc) · 5.34 KB
/
Copy pathTextDiffOptions.php
File metadata and controls
238 lines (190 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
declare(strict_types=1);
namespace Pharaonic\Diff;
use InvalidArgumentException;
/**
* Immutable options for Diff::text()
*
* Every method returns new options and leaves the current ones unchanged.
*/
final class TextDiffOptions
{
private int $context = 3;
private bool $pairModifiedLines = true;
private float $similarityThreshold = 0.55;
private string $inline = InlineMode::WORD;
private bool $ignoreWhitespaceChanges = false;
private bool $ignoreTrailingWhitespace = false;
private int $maxLines = 20_000;
private int $maxInputBytes = 4_194_304;
private int $maxInlineLength = 1_000;
private function __construct() {}
public static function create(): self
{
return new self();
}
/**
* How many unchanged lines surround each change in a hunk (3 by default)
*/
public function context(int $lines): self
{
self::atLeast('context', $lines, 0);
$options = clone $this;
$options->context = $lines;
return $options;
}
/**
* Whether removed and added lines that look alike are paired as modified (on by default)
*/
public function pairModifiedLines(bool $pair = true): self
{
$options = clone $this;
$options->pairModifiedLines = $pair;
return $options;
}
/**
* How similar, from 0 to 1, a removed and an added line must be to pair them (0.55 by default)
*/
public function similarityThreshold(float $threshold): self
{
if (! ($threshold >= 0.0 && $threshold <= 1.0)) {
throw new InvalidArgumentException('The similarity threshold must be between 0 and 1.');
}
$options = clone $this;
$options->similarityThreshold = $threshold;
return $options;
}
/**
* How changes inside a modified line are highlighted: an InlineMode constant (word by default)
*/
public function inline(string $mode): self
{
if (! in_array($mode, [InlineMode::NONE, InlineMode::WORD, InlineMode::CHARACTER], true)) {
throw new InvalidArgumentException(sprintf('Unknown inline mode "%s"; use an InlineMode constant.', $mode));
}
$options = clone $this;
$options->inline = $mode;
return $options;
}
/**
* Treat lines that differ only in the amount of whitespace as equal, like "git diff -b"
*/
public function ignoreWhitespaceChanges(): self
{
$options = clone $this;
$options->ignoreWhitespaceChanges = true;
return $options;
}
/**
* Treat lines that differ only in trailing whitespace as equal, like "git diff --ignore-space-at-eol"
*/
public function ignoreTrailingWhitespace(): self
{
$options = clone $this;
$options->ignoreTrailingWhitespace = true;
return $options;
}
/**
* The most lines either text may have (20,000 by default); longer texts throw
*
* The defaults keep the slowest case, two entirely different texts, to a few seconds and
* under 100 MB.
*/
public function maxLines(int $lines): self
{
self::atLeast('maximum number of lines', $lines, 1);
$options = clone $this;
$options->maxLines = $lines;
return $options;
}
/**
* The most bytes either text may have (4 MiB by default); larger texts throw
*/
public function maxInputBytes(int $bytes): self
{
self::atLeast('maximum number of bytes', $bytes, 1);
$options = clone $this;
$options->maxInputBytes = $bytes;
return $options;
}
/**
* The longest line, in bytes, that gets inline highlighting (1,000 by default); longer
* modified lines are highlighted as a whole
*/
public function maxInlineLength(int $bytes): self
{
self::atLeast('maximum inline length', $bytes, 1);
$options = clone $this;
$options->maxInlineLength = $bytes;
return $options;
}
/**
* @internal
*/
public function contextLines(): int
{
return $this->context;
}
/**
* @internal
*/
public function pairsModifiedLines(): bool
{
return $this->pairModifiedLines;
}
/**
* @internal
*/
public function threshold(): float
{
return $this->similarityThreshold;
}
/**
* @internal
*/
public function inlineMode(): string
{
return $this->inline;
}
/**
* @internal
*/
public function ignoresWhitespaceChanges(): bool
{
return $this->ignoreWhitespaceChanges;
}
/**
* @internal
*/
public function ignoresTrailingWhitespace(): bool
{
return $this->ignoreTrailingWhitespace;
}
/**
* @internal
*/
public function lineLimit(): int
{
return $this->maxLines;
}
/**
* @internal
*/
public function byteLimit(): int
{
return $this->maxInputBytes;
}
/**
* @internal
*/
public function inlineLengthLimit(): int
{
return $this->maxInlineLength;
}
private static function atLeast(string $name, int $value, int $minimum): void
{
if ($value < $minimum) {
throw new InvalidArgumentException(sprintf('The %s must be at least %d, %d given.', $name, $minimum, $value));
}
}
}