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

GitMerger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 52
ccs 0
cts 39
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeTo() 0 5 1
A mergeGitLog() 0 7 2
B fetchGitLog() 0 18 5
A matchTag() 0 11 3
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 merger.
18
 * Merges git log into History.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class GitMerger extends Builder
23
{
24
    public function mergeTo($history)
25
    {
26
        $this->setHistory($history);
0 ignored issues
show
Bug introduced by
The method setHistory() does not seem to exist on object<hiqdev\chkipper\history\GitMerger>.

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...
27
        $this->mergeGitLog();
28
    }
29
30
    public function mergeGitLog()
31
    {
32
        $log = $this->fetchGitLog();
33
        foreach ($log as $hash => $data) {
34
            $this->getHistory()->prependCommit($data['tag'], null, $data['commit']);
0 ignored issues
show
Bug introduced by
The method prependCommit() 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...
35
        }
36
    }
37
38
    public function fetchGitLog($reverse = false)
39
    {
40
        $reverse = $reverse ? '--reverse' : '';
41
        exec("git log $reverse --date=short --pretty='format:%h %ad %s [%ae] %d'", $logs);
42
        $res = [];
43
        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...
44
            if (!preg_match('/^(\w+) ([0-9-]+ .*? \[.*?\]) \(?(.*?)\)?$/', $log, $m)) {
45
                throw new UnexpectedValueException('failed parse git log');
46
            }
47
            var_dump($m);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($m); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
48
            $res[$m[1]] = [
49
                'tag'    => isset($m[3]) ? $this->matchTag($m[3]) : null,
50
                'commit' => new Commit($m[1], $m2[2]),
0 ignored issues
show
Bug introduced by
The variable $m2 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
51
            ];
52
        }
53
54
        return $res;
55
    }
56
57
    /**
58
     * Finds first tag in given refs string.
59
     * @param string $str 
60
     * @return string
61
     */
62
    public function matchTag($str)
63
    {
64
        $refs = explode(', ', $str);
65
        foreach ($refs as $ref) {
66
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
67
                return $m[1];
68
            }
69
        }
70
71
        return null;
72
    }
73
}
74