Completed
Push — master ( 69f712...a96098 )
by Andrii
02:39
created

GitLogParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 61.11%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 69
ccs 22
cts 36
cp 0.6111
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeTo() 0 5 1
A mergeGitLog() 0 9 2
A parseGitLog() 0 6 1
B parseLines() 0 21 6
A matchTag() 0 11 3
A matchNote() 0 4 2
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 mergeTo($history)
24
    {
25
        $this->setHistory($history);
26
        $this->mergeGitLog();
27
    }
28
29
    public function mergeGitLog()
30
    {
31
        $log = $this->fetchGitLog();
0 ignored issues
show
Bug introduced by
The method fetchGitLog() does not seem to exist on object<hiqdev\chkipper\history\GitLogParser>.

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...
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 exist on hiqdev\chkipper\history\History. Did you maybe mean addCommitLinks()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
36
        }
37
    }
38
39
    public function parseGitLog()
40
    {
41
        exec("git log --date=short --pretty='format:%h %ad %s [%ae] %d'", $logs);
42
43
        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...
44
    }
45
46 1
    public function parseLines(array $lines)
47
    {
48 1
        $this->getHistory()->initTags();
49
50 1
        foreach ($lines as $line) {
51 1
            if (!preg_match('/^(\w+) (([0-9-]+) (.*?) \[.*?\]) *\(?(.*?)\)?$/', $line, $m)) {
52
                throw new UnexpectedValueException('failed parse git line');
53
            }
54 1
            $tag  = $this->matchTag($m[5]);
55 1
            $note = $this->matchNote($m[4]);
56 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...
57 1
                $this->addTag($tag, $m[3]);
58 1
            }
59 1
            if (false && $note) { /// disabled adding notes
60
                $this->addNote($note);
61
            }
62 1
            $this->addCommit($m[1], $m[2]);
63 1
        }
64
65 1
        return $this->getHistory();
66
    }
67
68
    /**
69
     * Finds first tag in given refs string.
70
     * @param string $str 
71
     * @return string
72
     */
73 1
    public function matchTag($str)
74
    {
75 1
        $refs = explode(', ', $str);
76 1
        foreach ($refs as $ref) {
77 1
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
78 1
                return $m[1];
79
            }
80 1
        }
81
82 1
        return null;
83
    }
84
85 1
    public function matchNote($str)
86
    {
87 1
        return strpos($str, ' ') ? $str : null;
88
    }
89
}
90