Completed
Push — master ( 69f712...a96098 )
by Andrii
02:39
created

Commit::getAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
 * Commit class.
16
 *
17
 * @property string $hash
18
 * @property string $label
19
 * @property array  $comments: comment => comment
20
 * @property string $date
21
 * @property string $author
22
 * @property string $hash
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class Commit
27
{
28
    /**
29
     * @var string 7 characters commit hash
30
     */
31
    protected $_hash;
32
33
    /**
34
     * @var string string representation of commit
35
     */
36
    protected $_label;
37
    protected $_comments = [];
38
39
    protected $_date;
40
    protected $_author;
41
    protected $_subject;
42
43 3
    public function __construct($hash, $label = null, array $comments = [])
44
    {
45 3
        $this->setHash($hash);
46 3
        $this->setLabel($label);
47 3
        $this->setComments($comments);
48 3
    }
49
50
    public function set(Commit $commit)
51
    {
52
        $this->setHash($commit->hashHash());
0 ignored issues
show
Bug introduced by
The method hashHash() does not seem to exist on object<hiqdev\chkipper\history\Commit>.

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...
53
        $this->setLabel($commit->getLabel());
54
        $this->setComments($commit->getComments());
55
    }
56
57 3
    public function setHash($hash)
58
    {
59 3
        $this->_hash = $hash;
60 3
    }
61
62 3
    public function getHash()
63
    {
64 3
        return $this->_hash;
65
    }
66
67 3
    public function setLabel($label)
68
    {
69 3
        if ($label) {
70 3
            $this->_label = $label;
71 3
            if (preg_match('/^(\d{4}-\d{2}-\d{2})\s*(.*?)\s*\[(.*?)\]$/', $label, $m) ||
72 3
                preg_match('/^(\d{4}-\d{2}-\d{2})\s*(.*?)\s*\((.*?)\)$/', $label, $m)) {
73 3
                $this->setDate($m[1]);
74 3
                $this->setSubject($m[2]);
75 3
                $this->setAuthor($m[3]);
76 3
            }
77 3
        }
78 3
    }
79
80 3
    public function getLabel()
81
    {
82 3
        return $this->_subject || $this->_date || $this->_author
83 3
            ? $this->getDate() . ' ' . $this->getSubject() . ' [' . $this->getAuthor() . ']'
84 3
            : $this->_label
85 3
        ;
86
    }
87
88 2
    public function addComment($comment)
89
    {
90 2
        $this->_comments[$comment] = $comment;
91 2
    }
92
93 3
    public function addComments(array $comments)
94
    {
95 3
        foreach ($comments as $comment) {
96 1
            $this->addComment($comment);
97 3
        }
98 3
    }
99
100 3
    public function setComments($comments)
101
    {
102 3
        $this->_comments = [];
103 3
        $this->addComments($comments);
104 3
    }
105
106 3
    public function getComments()
107
    {
108 3
        return $this->_comments;
109
    }
110
111 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...
112
    {
113 3
        $timestamp = strtotime($value);
114 3
        if ($timestamp !== false) {
115 3
            $this->_date = $timestamp;
116 3
        }
117
118 3
        return $this;
119
    }
120
121 3
    public function getDate()
122
    {
123 3
        return $this->_date ? date('Y-m-d', $this->_date) : null;
124
    }
125
126 3
    public function setSubject($value)
127
    {
128 3
        $this->_subject = $value;
129
130 3
        return $this;
131
    }
132
133 3
    public function getSubject()
134
    {
135 3
        return $this->_subject;
136
    }
137
138 3
    public function setAuthor($value)
139
    {
140 3
        $this->_author = $value;
141
142 3
        return $this;
143
    }
144
145 3
    public function getAuthor()
146
    {
147 3
        return $this->_author;
148
    }
149
}
150