Completed
Push — master ( 13f9c8...64a1ae )
by Andrii
02:43
created

History::addGitLog()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.2
cc 4
eloc 7
nc 6
nop 0
crap 20
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
    protected $_headers = [];
23
    protected $_hashes  = [];
24
    protected $_links   = [];
25
    protected $_tags    = [];
26
27 2
    public function addHeader($str)
28
    {
29 2
        $this->_headers[$str] = $str;
30 2
    }
31
32 1
    public function addHeaders(array $value)
33
    {
34 1
        foreach ($value as $header) {
35 1
            $this->addHeader($header);
36 1
        }
37 1
    }
38
39 1
    public function setHeaders(array $value)
40
    {
41 1
        $this->_headers = [];
42 1
        $this->addHeaders($value);
43 1
    }
44
45 1
    public function getHeaders()
46
    {
47 1
        return $this->_headers;
48
    }
49
50 2
    public function addLink($link, $href)
51
    {
52 2
        $this->_links[$link] = $href;
53 2
    }
54
55 1
    public function setLinks($value)
56
    {
57 1
        $this->_links = $value;
58 1
    }
59
60 1
    public function getLinks()
61
    {
62 1
        return $this->_links;
63
    }
64
65 1
    public function getTags()
66
    {
67 1
        return $this->_tags;
68
    }
69
70 1
    public function setTags(array $value)
71
    {
72 1
        $this->_tags = $value;
73 1
    }
74
75 2
    public function findTag($tag)
76
    {
77 2
        if (!isset($this->_tags[$tag])) {
78 2
            $this->_tags[$tag] = new Tag($tag);
79 2
        }
80
81 2
        return $this->_tags[$tag];
82
    }
83
84
    public function hasTag($tag)
85
    {
86
        return array_key_exists($tag, $this->_tags);
87
    }
88
89 2
    public function addTag($tag, $date = null)
90
    {
91 2
        $this->findTag($tag)->setDate($date);
92 2
    }
93
94 2
    public function addNote($tag, $note)
95
    {
96 2
        $this->findTag($tag)->findNote($note);
97 2
    }
98
99
    public function hasHash($hash)
100
    {
101
        return array_key_exists((string) $hash, $this->_hashes);
102
    }
103
104 2
    public function addHash($hash, $label)
105
    {
106 2
        $this->_hashes[(string) $hash] = $label;
107 2
    }
108
109 2
    public function addCommit($tag, $note, $hash, $label = null)
110
    {
111 2
        $this->addHash($hash, $label);
112 2
        $this->findTag($tag)->findNote($note)->findCommit($hash)->setLabel($label);
113 2
    }
114
115 2
    public function addComment($tag, $note, $hash, $text = null)
116
    {
117 2
        $this->findTag($tag)->findNote($note)->findCommit($hash)->addComment($text);
118 2
    }
119
120
    public function addHistory($commit, $front = false)
121
    {
122
        $tag    = $commit['tag'];
123
        $note   = $commit['note'];
124
        $hash   = $commit['hash'];
125
        $render = static::renderCommit($commit);
126
        $hashes = &$this->_tags[$tag][$note];
127
        $hashes = (array) $hashes;
128
        if ($front) {
129
            $hashes = [$hash => [$render]] + $hashes;
130
        } else {
131
            $hashes[$hash][] = $render;
132
        }
133
    }
134
135
    public function addGitLog()
136
    {
137
        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...
138
            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...
139
                continue;
140
            }
141
            $this->addHistory($commit, true);
142
        }
143
        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...
144
            $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...
145
        }
146
    }
147
148
    public function render($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data 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...
149
    {
150
        $res = static::renderHeader('commits history');
151
152
        foreach ($this->_tags as $tag => $notes) {
153
            $prev = $res;
154
            $tag  = static::arrayPop($notes, 'tag') ?: $tag;
155
            $new  = static::arrayPop($notes, '') ?: [];
156
            $res .= static::renderTag($tag);
157
            $save = $res;
158
            foreach ($new as $hash => $lines) {
159
                $res .= static::renderLines($lines);
160
            }
161
            foreach ($notes as $note => $cs) {
162
                $note = static::arrayPop($cs, 'note');
163
                $res .= static::renderNote($note);
164
                foreach ($cs as $hash => $lines) {
165
                    $res .= static::renderLines($lines);
166
                }
167
            }
168
            if ($save === $res && stripos($tag, static::getVcs()->lastTag) !== false) {
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...
169
                $res = $prev;
170
                unset($this->_tags[$tag]);
171
            }
172
        }
173
174
        return $res;
175
    }
176
177
    public static function arrayPop(&$array, $key)
178
    {
179
        $res = $array[$key];
180
        unset($array[$key]);
181
182
        return $res;
183
    }
184
185
    public static function renderNote($note)
186
    {
187
        return "- $note\n";
188
    }
189
190
    public static function renderLines(array $lines)
191
    {
192
        $res = implode("\n", array_filter($lines));
193
194
        return $res ? $res . "\n" : '';
195
    }
196
197
    public static function renderCommit($commit)
198
    {
199
        return static::skipCommit($commit) ? '' : "    - $commit[hash] $commit[date] $commit[comment] ($commit[email])";
200
    }
201
202
    public static function skipCommit($commit)
203
    {
204
        $comment = $commit['comment'];
205
206
        static $equals = [
207
            ''      => 1,
208
            'minor' => 1,
209
        ];
210
        if ($equals[$comment]) {
211
            return true;
212
        }
213
214
        static $starts = [
215
            'version bump',
216
            'bumped version',
217
            "merge branch 'master'",
218
        ];
219
        foreach ($starts as $start) {
220
            if (strtolower(substr($comment, 0, strlen($start))) === $start) {
221
                return true;
222
            }
223
        }
224
225
        return false;
226
    }
227
228
    public static function renderTag($tag)
229
    {
230
        if (strpos($tag, ' ') === false || $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...
231
            $tag .= static::renderDate(static::getVcs()->tags[$tag]);
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...
232
        }
233
234
        return "\n## $tag\n\n";
235
    }
236
237
    public static function renderDate($date)
238
    {
239
        return $date ? date(' Y-m-d', strtotime($date)) : '';
240
    }
241
242
    public static function renderHeader($string)
0 ignored issues
show
Unused Code introduced by
The parameter $string 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...
243
    {
244
        $res = '';
245
        foreach ($this->_headers as $str) {
0 ignored issues
show
Bug introduced by
The variable $this 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...
246
            $res .= $str . "\n";
247
        }
248
249
        return $res;
250
    }
251
}
252