Completed
Push — master ( 7cde6a...69f712 )
by Andrii
06:00
created

History::removeCommitLinks()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 5
nc 4
nop 1
crap 5
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
 *
17
 * @property array $headers: header => header
18
 * @property array $hashes:  hash => hash
19
 * @property array $links:   link => href
20
 * @property array $tags:    tag name => tag object
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class History
25
{
26
    public $lastTag = 'Under development';
27
28
    public $initTag = 'Development started';
29
30
    protected $_project;
31
    protected $_headers = [];
32
    protected $_hashes  = [];
33
    protected $_links   = [];
34
    protected $_tags    = [];
35
36 1
    public function setProject($value)
37
    {
38 1
        $this->_project = $value;
39 1
    }
40
41 1
    public function getProject()
42
    {
43 1
        if ($this->_project === null) {
44
            $this->_project = $this->detectProject();
45
        }
46
47 1
        return $this->_project;
48
    }
49
50
    public function detectProject()
51
    {
52
        foreach ($this->getHeaders() as $line) {
53
            if (preg_match('/\b([a-z0-9._-]{2,}\/[a-z0-9._-]{2,})\b/i', $line, $m)) {
54
                return $m[1];
55
            }
56
        }
57
    }
58
59 2
    public function addHeader($str)
60
    {
61 2
        $this->_headers[$str] = $str;
62 2
    }
63
64 1
    public function addHeaders(array $headers)
65
    {
66 1
        foreach ($headers as $header) {
67 1
            $this->addHeader($header);
68
        }
69 1
    }
70
71 1
    public function setHeaders(array $headers)
72
    {
73 1
        $this->_headers = [];
74 1
        $this->addHeaders($headers);
75 1
    }
76
77 2
    public function getHeaders()
78
    {
79 2
        return $this->_headers;
80
    }
81
82 2
    public function hasLink($link)
83
    {
84 2
        return isset($this->_links[$link]);
85
    }
86
87
    public function removeLink($link)
88
    {
89
        unset($this->_links[$link]);
90
    }
91
92 3
    public function addLink($link, $href)
93
    {
94 3
        $this->_links[$link] = $href;
95 3
    }
96
97
    public function addLinks(array $links)
98
    {
99
        foreach ($links as $link => $href) {
100
            $this->addLink($link, $href);
101
        }
102
    }
103
104 1
    public function setLinks(array $links)
105
    {
106 1
        $this->_links = $links;
107 1
    }
108
109 2
    public function getLinks()
110
    {
111 2
        return $this->_links;
112
    }
113
114 2
    public function hasHash($hash)
115
    {
116 2
        return isset($this->_hashes[(string) $hash]);
117
    }
118
119 3
    public function addHash($hash)
120
    {
121 3
        $this->_hashes[(string) $hash] = $hash;
122 3
    }
123
124
    public function addHashes(array $hashes)
125
    {
126
        foreach ($hashes as $hash) {
127
            $this->addHash($hash);
128
        }
129
    }
130
131
    public function setHashes(array $hashes)
132
    {
133
        $this->_hashes = [];
134
        $this->addHashes($hashes);
135
    }
136
137 2
    public function getHashes()
138
    {
139 2
        return $this->_hashes;
140
    }
141
142 2
    public function getFirstTag()
143
    {
144 2
        return reset($this->_tags);
145
    }
146
147
    public function setFirstTag($name)
148
    {
149
        $this->getFirstTag()->setName($name);
150
    }
151
152 3
    public function countTags()
153
    {
154 3
        return count($this->_tags);
155
    }
156
157 3
    public function initTags()
158
    {
159 3
        if (!$this->countTags()) {
160 3
            $this->addTag(new Tag($this->lastTag));
161
        }
162 3
    }
163
164 2
    public function getTags()
165
    {
166 2
        return $this->_tags;
167
    }
168
169 1
    public function addTags(array $tags)
170
    {
171 1
        foreach ($tags as $name => $tag) {
172 1
            $this->addTag($tag);
173
        }
174 1
    }
175
176 1
    public function setTags(array $tags)
177
    {
178 1
        $this->_tags = [];
179 1
        $this->addTags($tags);
180 1
    }
181
182
    /**
183
     * Returns tag by name.
184
     * Creates if not exists.
185
     * Returns first tag when given empty name.
186
     * @param string|Tag $tag tag or tag name
187
     * @return Tag
188
     */
189 3
    public function findTag($tag)
190
    {
191 3
        if (!$tag) {
192 1
            $tag = reset($this->_tags) ?: $this->lastTag;
193
        }
194 3
        $name = $tag instanceof Tag ? $tag->getName() : $tag;
195 3
        if (!$this->hasTag($name)) {
196 3
            $this->_tags[$name] = new Tag($name);
197
        }
198
199 3
        return $this->_tags[$name];
200
    }
201
202 3
    public function hasTag($tag)
203
    {
204 3
        return isset($this->_tags[$tag]);
205
    }
206
207
    public function removeTag($name)
208
    {
209
        foreach ($this->_tags as $k => $tag) {
210
            if ($tag->getName() == $name) {
211
                unset($this->_tags[$k]);
212
213
                return;
214
            }
215
        }
216
    }
217
218 3
    public function addTag(Tag $tag)
219
    {
220 3
        return $this->findTag($tag->getName())->setDate($tag->getDate())->addNotes($tag->getNotes());
221
    }
222
223
    /**
224
     * Merges given history into the current.
225
     * @param History $history
226
     */
227
    public function merge(History $history)
228
    {
229
        $this->mergeTags($history->getTags());
230
        $this->addLinks($history->getLinks());
231
        $this->addHashes($history->getHashes());
232
    }
233
234
    /**
235
     * Merge given tags into the current history.
236
     * @param Tag[] $tags
237
     */
238
    public function mergeTags(array $tags)
239
    {
240
        foreach ($tags as $tag) {
241
            foreach ($tag->getNotes() as $note) {
242
                $note->removeCommits($this->getHashes());
243
            }
244
        }
245
        $olds = $this->getTags();
246
        $this->_tags = $tags;
247
        foreach ($olds as $tag) {
248
            $this->addTag($tag);
249
        }
250
    }
251
252
    /**
253
     * Normalizes the history.
254
     */
255 2
    public function normalize($options = [])
256
    {
257 2
        static $defaults = [
258
            'removeEmptyFirstTag' => [],
259
            'addInitTag'          => [],
260
            'setTagDates'         => [],
261
            'addCommitLinks'      => [],
262
            'removeCommitLinks'   => [],
263
        ];
264 2
        $options = array_merge($defaults, $options);
265 2
        foreach ($options as $func => $args) {
266 2
            if (is_array($args)) {
267 2
                call_user_func_array([$this, $func], $args);
268
            }
269
        }
270 2
    }
271
272
    /**
273
     * Removes first tag if it is empty: has no notes and no commits.
274
     */
275 2
    public function removeEmptyFirstTag()
276
    {
277 2
        $tag = $this->getFirstTag();
278 2
        $notes = $tag->getNotes();
279 2
        if (count($notes) > 1) {
280
            return;
281
        }
282 2
        if (count($notes) > 0) {
283 2
            $note = reset($notes);
284 2
            if ($note->getNote() || count($note->getCommits()) > 0) {
285 2
                return;
286
            }
287
        }
288
        $this->removeTag($tag->getName());
289
    }
290
291
    /**
292
     * Adds init tag with oldest commit date.
293
     */
294 2
    public function addInitTag()
295
    {
296 2
        if (!$this->hasTag($this->initTag)) {
297 1
            $min = '';
298 1
            foreach ($this->getTags() as $tag) {
299 1
                foreach ($tag->getNotes() as $note) {
300 1 View Code Duplication
                    foreach ($note->getCommits() as $commit) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301 1
                        $date = $commit->getDate();
302 1
                        if (!$min || strcmp($date, $min)<0) {
303 1
                            $min = $date;
304
                        }
305
                    }
306
                }
307
            }
308 1
            if ($min) {
309 1
                $this->addTag(new Tag($this->initTag, $min));
310
            }
311
        }
312 2
    }
313
314
    /**
315
     * Normalizes dates to all the tags.
316
     * Drops date for the last tag and sets for others.
317
     */
318 2
    public function setTagDates()
319
    {
320 2
        foreach ($this->getTags() as $tag) {
321 2
            if ($tag->getName() === $this->lastTag) {
322 2
                $tag->unsetDate();
323 2
            } elseif (!$tag->getDate()) {
324 2
                $tag->setDate($tag->findDate());
325
            }
326
        }
327 2
    }
328
329
    /**
330
     * Adds links for commits not having ones.
331
     */
332 2
    public function addCommitLinks()
333
    {
334 2
        foreach ($this->getHashes() as $hash) {
335 2
            if (!$this->hasLink($hash)) {
336 2
                $this->addLink($hash, $this->generateHashHref($hash));
337
            }
338
        }
339 2
    }
340
341 1
    public function generateHashHref($hash)
342
    {
343 1
        $project = $this->getProject();
344
345 1
        return "https://github.com/$project/commit/$hash";
346
    }
347
348
    /**
349
     * Removes commit links that are not present in the history.
350
     */
351 2
    public function removeCommitLinks($all = false)
352
    {
353 2
        foreach ($this->getLinks() as $link => $href) {
354 2
            if (preg_match('/^[0-9a-f]{7}$/', $link)) {
355 2
                if ($all || !$this->hasHash($link)) {
356 2
                    $this->removeLink($link);
357
                }
358
            }
359
        }
360 2
    }
361
}
362