Passed
Push — master ( 6a1c19...3acae6 )
by Sebastian
01:58
created

CommitMessage::getContentLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
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;
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 24
    public function __construct(string $content, string $commentCharacter = '#')
90
    {
91 24
        $this->rawContent       = $content;
92 24
        $this->rawLines         = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content);
0 ignored issues
show
Documentation Bug introduced by
It seems like empty($content) ? array(...\r\n|\r|\n/', $content) of type false is incompatible with the declared type string[] of property $rawLines.

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..

Loading history...
93 24
        $this->rawLineCount     = count($this->rawLines);
0 ignored issues
show
Bug introduced by
It seems like $this->rawLines can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        $this->rawLineCount     = count(/** @scrutinizer ignore-type */ $this->rawLines);
Loading history...
94 24
        $this->commentCharacter = $commentCharacter;
95 24
        $this->contentLines     = $this->getContentLines($this->rawLines, $commentCharacter);
0 ignored issues
show
Bug introduced by
It seems like $this->rawLines can also be of type false; however, parameter $rawLines of SebastianFeldmann\Git\Co...sage::getContentLines() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $this->contentLines     = $this->getContentLines(/** @scrutinizer ignore-type */ $this->rawLines, $commentCharacter);
Loading history...
96 24
        $this->contentLineCount = count($this->contentLines);
97 24
        $this->content          = implode(PHP_EOL, $this->contentLines);
98 24
    }
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
     * Get a specific content line
184
     *
185
     * @param  int $index
186
     * @return string
187
     */
188 1
    public function getContentLine(int $index) : string
189
    {
190 1
        return $this->contentLines[$index] ?? '';
191
    }
192
193
    /**
194
     * Return first line
195
     *
196
     * @return string
197
     */
198 3
    public function getSubject() : string
199
    {
200 3
        return $this->contentLines[0];
201
    }
202
203
    /**
204
     * Return content from line nr. 3 to the last line
205
     *
206
     * @return string
207
     */
208 3
    public function getBody() : string
209
    {
210 3
        return implode(PHP_EOL, $this->getBodyLines());
211
    }
212
213
    /**
214
     * Return lines from line nr. 3 to the last line
215
     *
216
     * @return array
217
     */
218 5
    public function getBodyLines() : array
219
    {
220 5
        return $this->contentLineCount < 3 ? [] : array_slice($this->contentLines, 2);
221
    }
222
223
    /**
224
     * Get the comment character
225
     *
226
     * Comment character defaults to '#'.
227
     *
228
     * @return string
229
     */
230 1
    public function getCommentCharacter() : string
231
    {
232 1
        return $this->commentCharacter;
233
    }
234
235
    /**
236
     * Get the lines that are not comments
237
     *
238
     * @param  array  $rawLines
239
     * @param  string $commentCharacter
240
     * @return string[]
241
     */
242 24
    private function getContentLines(array $rawLines, string $commentCharacter) : array
243
    {
244 24
        $lines = [];
245
246 24
        foreach($rawLines as $line) {
247 22
            if(!isset($line{0}) || $line{0} !== $commentCharacter) {
248 21
                $lines[] = $line;
249
            }
250
        }
251
252 24
        return $lines;
253
    }
254
255
    /**
256
     * Create CommitMessage from file
257
     *
258
     * @param  string $path
259
     * @param  string $commentCharacter
260
     * @return \SebastianFeldmann\Git\CommitMessage
261
     */
262 4
    public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage
263
    {
264 4
        if (!file_exists($path)) {
265 2
            throw new RuntimeException('Commit message file not found');
266
        }
267
268 2
        return new CommitMessage(file_get_contents($path), $commentCharacter);
269
    }
270
}
271