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

GitLogParser::parseLines()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 8.7624
cc 6
eloc 13
nc 6
nop 1
crap 42
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
use UnexpectedValueException;
14
15
/**
16
 * Git log parser.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class GitLogParser extends AbstractParser
21
{
22
    public function parseGitLog()
23
    {
24
        exec("git log --date=short --pretty='format:%h %ad %s [%ae] %d'", $logs);
25
26
        return $this->parseLines($logs);
0 ignored issues
show
Bug introduced by
It seems like $logs can also be of type null; however, hiqdev\chkipper\history\GitLogParser::parseLines() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
27
    }
28
29
    public function parseLines(array $lines)
30
    {
31
        $this->getHistory()->initTags();
32
33
        foreach ($lines as $line) {
34
            if (!preg_match('/^(\w+) (([0-9-]+) (.*?) \[.*?\]) *\(?(.*?)\)?$/', $line, $m)) {
35
                throw new UnexpectedValueException('failed parse git line');
36
            }
37
            $tag  = $this->matchTag($m[5]);
38
            $note = $this->matchNote($m[4]);
39
            if ($tag) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tag of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
40
                $this->addTag($tag, $m[3]);
41
            }
42
            if (false && $note) { /// disabled adding notes
43
                $this->addNote($note);
44
            }
45
            $this->addCommit($m[1], $m[2]);
46
        }
47
48
        return $this->getHistory();
49
    }
50
51
    /**
52
     * Finds first tag in given refs string.
53
     * @param string $str
54
     * @return string
55
     */
56
    public function matchTag($str)
57
    {
58
        $refs = explode(', ', $str);
59
        foreach ($refs as $ref) {
60
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
61
                return $m[1];
62
            }
63
        }
64
65
        return null;
66
    }
67
68
    public function matchNote($str)
69
    {
70
        return strpos($str, ' ') ? $str : null;
71
    }
72
}
73