Passed
Push — master ( 0c214a...b07957 )
by Fabian
07:06
created

Line::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
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
namespace PhpMerge\internal;
12
13
use SebastianBergmann\Diff\Line as DiffLine;
14
15
/**
16
 * Class Line
17
 *
18
 * @package    PhpMerge
19
 * @author     Fabian Bircher <[email protected]>
20
 * @copyright  Fabian Bircher <[email protected]>
21
 * @license    https://opensource.org/licenses/MIT
22
 * @version    Release: @package_version@
23
 * @link       http://github.com/bircher/php-merge
24
 * @internal   This class is not part of the public api.
25
 */
26
final class Line extends DiffLine
27
{
28
29
    /**
30
     * @var int
31
     */
32
    protected $index;
33
34
    /**
35
     * Line constructor.
36
     * @param int $type
37
     * @param string $content
38
     * @param int $index
39
     */
40 56
    public function __construct($type = self::UNCHANGED, $content = '', $index = null)
41
    {
42 56
        parent::__construct($type, $content);
43
44 56
        $this->index = $index;
45 56
    }
46
47
    /**
48
     * @return int
49
     */
50 52
    public function getIndex()
51
    {
52 52
        return $this->index;
53
    }
54
55
    /**
56
     * @param array $diff
57
     * @return Line[]
58
     */
59 52
    public static function createArray($diff)
60
    {
61 52
        $index = -1;
62 52
        $lines = [];
63 52
        foreach ($diff as $key => $value) {
64 52
            switch ($value[1]) {
65 52
                case 0:
66 52
                    $index++;
67 52
                    $line = new Line(Line::UNCHANGED, $value[0], $index);
68 52
                    break;
69
70 52
                case 1:
71 52
                    $line = new Line(Line::ADDED, $value[0], $index);
72 52
                    break;
73
74 52
                case 2:
75 52
                    $index++;
76 52
                    $line = new Line(Line::REMOVED, $value[0], $index);
77 52
                    break;
78
79
                default:
80 4
                    throw new \RuntimeException('Unsupported diff line type.');
81
            }
82 52
            $lines[] = $line;
83
        }
84 52
        return $lines;
85
    }
86
}
87