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

History::removeCommitLinks()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 9
cp 0.7778
rs 8.8571
nc 4
cc 5
eloc 5
nop 1
crap 5.2742
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 1
        }
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 3
        }
162 3
    }
163
164 2
    public function getTags()
165
    {
166 2
        return $this->_tags;
167
    }
168
169
    /**
170
     * Adds given tags to the history.
171
     * @param Tag[] $tags
172
     * @param boolean $prependNotes default is append
173
     */
174 1
    public function addTags(array $tags, $prependNotes = false)
175
    {
176 1
        foreach ($tags as $name => $tag) {
177 1
            $this->addTag($tag, $prependNotes);
178 1
        }
179 1
    }
180
181 1
    public function setTags(array $tags)
182
    {
183 1
        $this->_tags = [];
184 1
        $this->addTags($tags);
185 1
    }
186
187
    /**
188
     * Returns tag by name.
189
     * Creates if not exists.
190
     * Returns first tag when given empty name.
191
     * @param string|Tag $tag tag name or tag object
192
     * @return Tag
193
     */
194 3
    public function findTag($tag)
195
    {
196 3
        if (!$tag) {
197 1
            $tag = reset($this->_tags) ?: $this->lastTag;
198 1
        }
199 3
        $name = $tag instanceof Tag ? $tag->getName() : $tag;
200 3
        if (!$this->hasTag($name)) {
201 3
            $this->_tags[$name] = new Tag($name);
202 3
        }
203
204 3
        return $this->_tags[$name];
205
    }
206
207 3
    public function hasTag($tag)
208
    {
209 3
        return isset($this->_tags[$tag]);
210
    }
211
212
    public function removeTag($name)
213
    {
214
        foreach ($this->_tags as $k => $tag) {
215
            if ($tag->getName() == $name) {
216
                unset($this->_tags[$k]);
217
218
                return;
219
            }
220
        }
221
    }
222
223
    /**
224
     * Adds tag.
225
     * @param Tag $tag
226
     * @param boolean $prependNotes default is append
227
     * @return Tag the added tag
228
     */
229 3
    public function addTag(Tag $tag, $prependNotes = false)
230
    {
231 3
        return $this->findTag($tag->getName())->setDate($tag->getDate())->addNotes($tag->getNotes(), $prependNotes);
232
    }
233
234
    /**
235
     * Merges given history into the current.
236
     * @param History $history
237
     * @param boolean $prependNotes default is append
238
     */
239
    public function merge(History $history, $prependNotes = false)
240
    {
241
        $this->mergeTags($history->getTags(), $prependNotes);
242
        $this->addLinks($history->getLinks());
243
        $this->addHashes($history->getHashes());
244
    }
245
246
    /**
247
     * Merge given tags into the current history.
248
     * @param Tag[] $tags
249
     * @param boolean $prependNotes default is append
250
     */
251
    public function mergeTags(array $tags, $prependNotes = false)
252
    {
253
        foreach ($tags as $tag) {
254
            foreach ($tag->getNotes() as $note) {
255
                $note->removeCommits($this->getHashes());
256
            }
257
        }
258
        $this->addTags($tags, $prependNotes);
259
        #$olds = $this->getTags();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
260
        #$this->_tags = $tags;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
261
        #$this->addTags($$olds);
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
262
    }
263
264
    /**
265
     * Normalizes the history.
266
     */
267 2
    public function normalize($options = [])
268
    {
269
        static $defaults = [
270
            'removeEmptyFirstTag' => [],
271
            'addInitTag'          => [],
272
            'setTagDates'         => [],
273
            'addCommitLinks'      => [],
274
            'removeCommitLinks'   => [],
275 2
        ];
276 2
        $options = array_merge($defaults, $options);
277 2
        foreach ($options as $func => $args) {
278 2
            if (is_array($args)) {
279 2
                call_user_func_array([$this, $func], $args);
280 2
            }
281 2
        }
282 2
    }
283
284
    /**
285
     * Removes first tag if it is empty: has no notes and no commits.
286
     */
287 2
    public function removeEmptyFirstTag()
288
    {
289 2
        $tag = $this->getFirstTag();
290 2
        $notes = $tag->getNotes();
291 2
        if (count($notes) > 1) {
292
            return;
293
        }
294 2
        if (count($notes) > 0) {
295 2
            $note = reset($notes);
296 2
            if ($note->getNote() || count($note->getCommits()) > 0) {
297 2
                return;
298
            }
299
        }
300
        $this->removeTag($tag->getName());
301
    }
302
303
    /**
304
     * Adds init tag with oldest commit date.
305
     */
306 2
    public function addInitTag()
307
    {
308 2
        if (!$this->hasTag($this->initTag)) {
309 1
            $min = '';
310 1
            foreach ($this->getTags() as $tag) {
311 1
                foreach ($tag->getNotes() as $note) {
312 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...
313 1
                        $date = $commit->getDate();
314 1
                        if (!$min || strcmp($date, $min) < 0) {
315 1
                            $min = $date;
316 1
                        }
317 1
                    }
318 1
                }
319 1
            }
320 1
            if ($min) {
321 1
                $this->addTag(new Tag($this->initTag, $min));
322 1
            }
323 1
        }
324 2
    }
325
326
    /**
327
     * Normalizes dates to all the tags.
328
     * Drops date for the last tag and sets for others.
329
     */
330 2
    public function setTagDates()
331
    {
332 2
        foreach ($this->getTags() as $tag) {
333 2
            if ($tag->getName() === $this->lastTag) {
334 2
                $tag->unsetDate();
335 2
            } elseif (!$tag->getDate()) {
336
                $tag->setDate($tag->findDate());
337
            }
338 2
        }
339 2
    }
340
341
    /**
342
     * Adds links for commits not having ones.
343
     */
344 2
    public function addCommitLinks()
345
    {
346 2
        foreach ($this->getHashes() as $hash) {
347 2
            if (!$this->hasLink($hash)) {
348 1
                $this->addLink($hash, $this->generateHashHref($hash));
349 1
            }
350 2
        }
351 2
    }
352
353 1
    public function generateHashHref($hash)
354
    {
355 1
        $project = $this->getProject();
356
357 1
        return "https://github.com/$project/commit/$hash";
358
    }
359
360
    /**
361
     * Removes commit links that are not present in the history.
362
     */
363 2
    public function removeCommitLinks($all = false)
364
    {
365 2
        foreach ($this->getLinks() as $link => $href) {
366 2
            if (preg_match('/^[0-9a-f]{7}$/', $link)) {
367 2
                if ($all || !$this->hasHash($link)) {
368
                    $this->removeLink($link);
369
                }
370 2
            }
371 2
        }
372 2
    }
373
}
374