Completed
Push — master ( 8d38b3...065629 )
by Andrii
02:12
created

Tag::setDate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 3
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 tag.
15
 * Represents a version with notes.
16
 *
17
 * @property string $name
18
 * @property string $name
19
 * @property array  $notes: note => note object
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Tag
24
{
25
    protected $_name;
26
    protected $_date;
27
    protected $_notes = [];
28
29 3
    public function __construct($tag, $date = null, array $notes = [])
30
    {
31 3
        if ($tag instanceof self) {
32
            $this->set($tag);
33
        } else {
34 3
            $this->setName($tag);
35 3
            $this->setDate($date);
36 3
            $this->setNotes($notes);
37
        }
38 3
    }
39
40
    public function set(Tag $tag)
41
    {
42
        $this->setName($tag->getName());
43
        $this->setDate($tag->getDate());
44
        $this->setNotes($tag->getNotes());
45
    }
46
47 3
    public function setName($value)
48
    {
49 3
        if ($value) {
50 3
            $this->_name = $value;
51 3
        }
52 3
    }
53
54 3
    public function getName()
55
    {
56 3
        return $this->_name;
57
    }
58
59
    /**
60
     * Appends note along with commmits.
61
     * @param Note $note
62
     */
63 1
    public function addNote(Note $note)
64
    {
65 1
        $this->findNote($note->getNote())->addCommits($note->getCommits());
66 1
    }
67
68
    /**
69
     * Appends notes along with commits.
70
     * @param Note[] $notes array of notes
71
     * @param boolean $prepend default is append
72
     */
73 3
    public function addNotes(array $notes, $prepend = false)
74
    {
75 3
        if ($prepend) {
76
            $saved = $this->_notes;
77
            $this->_notes = $notes;
78
            $notes = $saved;
79
        }
80 3
        foreach ($notes as $note) {
81 1
            $this->addNote($note);
82 3
        }
83 3
    }
84
85
    /**
86
     * Set notes.
87
     * @param Note[] $notes array of notes
88
     */
89 3
    public function setNotes(array $notes)
90
    {
91 3
        $this->_notes = $notes;
92 3
    }
93
94
    /**
95
     * Sets date.
96
     * Checks if it is date and later then current.
97
     * @param mixed $value date
98
     * @return Tag this
99
     */
100 3 View Code Duplication
    public function setDate($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 3
        $timestamp = strtotime($value);
103 3
        if ($timestamp !== false && $timestamp > $this->_date) {
104 3
            $this->_date = $timestamp;
105 3
        }
106
107 3
        return $this;
108
    }
109
110
    /**
111
     * Unsets date.
112
     */
113 2
    public function unsetDate()
114
    {
115 2
        $this->_date = null;
116
117 2
        return $this;
118
    }
119
120 3
    public function getDate()
121
    {
122 3
        return $this->_date ? date('Y-m-d', $this->_date) : null;
123
    }
124
125 3
    public function findNote($note)
126
    {
127 3
        if (!isset($this->_notes[$note])) {
128 3
            $this->_notes[$note] = new Note($note);
129 3
        }
130
131 3
        return $this->_notes[$note];
132
    }
133
134 3
    public function getNotes()
135
    {
136 3
        return $this->_notes;
137
    }
138
139
    /**
140
     * Finds date of the tag which is the date of the latest commit.
141
     * @return string|null date or null if no commits
142
     */
143
    public function findDate()
144
    {
145
        $max = '';
146 View Code Duplication
        foreach ($this->getNotes() as $note) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
            foreach ($note->getCommits() as $commit) {
148
                $date = $commit->getDate();
149
                if (strcmp($date, $max) > 0) {
150
                    $max = $date;
151
                }
152
            }
153
        }
154
155
        return $max ?: null;
156
    }
157
}
158