Completed
Push — master ( 64a1ae...3862ca )
by Andrii
09:18
created

History::renderCommit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
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
/**
15
 * History class.
16
 * Holds history of commits.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class History
21
{
22
    public $lastTag = 'Under development';
23
24
    public $initTag = 'Development started';
25
26
    protected $_headers = [];
27
    protected $_hashes  = [];
28
    protected $_links   = [];
29
    protected $_tags    = [];
30
31 2
    public function addHeader($str)
32
    {
33 2
        $this->_headers[$str] = $str;
34 2
    }
35
36 1
    public function addHeaders(array $value)
37
    {
38 1
        foreach ($value as $header) {
39 1
            $this->addHeader($header);
40 1
        }
41 1
    }
42
43 1
    public function setHeaders(array $value)
44
    {
45 1
        $this->_headers = [];
46 1
        $this->addHeaders($value);
47 1
    }
48
49 1
    public function getHeaders()
50
    {
51 1
        return $this->_headers;
52
    }
53
54 2
    public function addLink($link, $href)
55
    {
56 2
        $this->_links[$link] = $href;
57 2
    }
58
59 1
    public function setLinks($value)
60
    {
61 1
        $this->_links = $value;
62 1
    }
63
64 1
    public function getLinks()
65
    {
66 1
        return $this->_links;
67
    }
68
69
    public function getFirstTag()
70
    {
71
        return reset($this->_tags);
72
    }
73
74 1
    public function getTags()
75
    {
76 1
        return $this->_tags;
77
    }
78
79 1
    public function setTags(array $value)
80
    {
81 1
        $this->_tags = $value;
82 1
    }
83
84
    /**
85
     * Returns tag by name.
86
     * Creates if not exists.
87
     * Returns first tag when given empty name.
88
     * @param string|Tag $tag tag or tag name
89
     * @return Tag
90
     */
91 2
    public function findTag($tag)
92
    {
93 2
        if (!$tag) {
94
            $tag = reset($this->_tags) ?: $this->lastTag;
95
        }
96 2
        $name = $tag instanceof Tag ? $tag->getName() : $tag;
97 2
        if (!$this->hasTag($name)) {
98 2
            $this->_tags[$name] = new Tag($name);
99 2
        }
100
101 2
        return $this->_tags[$name];
102
    }
103
104 2
    public function hasTag($tag)
105
    {
106 2
        return array_key_exists($tag, $this->_tags);
107
    }
108
109 2
    public function addTag($tag, $date = null)
110
    {
111 2
        $this->findTag($tag)->setDate($date);
112 2
    }
113
114 2
    public function addNote($tag, $note)
115
    {
116 2
        $this->findTag($tag)->findNote($note);
117 2
    }
118
119
    public function hasHash($hash)
120
    {
121
        return array_key_exists((string) $hash, $this->_hashes);
122
    }
123
124 2
    public function addHash($hash, $label)
125
    {
126 2
        $this->_hashes[(string) $hash] = $label;
127 2
    }
128
129 2
    public function addCommit($tag, $note, $hash, $label = null)
130
    {
131 2
        $this->addHash($hash, $label);
132 2
        $this->findTag($tag)->findNote($note)->findCommit($hash)->setLabel($label);
133 2
    }
134
135 2
    public function addComment($tag, $note, $hash, $text = null)
136
    {
137 2
        $this->findTag($tag)->findNote($note)->findCommit($hash)->addComment($text);
138 2
    }
139
140
    public function addHistory($commit, $front = false)
141
    {
142
        $tag    = $commit['tag'];
143
        $note   = $commit['note'];
144
        $hash   = $commit['hash'];
145
        $render = static::renderCommit($commit);
0 ignored issues
show
Bug introduced by
The method renderCommit() 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...
146
        $hashes = &$this->_tags[$tag][$note];
147
        $hashes = (array) $hashes;
148
        if ($front) {
149
            $hashes = [$hash => [$render]] + $hashes;
150
        } else {
151
            $hashes[$hash][] = $render;
152
        }
153
    }
154
155
    public function addGitLog()
156
    {
157
        foreach (array_reverse(static::getVcs()->commits, true) as $hash => $commit) {
0 ignored issues
show
Bug introduced by
The method getVcs() 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...
158
            if ($this->hasCommit($hash)) {
0 ignored issues
show
Bug introduced by
The method hasCommit() 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...
159
                continue;
160
            }
161
            $this->addHistory($commit, true);
162
        }
163
        if (!$this->hasHistory(static::getVcs()->initTag)) {
0 ignored issues
show
Bug introduced by
The method getVcs() 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...
Bug introduced by
The method hasHistory() 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...
164
            $this->addHistory(['tag' => static::getVcs()->initTag]);
0 ignored issues
show
Bug introduced by
The method getVcs() 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...
165
        }
166
    }
167
168
    public static function skipCommit($commit)
169
    {
170
        $comment = $commit['comment'];
171
172
        static $equals = [
173
            ''      => 1,
174
            'minor' => 1,
175
        ];
176
        if ($equals[$comment]) {
177
            return true;
178
        }
179
180
        static $starts = [
181
            'version bump',
182
            'bumped version',
183
            "merge branch 'master'",
184
        ];
185
        foreach ($starts as $start) {
186
            if (strtolower(substr($comment, 0, strlen($start))) === $start) {
187
                return true;
188
            }
189
        }
190
191
        return false;
192
    }
193
194
}
195