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
|
|
|
private function processNewChangeHunk(string $line): State |
87
|
|
|
{ |
88
|
|
|
return new self($this->fileMutationBuilder, $line); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function processAddLine(): void |
92
|
|
|
{ |
93
|
|
|
$lineMutation = LineMutation::newLineNumber(new LineNumber($this->newFileLine)); |
94
|
|
|
$this->fileMutationBuilder->addLineMutation($lineMutation); |
95
|
|
|
++$this->newFileLine; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function processRemoveLine(): void |
99
|
|
|
{ |
100
|
|
|
$lineMutation = LineMutation::originalLineNumber(new LineNumber($this->originalFileLine)); |
101
|
|
|
$this->fileMutationBuilder->addLineMutation($lineMutation); |
102
|
|
|
++$this->originalFileLine; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function processNoChange(): void |
106
|
|
|
{ |
107
|
|
|
++$this->originalFileLine; |
108
|
|
|
++$this->newFileLine; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function finish(): void |
112
|
|
|
{ |
113
|
|
|
$this->fileMutationBuilder->build(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|