History::isLastTag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
46 2
    public function isInitTag($tag)
47
    {
48 2
        return $tag === $this->initTag;
49
    }
50
51 2
    public function isLastTag($tag)
52
    {
53 2
        return $tag === $this->lastTag;
54
    }
55
56 1
    public function setProject($value)
57
    {
58 1
        $this->_project = $value;
59 1
    }
60
61 2
    public function getProject()
62
    {
63 2
        if ($this->_project === null) {
64 1
            $this->_project = $this->_config->getName() ?: $this->detectProject();
65 1
        }
66
67 2
        return $this->_project;
68
    }
69
70 1
    public function detectProject()
71
    {
72 1
        foreach ($this->getHeaders() as $line) {
73 1
            if (preg_match('/\b([a-z0-9._-]{2,}\/[a-z0-9._-]{2,})\b/i', $line, $m)) {
74 1
                return $m[1];
75
            }
76
        }
77
    }
78
79 2
    public function addHeader($str)
80
    {
81 2
        $this->_headers[$str] = $str;
82 2
    }
83
84 1
    public function addHeaders(array $headers)
85
    {
86 1
        foreach ($headers as $header) {
87 1
            $this->addHeader($header);
88 1
        }
89 1
    }
90
91 1
    public function setHeaders(array $headers)
92
    {
93 1
        $this->_headers = [];
94 1
        $this->addHeaders($headers);
95 1
    }
96
97 2
    public function getHeaders()
98
    {
99 2
        return $this->_headers;
100
    }
101
102 2
    public function hasLink($link)
103
    {
104 2
        return isset($this->_links[$link]);
105
    }
106
107
    public function removeLink($link)
108
    {
109
        unset($this->_links[$link]);
110
    }
111
112 3
    public function addLink($link, $href)
113
    {
114 3
        $this->_links[$link] = $href;
115 3
    }
116
117
    public function unshiftLink($link, $href)
118
    {
119
        $this->_links = [$link => $href] + $this->_links;
120
    }
121
122
    public function addLinks(array $links)
123
    {
124
        foreach ($links as $link => $href) {
125
            $this->addLink($link, $href);
126
        }
127
    }
128
129 1
    public function setLinks(array $links)
130
    {
131 1
        $this->_links = $links;
132 1
    }
133
134 2
    public function getLinks()
135
    {
136 2
        return $this->_links;
137
    }
138
139 2
    public function hasHash($hash)
140
    {
141 2
        return isset($this->_hashes[(string) $hash]);
142
    }
143
144 3
    public function addHash($hash)
145
    {
146 3
        $this->_hashes[(string) $hash] = $hash;
147 3
    }
148
149
    public function addHashes(array $hashes)
150
    {
151
        foreach ($hashes as $hash) {
152
            $this->addHash($hash);
153
        }
154
    }
155
156
    public function setHashes(array $hashes)
157
    {
158
        $this->_hashes = [];
159
        $this->addHashes($hashes);
160
    }
161
162 2
    public function getHashes()
163
    {
164 2
        return $this->_hashes;
165
    }
166
167 2
    public function getFirstTag()
168
    {
169 2
        return reset($this->_tags);
170
    }
171
172
    public function setFirstTag($name)
173
    {
174
        $this->getFirstTag()->setName($name);
175
    }
176
177 3
    public function countTags()
178
    {
179 3
        return count($this->_tags);
180
    }
181
182 3
    public function initTags()
183
    {
184 3
        if (!$this->countTags()) {
185 3
            $this->addTag(new Tag($this->lastTag));
186 3
        }
187 3
    }
188
189 2
    public function getTags()
190
    {
191 2
        return $this->_tags;
192
    }
193
194
    /**
195
     * Adds given tags to the history.
196
     * @param Tag[] $tags
197
     * @param boolean $prependNotes default is append
198
     */
199 1
    public function addTags(array $tags, $prependNotes = false)
200
    {
201 1
        foreach ($tags as $name => $tag) {
202 1
            $this->addTag($tag, $prependNotes);
203 1
        }
204 1
    }
205
206 1
    public function setTags(array $tags)
207
    {
208 1
        $this->_tags = [];
209 1
        $this->addTags($tags);
210 1
    }
211
212
    /**
213
     * Returns tag by name.
214
     * Creates if not exists.
215
     * Returns first tag when given empty name.
216
     * @param string|Tag $tag tag name or tag object
217
     * @return Tag
218
     */
219 3
    public function findTag($tag)
220
    {
221 3
        if (!$tag) {
222 1
            $tag = reset($this->_tags) ?: $this->lastTag;
223 1
        }
224 3
        $name = $tag instanceof Tag ? $tag->getName() : $tag;
225 3
        if (!$this->hasTag($name)) {
226 3
            $this->_tags[$name] = new Tag($name);
227 3
        }
228
229 3
        return $this->_tags[$name];
230
    }
231
232 3
    public function hasTag($tag)
233
    {
234 3
        return isset($this->_tags[$tag]);
235
    }
236
237
    public function removeTag($name)
238
    {
239
        foreach ($this->_tags as $k => $tag) {
240
            if ($tag->getName() === $name) {
241
                unset($this->_tags[$k]);
242
243
                return;
244
            }
245
        }
246
    }
247
248
    /**
249
     * Adds tag.
250
     * @param Tag $tag
251
     * @param boolean $prependNotes default is append
252
     * @return Tag the added tag
253
     */
254 3
    public function addTag(Tag $tag, $prependNotes = false)
255
    {
256 3
        return $this->findTag($tag->getName())->setDate($tag->getDate())->addNotes($tag->getNotes(), $prependNotes);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findTag($tag->get...Notes(), $prependNotes) targeting hiqdev\chkipper\lib\Tag::addNotes() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
257
    }
258
259
    /**
260
     * Merges given history into the current.
261
     * @param History $history
262
     * @param boolean $prependNotes default is append
263
     */
264
    public function merge(History $history, $prependNotes = false)
265
    {
266
        $this->mergeTags($history->getTags(), $prependNotes);
267
        $this->addLinks($history->getLinks());
268
        $this->addHashes($history->getHashes());
269
    }
270
271
    /**
272
     * Merge given tags into the current history.
273
     * @param Tag[] $tags
274
     * @param boolean $prependNotes default is append
275
     */
276
    public function mergeTags(array $tags, $prependNotes = false)
277
    {
278
        foreach ($tags as $tag) {
279
            foreach ($tag->getNotes() as $note) {
280
                $note->removeCommits($this->getHashes());
281
            }
282
        }
283
        $this->addTags($tags, $prependNotes);
284
        //$olds = $this->getTags();
285
        //$this->_tags = $tags;
286
        //$this->addTags($$olds);
287
    }
288
}
289