Completed
Push — master ( e98a3d...a4da4b )
by Andrii
04:15
created

GitLogParser::mergeGitLog()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.6666
cc 2
eloc 6
nc 1
nop 0
crap 6
1
<?php
2
3
/*
4
 * Changelog keeper
5
 *
6
 * @link      https://github.com/hiqdev/chkipper
7
 * @package   chkipper
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\chkipper\history;
13
14
use UnexpectedValueException;
15
16
/**
17
 * Git log parser.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class GitLogParser extends AbstractParser
22
{
23
    public function parseGitLog()
24
    {
25
        exec("git log --date=short --pretty='format:%h %ad %s [%ae] %d'", $logs);
26
27
        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...
28
    }
29
30 1
    public function parseLines(array $lines)
31
    {
32 1
        $this->getHistory()->initTags();
33
34 1
        foreach ($lines as $line) {
35 1
            if (!preg_match('/^(\w+) (([0-9-]+) (.*?) \[.*?\]) *\(?(.*?)\)?$/', $line, $m)) {
36
                throw new UnexpectedValueException('failed parse git line');
37
            }
38 1
            $tag  = $this->matchTag($m[5]);
39 1
            $note = $this->matchNote($m[4]);
40 1
            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...
41 1
                $this->addTag($tag, $m[3]);
42 1
            }
43 1
            if (false && $note) { /// disabled adding notes
44
                $this->addNote($note);
45
            }
46 1
            $this->addCommit($m[1], $m[2]);
47 1
        }
48
49 1
        return $this->getHistory();
50
    }
51
52
    /**
53
     * Finds first tag in given refs string.
54
     * @param string $str
55
     * @return string
56
     */
57 1
    public function matchTag($str)
58
    {
59 1
        $refs = explode(', ', $str);
60 1
        foreach ($refs as $ref) {
61 1
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
62 1
                return $m[1];
63
            }
64 1
        }
65
66 1
        return null;
67
    }
68
69 1
    public function matchNote($str)
70
    {
71 1
        return strpos($str, ' ') ? $str : null;
72
    }
73
}
74