Completed
Push — master ( 90d35b...b28524 )
by Andrii
05:54
created

History::unshiftLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

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