Completed
Push — master ( 808823...246322 )
by Andrii
02:33
created

History::getHashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 with additional information:
17
 * - list of headers
18
 * - list of hashes
19
 * - list of links
20
 * - list of tags
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 $_headers = [];
31
    protected $_hashes  = [];
32
    protected $_links   = [];
33
    protected $_tags    = [];
34
35 2
    public function addHeader($str)
36
    {
37 2
        $this->_headers[$str] = $str;
38 2
    }
39
40 1
    public function addHeaders(array $headers)
41
    {
42 1
        foreach ($headers as $header) {
43 1
            $this->addHeader($header);
44 1
        }
45 1
    }
46
47 1
    public function setHeaders(array $headers)
48
    {
49 1
        $this->_headers = [];
50 1
        $this->addHeaders($headers);
51 1
    }
52
53 1
    public function getHeaders()
54
    {
55 1
        return $this->_headers;
56
    }
57
58 2
    public function addLink($link, $href)
59
    {
60 2
        $this->_links[$link] = $href;
61 2
    }
62
63 1
    public function setLinks(array $links)
64
    {
65 1
        $this->_links = $links;
66 1
    }
67
68 1
    public function getLinks()
69
    {
70 1
        return $this->_links;
71
    }
72
73
    public function hasHash($hash)
74
    {
75
        return isset($this->_hashes[(string) $hash]);
76
    }
77
78 2
    public function addHash($hash)
79
    {
80 2
        $this->_hashes[(string) $hash] = $hash;
81 2
    }
82
83 1
    public function addHashes(array $hashes)
84
    {
85 1
        foreach ($hashes as $hash) {
86
            $this->addHash($hash);
87
        }
88
    }
89
90
    public function setHashes(array $hashes)
91
    {
92
        $this->_hashes = [];
93
        $this->addHashes($hashes);
94
    }
95
96
    public function getHashes()
97
    {
98
        return $this->_hashes;
99
    }
100
101
    public function getFirstTag()
102
    {
103
        return reset($this->_tags);
104
    }
105
106 2
    public function countTags()
107
    {
108 2
        return count($this->_tags);
109
    }
110
111 2
    public function initTags()
112
    {
113 2
        if (!$this->countTags()) {
114 2
            $this->addTag($this->lastTag);
115 2
        }
116 2
    }
117
118 1
    public function getTags()
119
    {
120 1
        return $this->_tags;
121
    }
122
123 1
    public function setTags(array $value)
124
    {
125 1
        $this->_tags = $value;
126 1
    }
127
128
    /**
129
     * Returns tag by name.
130
     * Creates if not exists.
131
     * Returns first tag when given empty name.
132
     * @param string|Tag $tag tag or tag name
133
     * @return Tag
134
     */
135 2
    public function findTag($tag, $pre = false)
0 ignored issues
show
Unused Code introduced by
The parameter $pre 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...
136
    {
137 2
        if (!$tag) {
138
            $tag = reset($this->_tags) ?: $this->lastTag;
139
        }
140 2
        $name = $tag instanceof Tag ? $tag->getName() : $tag;
141 2
        if (!$this->hasTag($name)) {
142 2
            $this->_tags[$name] = new Tag($name);
143 2
        }
144
145 2
        return $this->_tags[$name];
146
    }
147
148 2
    public function hasTag($tag)
149
    {
150 2
        return isset($this->_tags[$tag]);
151
    }
152
153 2
    public function addTag($tag, $date = null)
154
    {
155 2
        $this->findTag($tag)->setDate($date);
156 2
    }
157
158 2
    public function findNote($tag, $note)
159
    {
160 2
        $this->findTag($tag)->findNote($note);
161 2
    }
162
163 2
    public function findCommit($tag, $note, $hash, $pre = false)
164
    {
165 2
        $this->addHash($hash);
166
        return $this->findTag($tag, $pre)->findNote($note, $pre)->findCommit($hash, $pre);
0 ignored issues
show
Unused Code introduced by
The call to Tag::findNote() has too many arguments starting with $pre.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
167 2
    }
168
169
    public function addHistory($commit, $front = false)
170
    {
171
        $tag    = $commit['tag'];
172
        $note   = $commit['note'];
173
        $hash   = $commit['hash'];
174
        $render = static::renderCommit($commit);
0 ignored issues
show
Bug introduced by
The method renderCommit() 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...
175
        $hashes = &$this->_tags[$tag][$note];
176
        $hashes = (array) $hashes;
177
        if ($front) {
178
            $hashes = [$hash => [$render]] + $hashes;
179
        } else {
180
            $hashes[$hash][] = $render;
181
        }
182
    }
183
184
    public function addGitLog()
185
    {
186
        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...
187
            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...
188
                continue;
189
            }
190
            $this->addHistory($commit, true);
191
        }
192
        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...
193
            $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...
194
        }
195
    }
196
}
197