Completed
Push — master ( b4af2d...85fa4e )
by Andrii
02:31
created

Builder::setNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
 * Builder class.
16
 *
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class Builder
20
{
21
    /**
22
     * @var string current tag
23
     */
24
    protected $_tag = '';
25
26
    /**
27
     * @var string current note
28
     */
29
    protected $_note = '';
30
31
    /**
32
     * @var string current commit's hash
33
     */
34
    protected $_hash = '';
35
36
    /**
37
     * @var History history object
38
     */
39
    protected $_history;
40
41 3
    public function __construct(History $history = null)
42
    {
43 3
        $this->setHistory($history);
44 3
    }
45
46 3
    public function setHistory(History $history = null)
47
    {
48 3
        $this->_history = is_null($history) ? new History() : $history;
49 3
    }
50
51 3
    public function getHistory()
52
    {
53 3
        return $this->_history;
54
    }
55
56 3
    public function setTag($tag)
57
    {
58 3
        $this->_tag  = $tag;
59 3
        $this->setNote('');
60 3
    }
61
62 3
    public function setNote($note)
63
    {
64 3
        $this->_note = $note;
65 3
        $this->setHash('');
66 3
    }
67
68 3
    public function setHash($hash)
69
    {
70 3
        if ($hash) {
71 3
            $this->_hash = (string) $hash;
72 3
            $this->getHistory()->addHash($hash);
73 3
        }
74 3
    }
75
76 3
    public function getTag()
77
    {
78 3
        return $this->_tag;
79
    }
80
81 3
    public function getNote()
82
    {
83 3
        return $this->_note;
84
    }
85
86 2
    public function getHash()
87
    {
88 2
        return $this->_hash;
89
    }
90
91 2
    public function addHeader($header)
92
    {
93 2
        $this->getHistory()->addHeader($header);
94 2
    }
95
96 3
    public function addTag($tag, $date = null)
97
    {
98 3
        $this->setTag($tag);
99
100 3
        return $this->getHistory()->addTag(new Tag($tag, $date));
101
    }
102
103 2
    public function addNote($note)
104
    {
105 2
        $this->setNote($note);
106 2
        $this->getHistory()->findTag($this->getTag())->findNote($note);
107 2
    }
108
109 3
    public function addCommit($hash, $label)
110
    {
111 3
        $commit = new Commit($hash, $label);
112 3
        if (!$this->skipCommit($commit)) {
113 3
            $this->setHash($hash);
114 3
            $this->getHistory()->findTag($this->getTag())->findNote($this->getNote())->findCommit($hash)->setLabel($label);
115 3
        }
116 3
    }
117
118 2
    public function addComment($comment)
119
    {
120 2
        $this->getHistory()->findTag($this->getTag())->findNote($this->getNote())->findCommit($this->getHash())->addComment($comment);
121 2
    }
122
123 2
    public function addLink($link, $href)
124
    {
125 2
        $this->getHistory()->addLink($link, $href);
126 2
    }
127
128 1
    public function getLastTag()
129
    {
130 1
        return $this->getHistory()->lastTag;
131
    }
132
133 1
    public function getInitTag()
134
    {
135 1
        return $this->getHistory()->initTag;
136
    }
137
138 3
    public static function skipCommit(Commit $commit)
139
    {
140 3
        $subject = $commit->getSubject();
141
142
        static $equals = [
143
            ''       => 1,
144
            'bump'   => 1,
145
            'minor'  => 1,
146
            'bumped' => 1,
147 3
        ];
148 3
        if (isset($equals[$subject])) {
149 1
            return true;
150
        }
151
152
        static $starts = [
153
            'version bump',
154
            'bumped version',
155
            "merge branch 'master'",
156 3
        ];
157 3
        foreach ($starts as $start) {
158 3
            if (strtolower(substr($subject, 0, strlen($start))) === $start) {
159 1
                return true;
160
            }
161 3
        }
162
163 3
        return false;
164
    }
165
}
166