Completed
Push — master ( 7b4929...cefe8d )
by Andrii
01:50
created

MarkdownParser::parseLines()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
ccs 0
cts 24
cp 0
rs 4.8196
cc 10
eloc 22
nc 8
nop 1
crap 110

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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\history;
12
13
/**
14
 * Markdown history parser.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class MarkdownParser extends AbstractParser
19
{
20
    public function parseLines(array $lines)
21
    {
22
        $this->getHistory()->initTags();
23
24
        $no = 0;
25
        foreach ($lines as $str) {
26
            $str = rtrim($str);
27
            ++$no;
28
29
            /// skip empty lines
30
            if (!$str) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
31
                /// headers
32
            } elseif ($no <= 2 || preg_match('/^# /', $str)) {
33
                $this->addHeader($str);
34
35
            /// links
36
            } elseif (preg_match('/^\[(.*?)\]:\s*(.*)$/', $str, $m)) {
37
                $this->addLink($m[1], $m[2]);
38
39
            /// tags
40
            } elseif (
41
                preg_match('/^##\s+\[(.+?)\]\s*-?\s*(.*)$/', $str, $m) ||
42
                preg_match('/^##\s+(\S+?)\s*-\s*(.*)$/', $str, $m)
43
            ) {
44
                $this->addTag($m[1], $m[2]);
45
46
            /// notes
47
            } elseif (preg_match('/^-\s+(.*)$/', $str, $m)) {
48
                $this->addNote($m[1]);
49
50
            /// commits
51
            } elseif (preg_match('/^\s+-\s+\[?([0-9a-fA-F]{7})\]?\s*(.*)$/', $str, $m)) {
52
                $this->addCommit($m[1], $m[2]);
53
54
            /// comments
55
            } else {
56
                $this->addComment($str);
57
            }
58
        }
59
60
        return $this->getHistory();
61
    }
62
}
63