Completed
Pull Request — master (#2)
by Billie
01:47
created

CommitMessage::getBodyLines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
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;
11
12
use SplFileObject;
13
14
/**
15
 * Class CommitMessage
16
 *
17
 * @package SebastianFeldmann\Git
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/git
20
 * @since   Class available since Release 0.9.0
21
 */
22
class CommitMessage
23
{
24
    /**
25
     * Commit Message content
26
     *
27
     * @var string
28
     */
29
    private $content;
30
31
    /**
32
     * Content split lines
33
     *
34
     * @var string[]
35
     */
36
    private $lines;
37
38
    /**
39
     * Amount of lines
40
     *
41
     * @var int
42
     */
43
    private $lineCount;
44
45
    /**
46
     * CommitMessage constructor.
47
     *
48
     * @param string $content
49
     */
50 12
    public function __construct(string $content)
51
    {
52 12
        $this->content   = $content;
53 12
        $this->lines     = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content);
54 12
        $this->lineCount = count($this->lines);
55 12
    }
56
57
    /**
58
     * Is message empty.
59
     *
60
     * @return bool
61
     */
62 1
    public function isEmpty() : bool
63
    {
64 1
        return empty($this->content);
65
    }
66
67
    /**
68
     * Get complete commit message content.
69
     *
70
     * @return string
71
     */
72 1
    public function getContent() : string
73
    {
74 1
        return $this->content;
75
    }
76
77
    /**
78
     * Return all lines.
79
     *
80
     * @return array
81
     */
82 1
    public function getLines() : array
83
    {
84 1
        return $this->lines;
85
    }
86
87
    /**
88
     * Return line count.
89
     *
90
     * @return int
91
     */
92 2
    public function getLineCount() : int
93
    {
94 2
        return $this->lineCount;
95
    }
96
97
    /**
98
     * Get a specific line.
99
     *
100
     * @param  int $index
101
     * @return string
102
     */
103 1
    public function getLine(int $index) : string
104
    {
105 1
        return isset($this->lines[$index]) ? $this->lines[$index] : '';
106
    }
107
108
    /**
109
     * Return first line.
110
     *
111
     * @return string
112
     */
113 2
    public function getSubject() : string
114
    {
115 2
        return $this->lines[0];
116
    }
117
118
    /**
119
     * Return content from line nr. 3 to the last line.
120
     *
121
     * @return string
122
     */
123 2
    public function getBody() : string
124
    {
125 2
        return implode(PHP_EOL, $this->getBodyLines());
126
    }
127
128
    /**
129
     * Return lines from line nr. 3 to the last line.
130
     *
131
     * @return array
132
     */
133 3
    public function getBodyLines() : array
134
    {
135 3
        return $this->lineCount < 3 ? [] : array_slice($this->lines, 2);
136
    }
137
138
    /**
139
     * Create CommitMessage from file.
140
     *
141
     * @param  string $path
142
     * @param  string|null $commentChar Lines beginning with this character will be ignored
143
     * @return \SebastianFeldmann\Git\CommitMessage
144
     */
145 3
    public static function createFromFile(string $path, string $commentChar = null) : CommitMessage
146
    {
147
148 3
        if (!file_exists($path)) {
149 1
            throw new \RuntimeException('Commit message file not found');
150
        }
151
152 2
        $file = new SplFileObject($path);
153 2
        $fileContent = '';
154
155 2
        foreach($file as $line) {
156 2
            if(!isset($line{0}) || $line{0} !== $commentChar) {
157 2
                $fileContent .= $line;
158
            }
159
        }
160
161 2
        return new CommitMessage($fileContent);
162
    }
163
}
164