FindNewFileNameState::finish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\HistoryAnalyser\UnifiedDiffParser\NewFileName;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\StringUtils;
17
18
/**
19
 * Looking for the New File name. Previous line of the diff will have contained the Original File name.
20
 *
21
 * If this refers to either an added or deleted file then ignore the Change Hunks and scan for next File Diff.
22
 */
23
final class FindNewFileNameState implements State
24
{
25
    public const NEW_FILE = '+++ b/';
26
27
    /**
28
     * FindNewFileNameState constructor.
29
     */
30
    public function __construct(
31
        private FileMutationBuilder $fileMutationBuilder,
32
    ) {
33
    }
34
35
    public function processLine(string $line): State
36
    {
37
        if (LineTypeDetector::isDeletedFile($line)) {
38
            return new FindFileDiffStartState($this->fileMutationBuilder->build());
39
        }
40
41
        if (!StringUtils::startsWith(self::NEW_FILE, $line)) {
42
            throw DiffParseException::missingNewFileName($line);
43
        }
44
45
        $newFileName = StringUtils::removeFromStart(self::NEW_FILE, $line);
46
        $this->fileMutationBuilder->setNewFileName(new NewFileName($newFileName));
47
48
        if ($this->fileMutationBuilder->isAddedFile()) {
49
            return new FindFileDiffStartState($this->fileMutationBuilder->build());
50
        }
51
52
        return new FindChangeHunkStartState($this->fileMutationBuilder);
53
    }
54
55
    public function finish(): void
56
    {
57
        throw DiffParseException::missingNewFileName(DiffParseException::END_OF_FILE);
58
    }
59
}
60