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

History::addComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 4
crap 1
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.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class History
21
{
22
    public $lastTag = 'Under development';
23
24
    public $initTag = 'Development started';
25
26
    protected $_headers = [];
27
    protected $_hashes  = [];
28
    protected $_links   = [];
29
    protected $_tags    = [];
30
31 2
    public function addHeader($str)
32
    {
33 2
        $this->_headers[$str] = $str;
34 2
    }
35
36 1
    public function addHeaders(array $headers)
37
    {
38 1
        foreach ($headers as $header) {
39 1
            $this->addHeader($header);
40 1
        }
41 1
    }
42
43 1
    public function setHeaders(array $headers)
44
    {
45 1
        $this->_headers = [];
46 1
        $this->addHeaders($headers);
47 1
    }
48
49 1
    public function getHeaders()
50
    {
51 1
        return $this->_headers;
52
    }
53
54 2
    public function addLink($link, $href)
55
    {
56 2
        $this->_links[$link] = $href;
57 2
    }
58
59 1
    public function setLinks(array $links)
60
    {
61 1
        $this->_links = $links;
62 1
    }
63
64 1
    public function getLinks()
65
    {
66 1
        return $this->_links;
67
    }
68
69
    public function hasHash($hash)
70
    {
71
        return isset($this->_hashes[(string) $hash]);
72
    }
73
74 2
    public function addHash($hash)
75
    {
76 2
        $this->_hashes[(string) $hash] = $hash;
77 2
    }
78
79
    public function addHashes(array $hashes)
80
    {
81
        foreach ($hashes as $hash) {
82
            $this->addHash($hash);
83
        }
84
    }
85
86
    public function setHashes(array $hashes)
87
    {
88
        $this->_hashes = [];
89
        $this->addHashes($hashes);
90
    }
91
92
    public function getHashes()
93
    {
94
        return $this->_hashes;
95
    }
96
97
    public function getFirstTag()
98
    {
99
        return reset($this->_tags);
100
    }
101
102 1
    public function getTags()
103
    {
104 1
        return $this->_tags;
105
    }
106
107 1
    public function setTags(array $value)
108
    {
109 1
        $this->_tags = $value;
110 1
    }
111
112
    /**
113
     * Returns tag by name.
114
     * Creates if not exists.
115
     * Returns first tag when given empty name.
116
     * @param string|Tag $tag tag or tag name
117
     * @return Tag
118
     */
119 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...
120
    {
121 2
        if (!$tag) {
122
            $tag = reset($this->_tags) ?: $this->lastTag;
123
        }
124 2
        $name = $tag instanceof Tag ? $tag->getName() : $tag;
125 2
        if (!$this->hasTag($name)) {
126 2
            $this->_tags[$name] = new Tag($name);
127 2
        }
128
129 2
        return $this->_tags[$name];
130
    }
131
132 2
    public function hasTag($tag)
133
    {
134 2
        return isset($this->_tags[$tag]);
135
    }
136
137 2
    public function addTag($tag, $date = null)
138
    {
139 2
        $this->findTag($tag)->setDate($date);
140 2
    }
141
142 2
    public function findNote($tag, $note)
143
    {
144 2
        $this->findTag($tag)->findNote($note);
145 2
    }
146
147 2
    public function findCommit($tag, $note, $hash, $pre = false)
148
    {
149 2
        $this->addHash($hash);
150
        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...
151 2
    }
152
153
    public function addHistory($commit, $front = false)
154
    {
155
        $tag    = $commit['tag'];
156
        $note   = $commit['note'];
157
        $hash   = $commit['hash'];
158
        $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...
159
        $hashes = &$this->_tags[$tag][$note];
160
        $hashes = (array) $hashes;
161
        if ($front) {
162
            $hashes = [$hash => [$render]] + $hashes;
163
        } else {
164
            $hashes[$hash][] = $render;
165
        }
166
    }
167
168
    public function addGitLog()
169
    {
170
        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...
171
            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...
172
                continue;
173
            }
174
            $this->addHistory($commit, true);
175
        }
176
        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...
177
            $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...
178
        }
179
    }
180
181
    public static function skipCommit($commit)
182
    {
183
        $comment = $commit['comment'];
184
185
        static $equals = [
186
            ''      => 1,
187
            'minor' => 1,
188
        ];
189
        if ($equals[$comment]) {
190
            return true;
191
        }
192
193
        static $starts = [
194
            'version bump',
195
            'bumped version',
196
            "merge branch 'master'",
197
        ];
198
        foreach ($starts as $start) {
199
            if (strtolower(substr($comment, 0, strlen($start))) === $start) {
200
                return true;
201
            }
202
        }
203
204
        return false;
205
    }
206
207
}
208