-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathFileDiff.php
More file actions
163 lines (133 loc) · 4.48 KB
/
FileDiff.php
File metadata and controls
163 lines (133 loc) · 4.48 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
<?php
declare(strict_types=1);
namespace Rector\ValueObject\Reporting;
use Nette\Utils\Strings;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\RectorInterface;
use Rector\Parallel\ValueObject\BridgeItem;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\Util\RectorClassesSorter;
use Symplify\EasyParallel\Contract\SerializableInterface;
use Webmozart\Assert\Assert;
final readonly class FileDiff implements SerializableInterface
{
/**
* @var string
* @see https://en.wikipedia.org/wiki/Diff#Unified_format
* @see https://regex101.com/r/AUPIX4/2
*/
private const DIFF_HUNK_HEADER_REGEX = '#@@(.*?)(?<' . self::FIRST_LINE_KEY . '>\d+)(,(?<' . self::LINE_RANGE_KEY . '>\d+))?(.*?)@@#';
/**
* @var string
*/
private const FIRST_LINE_KEY = 'first_line';
private const LINE_RANGE_KEY = 'line_range';
/**
* @param RectorWithLineChange[] $rectorsWithLineChanges
*/
public function __construct(
private string $relativeFilePath,
private string $diff,
private string $diffConsoleFormatted,
private array $rectorsWithLineChanges = []
) {
Assert::allIsInstanceOf($rectorsWithLineChanges, RectorWithLineChange::class);
}
public function getDiff(): string
{
return $this->diff;
}
public function getDiffConsoleFormatted(): string
{
return $this->diffConsoleFormatted;
}
public function getRelativeFilePath(): string
{
return $this->relativeFilePath;
}
public function getAbsoluteFilePath(): ?string
{
return \realpath($this->relativeFilePath) ?: null;
}
/**
* @return RectorWithLineChange[]
*/
public function getRectorChanges(): array
{
return $this->rectorsWithLineChanges;
}
/**
* @return string[]
*/
public function getRectorShortClasses(): array
{
$rectorShortClasses = [];
foreach ($this->getRectorClasses() as $rectorClass) {
$rectorShortClasses[] = (string) Strings::after($rectorClass, '\\', -1);
}
return $rectorShortClasses;
}
/**
* @return array<class-string<RectorInterface|PostRectorInterface>>
*/
public function getRectorClasses(): array
{
$rectorClasses = [];
foreach ($this->rectorsWithLineChanges as $rectorWithLineChange) {
$rectorClasses[] = $rectorWithLineChange->getRectorClass();
}
return RectorClassesSorter::sortAndFilterOutPostRectors($rectorClasses);
}
public function getFirstLineNumber(): ?int
{
$match = Strings::match($this->diff, self::DIFF_HUNK_HEADER_REGEX);
// probably some error in diff
if (! isset($match[self::FIRST_LINE_KEY])) {
return null;
}
return (int) $match[self::FIRST_LINE_KEY];
}
public function getLastLineNumber(): ?int
{
$match = Strings::match($this->diff, self::DIFF_HUNK_HEADER_REGEX);
$firstLine = $this->getFirstLineNumber();
// probably some error in diff
if (! isset($match[self::LINE_RANGE_KEY])) {
return $firstLine;
}
// line range is not mandatory
if ($match[self::LINE_RANGE_KEY] === '') {
return $firstLine;
}
$lineRange = (int) $match[self::LINE_RANGE_KEY];
return $firstLine + $lineRange;
}
/**
* @return array{relative_file_path: string, diff: string, diff_console_formatted: string, rectors_with_line_changes: RectorWithLineChange[]}
*/
public function jsonSerialize(): array
{
return [
BridgeItem::RELATIVE_FILE_PATH => $this->relativeFilePath,
BridgeItem::DIFF => $this->diff,
BridgeItem::DIFF_CONSOLE_FORMATTED => $this->diffConsoleFormatted,
BridgeItem::RECTORS_WITH_LINE_CHANGES => $this->rectorsWithLineChanges,
];
}
/**
* @param array<string, mixed> $json
*/
public static function decode(array $json): self
{
$rectorWithLineChanges = [];
foreach ($json[BridgeItem::RECTORS_WITH_LINE_CHANGES] as $rectorWithLineChangesJson) {
$rectorWithLineChanges[] = RectorWithLineChange::decode($rectorWithLineChangesJson);
}
return new self(
$json[BridgeItem::RELATIVE_FILE_PATH],
$json[BridgeItem::DIFF],
$json[BridgeItem::DIFF_CONSOLE_FORMATTED],
$rectorWithLineChanges,
);
}
}