ChangeHunkParserState   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 91
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A processNewChangeHunk() 0 3 1
A processRemoveLine() 0 5 1
A processNewFileDiffStart() 0 5 1
A __construct() 0 7 1
A processLine() 0 25 5
A finish() 0 3 1
A processAddLine() 0 5 1
A processNoChange() 0 4 1
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
final 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
     * ChangeHunkParserState constructor.
36
     *
37
     * @throws DiffParseException
38
     */
39
    public function __construct(
40
        private FileMutationBuilder $fileMutationBuilder,
41
        string $rangeInformationAsString,
42
    ) {
43
        $rangeInformation = new RangeInformation($rangeInformationAsString);
44
        $this->originalFileLine = $rangeInformation->getOriginalFileStartLine();
45
        $this->newFileLine = $rangeInformation->getNewFileStartLine();
46
    }
47
48
    public function processLine(string $line): State
49
    {
50
        if (LineTypeDetector::isStartOfFileDiff($line)) {
51
            return $this->processNewFileDiffStart();
52
        }
53
54
        if (LineTypeDetector::isStartOfChangeHunk($line)) {
55
            return $this->processNewChangeHunk($line);
56
        }
57
58
        if (StringUtils::startsWith('+', $line)) {
59
            $this->processAddLine();
60
61
            return $this;
62
        }
63
64
        if (StringUtils::startsWith('-', $line)) {
65
            $this->processRemoveLine();
66
67
            return $this;
68
        }
69
70
        $this->processNoChange();
71
72
        return $this;
73
    }
74
75
    private function processNewFileDiffStart(): State
76
    {
77
        $fileMutationsBuilder = $this->fileMutationBuilder->build();
78
79
        return new FindOriginalFileNameState($fileMutationsBuilder);
80
    }
81
82
    /**
83
     * @throws DiffParseException
84
     */
85
    private function processNewChangeHunk(string $line): State
86
    {
87
        return new self($this->fileMutationBuilder, $line);
88
    }
89
90
    private function processAddLine(): void
91
    {
92
        $lineMutation = LineMutation::newLineNumber(new LineNumber($this->newFileLine));
93
        $this->fileMutationBuilder->addLineMutation($lineMutation);
94
        ++$this->newFileLine;
95
    }
96
97
    private function processRemoveLine(): void
98
    {
99
        $lineMutation = LineMutation::originalLineNumber(new LineNumber($this->originalFileLine));
100
        $this->fileMutationBuilder->addLineMutation($lineMutation);
101
        ++$this->originalFileLine;
102
    }
103
104
    private function processNoChange(): void
105
    {
106
        ++$this->originalFileLine;
107
        ++$this->newFileLine;
108
    }
109
110
    public function finish(): void
111
    {
112
        $this->fileMutationBuilder->build();
113
    }
114
}
115