Completed
Push — master ( 808823...246322 )
by Andrii
02:33
created

GitLogParser::fetchGitLog()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 17
cp 0
rs 8.8571
cc 5
eloc 12
nc 6
nop 1
crap 30
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
0 ignored issues
show
Bug introduced by
There is one abstract method parseLines in this class; you could implement it, or declare this class as abstract.
Loading history...
22
{
23
    public function mergeTo($history)
24
    {
25
        $this->setHistory($history);
26
        $this->mergeGitLog();
27
    }
28
29
    public function mergeGitLog()
30
    {
31
        $log = $this->fetchGitLog();
32
        var_dump($log);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($log); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
33
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method mergeGitLog() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
34
        foreach ($log as $hash => $data) {
0 ignored issues
show
Unused Code introduced by
foreach ($log as $hash =...ata['commit'], true); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
35
            $this->getHistory()->addCommit($data['tag'], null, $data['commit'], true);
0 ignored issues
show
Bug introduced by
The method addCommit() does not seem to exist on object<hiqdev\chkipper\history\History>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        }
37
    }
38
39
    public function fetchGitLog($reverse = false)
0 ignored issues
show
Unused Code introduced by
The parameter $reverse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        exec("git log --date=short --pretty='format:%h %ad %s [%ae] %d'", $logs);
42
        $res = [];
43
        $tag = '';
44
        foreach ($logs as $log) {
0 ignored issues
show
Bug introduced by
The expression $logs of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
            if (!preg_match('/^(\w+) ([0-9-]+ .*? \[.*?\]) *\(?(.*?)\)?$/', $log, $m)) {
46
                throw new UnexpectedValueException('failed parse git log');
47
            }
48
            $tag = $this->matchTag($m[3]) ?: $tag;
49
            $res[$m[1]] = [
50
                'tag'    => isset($m[3]) ? $this->matchTag($m[3]) : null,
51
                'commit' => new Commit($m[1], $m[2]),
52
            ];
53
        }
54
55
        return $res;
56
    }
57
58
    /**
59
     * Finds first tag in given refs string.
60
     * @param string $str 
61
     * @return string
62
     */
63
    public function matchTag($str)
64
    {
65
        $refs = explode(', ', $str);
66
        foreach ($refs as $ref) {
67
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
68
                return $m[1];
69
            }
70
        }
71
72
        return null;
73
    }
74
}
75