Repository navigation
Expand file tree
/
Copy pathTextDiffResult.php
More file actions
177 lines (157 loc) · 5.25 KB
/
Copy pathTextDiffResult.php
File metadata and controls
177 lines (157 loc) · 5.25 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
<?php
declare(strict_types=1);
namespace Pharaonic\Diff;
use JsonSerializable;
use Pharaonic\Diff\Renderer\HtmlRenderer;
use Pharaonic\Diff\Renderer\UnifiedRenderer;
/**
* The lines of two texts, aligned: unchanged, removed and added lines in reading order, with the
* removed lines of a change before its added lines
*/
final class TextDiffResult implements JsonSerializable
{
/**
* @param list<DiffLine> $lines
* @param list<Hunk> $hunks
* @param list<ModifiedLinePair> $pairs
*
* @internal Results are created by Diff::text().
*/
public function __construct(
private array $lines,
private array $hunks,
private array $pairs,
private bool $oldEndsWithNewline,
private bool $newEndsWithNewline,
) {}
public function hasChanges(): bool
{
foreach ($this->lines as $line) {
if ($line->type() !== ChangeType::UNCHANGED) {
return true;
}
}
return false;
}
/**
* @return list<DiffLine>
*/
public function lines(): array
{
return $this->lines;
}
/**
* The changes with TextDiffOptions::context() unchanged lines around them; none when the texts
* are equal
*
* @return list<Hunk>
*/
public function hunks(): array
{
return $this->hunks;
}
/**
* @return list<DiffLine>
*/
public function addedLines(): array
{
return $this->ofType(ChangeType::ADDED);
}
/**
* @return list<DiffLine>
*/
public function removedLines(): array
{
return $this->ofType(ChangeType::REMOVED);
}
/**
* Removed and added lines that look alike, in order; empty when pairing is turned off
*
* @return list<ModifiedLinePair>
*/
public function modifiedPairs(): array
{
return $this->pairs;
}
/**
* Line counts; a modified pair still counts once as removed and once as added
*
* @return array{added: int, removed: int, modified: int, unchanged: int}
*/
public function stats(): array
{
return [
'added' => count($this->addedLines()),
'removed' => count($this->removedLines()),
'modified' => count($this->pairs),
'unchanged' => count($this->ofType(ChangeType::UNCHANGED)),
];
}
/**
* A unified diff that `git apply` and `patch` accept, or an empty string when the texts are
* equal; its lines end with "\n"
*/
public function toUnified(string $oldLabel = 'before', string $newLabel = 'after'): string
{
return UnifiedRenderer::render($this, $oldLabel, $newLabel);
}
/**
* A GitHub-style HTML table of the hunks, or an empty string when the texts are equal
*
* Every piece of text is escaped. Style it with resources/diff.css or your own rules for the
* ph-diff-* classes; line numbers and +/- markers are in data-line-number and data-marker
* attributes, so copying the code does not copy them.
*/
public function toHtml(): string
{
return HtmlRenderer::render($this);
}
/**
* @return array{stats: array{added: int, removed: int, modified: int, unchanged: int}, old_ends_with_newline: bool, new_ends_with_newline: bool, hunks: list<array{old_start: int, old_length: int, new_start: int, new_length: int, lines: list<array{type: string, content: string, old_line: int|null, new_line: int|null, inline: list<array{type: string, text: string}>}>}>, modified: list<array{old_line: int|null, new_line: int|null}>}
*/
public function toArray(): array
{
return [
'stats' => $this->stats(),
'old_ends_with_newline' => $this->oldEndsWithNewline,
'new_ends_with_newline' => $this->newEndsWithNewline,
'hunks' => array_map(static fn(Hunk $hunk): array => $hunk->toArray(), $this->hunks),
'modified' => array_map(static fn(ModifiedLinePair $pair): array => $pair->toArray(), $this->pairs),
];
}
/**
* @return array{stats: array{added: int, removed: int, modified: int, unchanged: int}, old_ends_with_newline: bool, new_ends_with_newline: bool, hunks: list<array{old_start: int, old_length: int, new_start: int, new_length: int, lines: list<array{type: string, content: string, old_line: int|null, new_line: int|null, inline: list<array{type: string, text: string}>}>}>, modified: list<array{old_line: int|null, new_line: int|null}>}
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* Encode toArray() as JSON; JSON_THROW_ON_ERROR is always added
*/
public function toJson(int $flags = 0): string
{
return json_encode($this->toArray(), $flags | JSON_THROW_ON_ERROR);
}
/**
* @internal
*/
public function oldEndsWithNewline(): bool
{
return $this->oldEndsWithNewline;
}
/**
* @internal
*/
public function newEndsWithNewline(): bool
{
return $this->newEndsWithNewline;
}
/**
* @return list<DiffLine>
*/
private function ofType(string $type): array
{
return array_values(array_filter($this->lines, static fn(DiffLine $line): bool => $line->type() === $type));
}
}