forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatcher.php
More file actions
173 lines (145 loc) · 4.2 KB
/
Copy pathPatcher.php
File metadata and controls
173 lines (145 loc) · 4.2 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
<?php declare(strict_types = 1);
namespace PHPStan\Fixable;
use Nette\Utils\Strings;
use PhpMerge\internal\Hunk;
use PhpMerge\internal\Line;
use PhpMerge\MergeConflict;
use PhpMerge\PhpMerge;
use PHPStan\Analyser\FixedErrorDiff;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\FileReader;
use ReflectionClass;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use function array_map;
use function count;
use function hash;
use function implode;
use function str_starts_with;
use function substr;
use const PHP_VERSION_ID;
use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;
#[AutowiredService]
final class Patcher
{
private Differ $differ;
public function __construct()
{
$this->differ = new Differ(new UnifiedDiffOutputBuilder());
}
/**
* @param FixedErrorDiff[] $diffs
* @throws FileChangedException
* @throws MergeConflictException
*/
public function applyDiffs(string $fileName, array $diffs): string
{
$fileContents = FileReader::read($fileName);
$fileHash = hash('sha256', $fileContents);
$diffHunks = [];
foreach ($diffs as $diff) {
if ($diff->originalHash !== $fileHash) {
throw new FileChangedException();
}
$diffHunks[] = Hunk::createArray(Line::createArray($this->reconstructFullDiff($fileContents, $diff->diff)));
}
if (count($diffHunks) === 0) {
return $fileContents;
}
$baseLines = Line::createArray(array_map(
static fn ($l) => [$l, Differ::OLD],
self::splitStringByLines($fileContents),
));
$refMerge = new ReflectionClass(PhpMerge::class);
$refMergeMethod = $refMerge->getMethod('mergeHunks');
if (PHP_VERSION_ID < 80100) {
$refMergeMethod->setAccessible(true);
}
$result = Line::createArray(array_map(
static fn ($l) => [$l, Differ::OLD],
$refMergeMethod->invokeArgs(null, [
$baseLines,
$diffHunks[0],
[],
]),
));
for ($i = 0; $i < count($diffHunks); $i++) {
/** @var MergeConflict[] $conflicts */
$conflicts = [];
$merged = $refMergeMethod->invokeArgs(null, [
$baseLines,
Hunk::createArray(Line::createArray($this->differ->diffToArray($fileContents, implode('', array_map(static fn ($l) => $l->getContent(), $result))))),
$diffHunks[$i],
&$conflicts,
]);
if (count($conflicts) > 0) {
throw new MergeConflictException();
}
$result = Line::createArray(array_map(
static fn ($l) => [$l, Differ::OLD],
$merged,
));
}
return implode('', array_map(static fn ($l) => $l->getContent(), $result));
}
/**
* @return array<array{mixed, Differ::OLD|Differ::ADDED|Differ::REMOVED}>
*/
private function reconstructFullDiff(string $originalText, string $unifiedDiff): array
{
$originalLines = self::splitStringByLines($originalText);
$diffLines = self::splitStringByLines($unifiedDiff);
$result = [];
$origLineNo = 0;
$diffPos = 0;
while ($diffPos < count($diffLines)) {
$line = $diffLines[$diffPos];
$matches = Strings::match($line, '/^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/');
if ($matches !== null) {
// Parse hunk header
$origStart = (int) $matches[1] - 1; // 0-based
$diffPos++;
// Emit kept lines before hunk
while ($origLineNo < $origStart) {
$result[] = [$originalLines[$origLineNo], Differ::OLD];
$origLineNo++;
}
// Process hunk
while ($diffPos < count($diffLines)) {
$line = $diffLines[$diffPos];
if (str_starts_with($line, '@@')) {
break; // next hunk
}
$prefix = $line[0] ?? '';
$content = substr($line, 1);
if ($prefix === ' ') {
$result[] = [$content, Differ::OLD];
$origLineNo++;
} elseif ($prefix === '-') {
$result[] = [$content, Differ::REMOVED];
$origLineNo++;
} elseif ($prefix === '+') {
$result[] = [$content, Differ::ADDED];
}
$diffPos++;
}
} else {
$diffPos++;
}
}
// Emit remaining lines as kept
while ($origLineNo < count($originalLines)) {
$result[] = [$originalLines[$origLineNo], Differ::OLD];
$origLineNo++;
}
return $result;
}
/**
* @return string[]
*/
private static function splitStringByLines(string $input): array
{
return Strings::split($input, '/(.*\R)/', PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}
}