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

FileMutations   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A addFileMutation() 0 10 2
A getCount() 0 3 1
A getFileMutation() 0 5 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;
14
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * Holds FileMutation objects. Use the.
19
 */
20
class FileMutations
21
{
22
    /**
23
     * @var FileMutation[]
24
     */
25
    private $fileMutations;
26
27
    /**
28
     * FileMutations constructor.
29
     *
30
     * @param FileMutation[] $fileMutations
31
     */
32
    public function __construct(array $fileMutations)
33
    {
34
        $this->fileMutations = [];
35
        foreach ($fileMutations as $fileMutation) {
36
            $this->addFileMutation($fileMutation);
37
        }
38
    }
39
40
    /**
41
     * Returns FileMutations for the given file name. Or null if there are no file mutations for that file in the diff.
42
     */
43
    public function getFileMutation(NewFileName $newFileName): ?FileMutation
44
    {
45
        $newFileNameAsString = $newFileName->getFileName();
46
47
        return $this->fileMutations[$newFileNameAsString] ?? null;
48
    }
49
50
    private function addFileMutation(FileMutation $fileMutation): void
51
    {
52
        if ($fileMutation->isDeletedFile()) {
53
            return;
54
        }
55
        $newFileNameAsString = $fileMutation->getNewFileName()->getFileName();
56
        $alreadyExists = array_key_exists($newFileNameAsString, $this->fileMutations);
57
        Assert::false($alreadyExists, "Multiple new files with name [$newFileNameAsString]");
58
59
        $this->fileMutations[$newFileNameAsString] = $fileMutation;
60
    }
61
62
    /**
63
     * Returns number of FileMutations (only usecase is for testing).
64
     */
65
    public function getCount(): int
66
    {
67
        return count($this->fileMutations);
68
    }
69
}
70