|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Static Analysis Results Baseliner (sarb). |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dave Liddament |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and licence information please view the LICENSE file distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\UnifiedDiffParser\internal; |
|
14
|
|
|
|
|
15
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber; |
|
16
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\UnifiedDiffParser\LineMutation; |
|
17
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\StringUtils; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Used when currently parsing a Change Hunk. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ChangeHunkParserState implements State |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
private $originalFileLine; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $newFileLine; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var FileMutationBuilder |
|
36
|
|
|
*/ |
|
37
|
|
|
private $fileMutationBuilder; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* ChangeHunkParserState constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @throws DiffParseException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(FileMutationBuilder $fileMutationBuilder, string $rangeInformationAsString) |
|
45
|
|
|
{ |
|
46
|
|
|
$rangeInformation = new RangeInformation($rangeInformationAsString); |
|
47
|
|
|
$this->fileMutationBuilder = $fileMutationBuilder; |
|
48
|
|
|
$this->originalFileLine = $rangeInformation->getOriginalFileStartLine(); |
|
49
|
|
|
$this->newFileLine = $rangeInformation->getNewFileStartLine(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function processLine(string $line): State |
|
53
|
|
|
{ |
|
54
|
|
|
if (LineTypeDetector::isStartOfFileDiff($line)) { |
|
55
|
|
|
return $this->processNewFileDiffStart(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (LineTypeDetector::isStartOfChangeHunk($line)) { |
|
59
|
|
|
return $this->processNewChangeHunk($line); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (StringUtils::startsWith('+', $line)) { |
|
63
|
|
|
$this->processAddLine(); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (StringUtils::startsWith('-', $line)) { |
|
69
|
|
|
$this->processRemoveLine(); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->processNoChange(); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function processNewFileDiffStart(): State |
|
80
|
|
|
{ |
|
81
|
|
|
$fileMutationsBuilder = $this->fileMutationBuilder->build(); |
|
82
|
|
|
|
|
83
|
|
|
return new FindOriginalFileNameState($fileMutationsBuilder); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @throws DiffParseException |
|
88
|
|
|
*/ |
|
89
|
|
|
private function processNewChangeHunk(string $line): State |
|
90
|
|
|
{ |
|
91
|
|
|
return new self($this->fileMutationBuilder, $line); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function processAddLine(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$lineMutation = LineMutation::newLineNumber(new LineNumber($this->newFileLine)); |
|
97
|
|
|
$this->fileMutationBuilder->addLineMutation($lineMutation); |
|
98
|
|
|
++$this->newFileLine; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function processRemoveLine(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$lineMutation = LineMutation::originalLineNumber(new LineNumber($this->originalFileLine)); |
|
104
|
|
|
$this->fileMutationBuilder->addLineMutation($lineMutation); |
|
105
|
|
|
++$this->originalFileLine; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function processNoChange(): void |
|
109
|
|
|
{ |
|
110
|
|
|
++$this->originalFileLine; |
|
111
|
|
|
++$this->newFileLine; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function finish(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->fileMutationBuilder->build(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|