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 RuntimeException; |
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
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $commentCharacter; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* All non comment lines |
60
|
|
|
* |
61
|
|
|
* @var string[] |
62
|
|
|
*/ |
63
|
|
|
private $contentLines; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the number of lines |
67
|
|
|
* |
68
|
|
|
* This excludes lines which are comments. |
69
|
|
|
* |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
private $contentLineCount; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Commit Message content |
76
|
|
|
* |
77
|
|
|
* This excludes lines that are comments. |
78
|
|
|
* |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
private $content; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* CommitMessage constructor |
85
|
|
|
* |
86
|
|
|
* @param string $content |
87
|
|
|
* @param string $commentCharacter |
88
|
|
|
*/ |
89
|
23 |
|
public function __construct(string $content, string $commentCharacter = '#') |
90
|
|
|
{ |
91
|
23 |
|
$this->rawContent = $content; |
92
|
23 |
|
$this->rawLines = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content); |
|
|
|
|
93
|
23 |
|
$this->rawLineCount = count($this->rawLines); |
|
|
|
|
94
|
23 |
|
$this->commentCharacter = $commentCharacter; |
95
|
23 |
|
$this->contentLines = $this->getContentLines($this->rawLines, $commentCharacter); |
|
|
|
|
96
|
23 |
|
$this->contentLineCount = count($this->contentLines); |
97
|
23 |
|
$this->content = implode(PHP_EOL, $this->contentLines); |
98
|
23 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Is message empty. |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
2 |
|
public function isEmpty() : bool |
106
|
|
|
{ |
107
|
2 |
|
return empty($this->content); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get commit message content |
113
|
|
|
* |
114
|
|
|
* This excludes lines that are comments. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
2 |
|
public function getContent() : string |
119
|
|
|
{ |
120
|
2 |
|
return $this->content; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get complete commit message content |
125
|
|
|
* |
126
|
|
|
* This includes lines that are comments. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
1 |
|
public function getRawContent() : string |
131
|
|
|
{ |
132
|
1 |
|
return $this->rawContent; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return all lines |
137
|
|
|
* |
138
|
|
|
* This includes lines that are comments. |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
2 |
|
public function getLines() : array |
143
|
|
|
{ |
144
|
2 |
|
return $this->rawLines; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return line count |
149
|
|
|
* |
150
|
|
|
* This includes lines that are comments. |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
3 |
|
public function getLineCount() : int |
155
|
|
|
{ |
156
|
3 |
|
return $this->rawLineCount; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return content line count |
161
|
|
|
* |
162
|
|
|
* This doesn't includes lines that are comments. |
163
|
|
|
* |
164
|
|
|
* @return int |
165
|
|
|
*/ |
166
|
1 |
|
public function getContentLineCount() : int |
167
|
|
|
{ |
168
|
1 |
|
return $this->contentLineCount; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get a specific line |
173
|
|
|
* |
174
|
|
|
* @param int $index |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
2 |
|
public function getLine(int $index) : string |
178
|
|
|
{ |
179
|
2 |
|
return $this->rawLines[$index] ?? ''; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return first line |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
3 |
|
public function getSubject() : string |
188
|
|
|
{ |
189
|
3 |
|
return $this->contentLines[0]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return content from line nr. 3 to the last line |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
3 |
|
public function getBody() : string |
198
|
|
|
{ |
199
|
3 |
|
return implode(PHP_EOL, $this->getBodyLines()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return lines from line nr. 3 to the last line |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
5 |
|
public function getBodyLines() : array |
208
|
|
|
{ |
209
|
5 |
|
return $this->contentLineCount < 3 ? [] : array_slice($this->contentLines, 2); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the comment character |
214
|
|
|
* |
215
|
|
|
* Comment character defaults to '#'. |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
1 |
|
public function getCommentCharacter() : string |
220
|
|
|
{ |
221
|
1 |
|
return $this->commentCharacter; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the lines that are not comments |
226
|
|
|
* |
227
|
|
|
* @param array $rawLines |
228
|
|
|
* @param string $commentCharacter |
229
|
|
|
* @return string[] |
230
|
|
|
*/ |
231
|
23 |
|
private function getContentLines(array $rawLines, string $commentCharacter) : array |
232
|
|
|
{ |
233
|
23 |
|
$lines = []; |
234
|
|
|
|
235
|
23 |
|
foreach($rawLines as $line) { |
236
|
21 |
|
if(!isset($line{0}) || $line{0} !== $commentCharacter) { |
237
|
20 |
|
$lines[] = $line; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
23 |
|
return $lines; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Create CommitMessage from file |
246
|
|
|
* |
247
|
|
|
* @param string $path |
248
|
|
|
* @param string $commentCharacter |
249
|
|
|
* @return \SebastianFeldmann\Git\CommitMessage |
250
|
|
|
*/ |
251
|
4 |
|
public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage |
252
|
|
|
{ |
253
|
4 |
|
if (!file_exists($path)) { |
254
|
2 |
|
throw new RuntimeException('Commit message file not found'); |
255
|
|
|
} |
256
|
|
|
|
257
|
2 |
|
return new CommitMessage(file_get_contents($path), $commentCharacter); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..