Completed
Push — master ( fdef12...11a376 )
by Andrii
02:13
created

GitLogParser::matchNote()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
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\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 1
    public function parseLines(array $lines)
30
    {
31 1
        $this->getHistory()->initTags();
32
33 1
        foreach ($lines as $line) {
34 1
            if (!preg_match('/^(\w+) (([0-9-]+) (.*?) \[.*?\]) *\(?(.*?)\)?$/', $line, $m)) {
35
                throw new UnexpectedValueException('failed parse git line');
36
            }
37 1
            $tag  = $this->matchTag($m[5]);
38 1
            $note = $this->matchNote($m[4]);
39 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...
40 1
                $this->addTag($tag, $m[3]);
41 1
            }
42 1
            if (false && $note) { /// disabled adding notes
43
                $this->addNote($note);
44
            }
45 1
            $this->addCommit($m[1], $m[2]);
46 1
        }
47
48 1
        return $this->getHistory();
49
    }
50
51
    /**
52
     * Finds first tag in given refs string.
53
     * @param string $str
54
     * @return string
55
     */
56 1
    public function matchTag($str)
57
    {
58 1
        $refs = explode(', ', $str);
59 1
        foreach ($refs as $ref) {
60 1
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
61 1
                return $m[1];
62
            }
63 1
        }
64
65 1
        return null;
66
    }
67
68 1
    public function matchNote($str)
69
    {
70 1
        return strpos($str, ' ') ? $str : null;
71
    }
72
}
73