Passed
Pull Request — master (#69)
by Dave
02:21
created

FindNewFileNameState   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processLine() 0 18 4
A __construct() 0 3 1
A finish() 0 3 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\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
class FindNewFileNameState implements State
24
{
25
    const NEW_FILE = '+++ b/';
26
27
    /**
28
     * @var FileMutationBuilder
29
     */
30
    private $fileMutationBuilder;
31
32
    /**
33
     * FindNewFileNameState constructor.
34
     */
35
    public function __construct(FileMutationBuilder $fileMutationBuilder)
36
    {
37
        $this->fileMutationBuilder = $fileMutationBuilder;
38
    }
39
40
    public function processLine(string $line): State
41
    {
42
        if (LineTypeDetector::isDeletedFile($line)) {
43
            return new FindFileDiffStartState($this->fileMutationBuilder->build());
44
        }
45
46
        if (!StringUtils::startsWith(self::NEW_FILE, $line)) {
47
            throw DiffParseException::missingNewFileName($line);
48
        }
49
50
        $newFileName = StringUtils::removeFromStart(self::NEW_FILE, $line);
51
        $this->fileMutationBuilder->setNewFileName(new NewFileName($newFileName));
52
53
        if ($this->fileMutationBuilder->isAddedFile()) {
54
            return new FindFileDiffStartState($this->fileMutationBuilder->build());
55
        }
56
57
        return new FindChangeHunkStartState($this->fileMutationBuilder);
58
    }
59
60
    public function finish(): void
61
    {
62
        throw DiffParseException::missingNewFileName(DiffParseException::END_OF_FILE);
63
    }
64
}
65