Passed
Push — 4.x ( db19f6...38fd84 )
by Fabian
04:30
created

Line::isSame()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
nc 3
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the php-merge package.
4
 *
5
 * (c) Fabian Bircher <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace PhpMerge\internal;
14
15
/**
16
 * Class Line
17
 *
18
 * @internal This class is not part of the public api.
19
 */
20
final class Line
21
{
22
    public const ADDED     = 1;
23
    public const REMOVED   = 2;
24
    public const UNCHANGED = 3;
25
26
    /**
27
     * @var int
28
     */
29
    private $type;
30
31
    /**
32
     * @var string
33
     */
34
    private $content;
35
36
    /**
37
     * @var int
38
     */
39
    protected $index;
40
41
    /**
42
     * Line constructor.
43
     * @param int $type
44
     * @param string $content
45
     * @param int $index
46
     */
47 14
    public function __construct($type = self::UNCHANGED, $content = '', $index = null)
48
    {
49 14
        $this->type = $type;
50 14
        $this->content = $content;
51 14
        $this->index = $index;
52 14
    }
53
54
    /**
55
     * @return int
56
     */
57 13
    public function getIndex()
58
    {
59 13
        return $this->index;
60
    }
61
62
    /**
63
     * @param array $diff
64
     *
65
     * @return Line[]
66
     */
67 13
    public static function createArray($diff)
68
    {
69 13
        $index = -1;
70 13
        $lines = [];
71 13
        foreach ($diff as $key => $value) {
72 13
            switch ($value[1]) {
73 13
                case 0:
74 13
                    $index++;
75 13
                    $line = new Line(Line::UNCHANGED, $value[0], $index);
76 13
                    break;
77
78 13
                case 1:
79 13
                    $line = new Line(Line::ADDED, $value[0], $index);
80 13
                    break;
81
82 13
                case 2:
83 13
                    $index++;
84 13
                    $line = new Line(Line::REMOVED, $value[0], $index);
85 13
                    break;
86
87
                default:
88 1
                    throw new \RuntimeException('Unsupported diff line type.');
89
            }
90 13
            $lines[] = $line;
91
        }
92
93 13
        return $lines;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 8
    public function getContent(): string
100
    {
101 8
        return $this->content;
102
    }
103
104
    /**
105
     * @return int
106
     */
107 13
    public function getType(): int
108
    {
109 13
        return $this->type;
110
    }
111
112
    /**
113
     * @param Line $other
114
     *
115
     * @return bool
116
     */
117 8
    public function isSame(Line $other): bool
118
    {
119 8
        return $this->type === $other->type && $this->index === $other->index && $this->content === $other->content;
120
    }
121
}
122