Completed
Push — master ( 751a3d...b7ca33 )
by Sebastian
17:18 queued 15:56
created

Change::addLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Git.
4
 *
5
 * (c) Sebastian Feldmann <[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
namespace SebastianFeldmann\Git\Diff;
11
12
/**
13
 * Class Change.
14
 *
15
 * @author  Sebastian Feldmann <[email protected]>
16
 * @link    https://github.com/sebastianfeldmann/git
17
 * @since   Class available since Release 1.2.0
18
 */
19
class Change
20
{
21
    /**
22
     * Optional file header.
23
     *
24
     * @var string
25
     */
26
    private $header;
27
28
    /**
29
     * Pre range ['from' => x, 'to' => y]
30
     *
31
     * @var array
32
     */
33
    private $pre;
34
35
    /**
36
     * Post range ['from' => x, 'to' => y]
37
     *
38
     * @var array
39
     */
40
    private $post;
41
42
    /**
43
     * List of changed lines.
44
     *
45
     * @var \SebastianFeldmann\Git\Diff\Line[]
46
     */
47
    private $lines = [];
48
49
    /**
50
     * Chan
51
     * ge constructor.
52
     *
53
     * @param string $ranges
54
     * @param string $header
55
     */
56 12
    public function __construct(string $ranges, string $header = '')
57
    {
58 12
        $this->header = $header;
59 12
        $this->splitRanges($ranges);
60 11
    }
61
62
    /**
63
     * Header getter.
64
     *
65
     * @return string
66
     */
67 3
    public function getHeader(): string
68
    {
69 3
        return $this->header;
70
    }
71
72
    /**
73
     * Pre range getter.
74
     *
75
     * @return array
76
     */
77 1
    public function getPre(): array
78
    {
79 1
        return $this->pre;
80
    }
81
82
    /**
83
     * Post range getter.
84
     *
85
     * @return array
86
     */
87 1
    public function getPost(): array
88
    {
89 1
        return $this->post;
90
    }
91
92
    /**
93
     * Return list of changed lines.
94
     *
95
     * @return \SebastianFeldmann\Git\Diff\Line[]
96
     */
97 9
    public function getLines(): array
98
    {
99 9
        return $this->lines;
100
    }
101
102
    /**
103
     * Add a line to the change.
104
     *
105
     * @param \SebastianFeldmann\Git\Diff\Line $line
106
     */
107 9
    public function addLine(Line $line)
108
    {
109 9
        $this->lines[] = $line;
110 9
    }
111
112
    /**
113
     * Parse ranges and split them into pre and post range.
114
     *
115
     * @param string $ranges
116
     */
117 12
    private function splitRanges(string $ranges)
118
    {
119 12
        $matches = [];
120 12
        if (!preg_match('#^[\-|\+]{1}([0-9]+),([0-9]+) [\-\+]{1}([0-9]+),([0-9]+)$#', $ranges, $matches)) {
121 1
            throw new \RuntimeException('invalid ranges: ' . $ranges);
122
        }
123 11
        $this->pre  = ['from' => (int)$matches[1], 'to' => (int)$matches[2]];
124 11
        $this->post = ['from' => (int)$matches[3], 'to' => (int)$matches[4]];
125 11
    }
126
}
127