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

CommitMessage::getCommentCharacter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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;
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
     * This includes lines that are comments
28
     *
29
     * @var string
30
     */
31
    private $rawContent;
32
33
    /**
34
     * Content split lines
35
     *
36
     * This includes lines that are comments
37
     *
38
     * @var string[]
39
     */
40
    private $rawLines;
41
42
    /**
43
     * Amount of lines
44
     *
45
     * This includes lines that are comments
46
     *
47
     * @var int
48
     */
49
    private $rawLineCount;
50
51
    /**
52
     * The comment character
53
     *
54
     * Null would indicate the comment character is not set for this commit. A comment character might be null if this
55
     * commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook,
56
     * where the commit message can still contain comments.
57
     *
58
     * @var string|null
59
     */
60
    private $commentCharacter;
61
62
    /**
63
     * Get the lines
64
     *
65
     * This excludes the lines which are comments
66
     *
67
     * @var string[]
68
     */
69
    private $lines;
70
71
    /**
72
     * Get the number of lines
73
     *
74
     * This excludes lines which are comments
75
     *
76
     * @var int
77
     */
78
    private $lineCount;
79
80
    /**
81
     * Commit Message content
82
     *
83
     * This excludes lines that are comments
84
     *
85
     * @var string
86
     */
87
    private $content;
88
89
    /**
90
     * CommitMessage constructor.
91
     *
92
     * @param string      $content
93
     * @param string|null $commentCharacter
94
     */
95 21
    public function __construct(string $content, string $commentCharacter = null)
96
    {
97 21
        $this->rawContent   = $content;
98 21
        $this->rawLines     = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content);
99 21
        $this->rawLineCount = count($this->rawLines);
100
101 21
        $this->commentCharacter = $commentCharacter;
102 21
        $this->lines = $this->getContentLines($this->rawLines, $commentCharacter);
103 21
        $this->lineCount = count($this->lines);
104 21
        $this->content = implode(PHP_EOL, $this->lines);
105 21
    }
106
107
    /**
108
     * Is message empty.
109
     *
110
     * @return bool
111
     */
112 2
    public function isEmpty() : bool
113
    {
114 2
        return empty($this->content);
115
    }
116
117
    /**
118
     * Get complete commit message content.
119
     *
120
     * This includes lines that are comments
121
     *
122
     * @return string
123
     */
124 2
    public function getContent() : string
125
    {
126 2
        return $this->rawContent;
127
    }
128
129
    /**
130
     * Return all lines.
131
     *
132
     * This includes lines that are comments
133
     *
134
     * @return array
135
     */
136 2
    public function getLines() : array
137
    {
138 2
        return $this->rawLines;
139
    }
140
141
    /**
142
     * Return line count.
143
     *
144
     * This includes lines that are comments
145
     *
146
     * @return int
147
     */
148 3
    public function getLineCount() : int
149
    {
150 3
        return $this->rawLineCount;
151
    }
152
153
    /**
154
     * Get a specific line.
155
     *
156
     * @param  int $index
157
     * @return string
158
     */
159 2
    public function getLine(int $index) : string
160
    {
161 2
        return isset($this->rawLines[$index]) ? $this->rawLines[$index] : '';
162
    }
163
164
    /**
165
     * Return first line.
166
     *
167
     * @return string
168
     */
169 3
    public function getSubject() : string
170
    {
171 3
        return $this->lines[0];
172
    }
173
174
    /**
175
     * Return content from line nr. 3 to the last line.
176
     *
177
     * @return string
178
     */
179 3
    public function getBody() : string
180
    {
181 3
        return implode(PHP_EOL, $this->getBodyLines());
182
    }
183
184
    /**
185
     * Return lines from line nr. 3 to the last line.
186
     *
187
     * @return array
188
     */
189 5
    public function getBodyLines() : array
190
    {
191 5
        return $this->lineCount < 3 ? [] : array_slice($this->lines, 2);
192
    }
193
194
    /**
195
     * Get the comment character
196
     *
197
     * Null would indicate the comment character is not set for this commit. A comment character might be null if this
198
     * commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook.
199
     *
200
     * @return null|string
201
     */
202 1
    public function getCommentCharacter()
203
    {
204 1
        return $this->commentCharacter;
205
    }
206
207
    /**
208
     * Get the lines that are not comments
209
     *
210
     * Null comment character indicates no comment character.
211
     *
212
     * @param  array       $rawLines
213
     * @param  string|null $commentCharacter
214
     * @return string[]
215
     */
216 21
    private function getContentLines(array $rawLines, string $commentCharacter = null) : array {
217 21
        $lines = [];
218
219 21
        foreach($rawLines as $line) {
220 19
            if(!isset($line{0}) || $line{0} !== $commentCharacter) {
221 19
                $lines[] = $line;
222
            }
223
        }
224
225 21
        return $lines;
226
    }
227
228
    /**
229
     * Create CommitMessage from file.
230
     *
231
     * @param  string $path
232
     * @param  string|null $commentCharacter
233
     * @return \SebastianFeldmann\Git\CommitMessage
234
     */
235 3
    public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage
236
    {
237 3
        if (!file_exists($path)) {
238 1
            throw new \RuntimeException('Commit message file not found');
239
        }
240
241 2
        return new CommitMessage(file_get_contents($path), $commentCharacter);
242
    }
243
}
244