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

LineMutation::getOriginalLine()   A

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;
14
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber;
16
17
class LineMutation
18
{
19
    /**
20
     * @var LineNumber|null
21
     */
22
    private $newLine;
23
24
    /**
25
     * @var LineNumber|null
26
     */
27
    private $originalLine;
28
29
    public static function originalLineNumber(LineNumber $lineNumber): self
30
    {
31
        return new self($lineNumber, null);
32
    }
33
34
    public static function newLineNumber(LineNumber $lineNumber): self
35
    {
36
        return new self(null, $lineNumber);
37
    }
38
39
    private function __construct(?LineNumber $originalLine, ?LineNumber $newLine)
40
    {
41
        $this->newLine = $newLine;
42
        $this->originalLine = $originalLine;
43
    }
44
45
    public function getNewLine(): ?LineNumber
46
    {
47
        return $this->newLine;
48
    }
49
50
    public function getOriginalLine(): ?LineNumber
51
    {
52
        return $this->originalLine;
53
    }
54
55
    public function isAdded(): bool
56
    {
57
        return null !== $this->newLine;
58
    }
59
60
    /**
61
     * Returns true if other LineMutation is the same.
62
     *
63
     * @param LineMutation|null $other
64
     */
65
    public function isEqual(?self $other): bool
66
    {
67
        if (null === $other) {
68
            return false;
69
        }
70
71
        if (!$this->isLineNumberEqual($this->newLine, $other->newLine)) {
72
            return false;
73
        }
74
75
        return $this->isLineNumberEqual($this->originalLine, $other->originalLine);
76
    }
77
78
    private function isLineNumberEqual(?LineNumber $a, ?LineNumber $b): bool
79
    {
80
        if (null === $a) {
81
            return null === $b;
82
        }
83
84
        if (null === $b) {
85
            return false;
86
        }
87
88
        return $a->getLineNumber() === $b->getLineNumber();
89
    }
90
}
91