MarkdownParser::parseLines()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 8
nop 1
dl 0
loc 41
ccs 24
cts 24
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Changelog keeper
4
 *
5
 * @link      https://github.com/hiqdev/chkipper
6
 * @package   chkipper
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\lib\parsers;
12
13
/**
14
 * Markdown history parser.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class MarkdownParser extends AbstractParser
19
{
20 2
    public function parseLines(array $lines)
21
    {
22 2
        $this->getHistory()->initTags();
23
24 2
        $no = 0;
25 2
        foreach ($lines as $str) {
26 2
            $str = rtrim($str);
27 2
            ++$no;
28
29
            /// skip empty lines
30 2
            if (!$str) {
31
                /// headers
32 2
            } elseif ($no <= 2 || preg_match('/^# /', $str)) {
33 2
                $this->addHeader($str);
34
35
            /// links
36 2
            } elseif (preg_match('/^\[(.*?)\]:\s*(.*)$/', $str, $m)) {
37 2
                $this->addLink($m[1], $m[2]);
38
39
            /// tags
40 2
            } elseif (
41 2
                preg_match('/^##\s+\[(.+?)\]\s*-?\s*(.*)$/', $str, $m) ||
42 2
                preg_match('/^##\s+(\S+?)\s*-\s*(.*)$/', $str, $m)
43 2
            ) {
44 2
                $this->addTag($m[1], $m[2]);
45
46
            /// notes
47 2
            } elseif (preg_match('/^-\s+(.*)$/', $str, $m)) {
48 2
                $this->addNote($m[1]);
49
50
            /// commits
51 2
            } elseif (preg_match('/^\s+-\s+\[?([0-9a-fA-F]{7})\]?\s*(.*)$/', $str, $m)) {
52 2
                $this->addCommit($m[1], $m[2]);
53
54
            /// comments
55 2
            } else {
56 2
                $this->addComment($str);
57
            }
58 2
        }
59
60 2
        return $this->getHistory();
61
    }
62
}
63