GitLogParser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 51
ccs 22
cts 28
cp 0.7856
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A matchTag() 0 10 3
A matchNote() 0 3 2
A parseLines() 0 20 6
A parseGitLog() 0 5 1
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
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);
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) {
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