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

Commit::getComments()   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 2
Bugs 0 Features 0
Metric Value
c 2
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
 * Holds commit properties:
17
 * - hash
18
 * - date
19
 * - author
20
 * - subject
21
 * - list of comments.
22
 *
23
 * @author Andrii Vasyliev <[email protected]>
24
 */
25
class Commit
26
{
27
    protected $_hash;
28
    protected $_label;
29
    protected $_comments = [];
30
31
    protected $_date;
32
    protected $_author;
33
    protected $_subject;
34
35 2
    public function __construct($hash, $label = null, array $comments = [])
36
    {
37 2
        $this->setHash($hash);
38 2
        $this->setLabel($label);
39 2
        $this->setComments($comments);
40 2
    }
41
42
    public function set(Commit $commit)
43
    {
44
        $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...
45
        $this->setLabel($commit->getLabel());
46
        $this->setComments($commit->getComments());
47
    }
48
49 2
    public function setHash($hash)
50
    {
51 2
        $this->_hash = $hash;
52 2
    }
53
54 1
    public function getHash()
55
    {
56 1
        return $this->_hash;
57
    }
58
59 2
    public function setLabel($label)
60
    {
61 2
        if ($label) {
62 2
            $this->_label = $label;
63 2
            if (preg_match('/^(\d{4}-\d{2}-\d{2})\s*(.*?)\s*[\(\[](.*?)[\)\]]$/', $label, $m)) {
64 2
                $this->setDate($m[1]);
65 2
                $this->setSubject($m[2]);
66 2
                $this->setAuthor($m[3]);
67 2
            }
68 2
        }
69 2
    }
70
71 1
    public function getLabel()
72
    {
73 1
        return $this->_subject || $this->_date || $this->_author
74 1
            ? $this->getDate() . ' ' . $this->getSubject() . ' [' . $this->getAuthor() . ']'
75 1
            : $this->_label
76 1
        ;
77
    }
78
79 2
    public function addComment($comment)
80
    {
81 2
        $this->_comments[$comment] = $comment;
82 2
    }
83
84 2
    public function addComments(array $comments)
85
    {
86 2
        foreach ($comments as $comment) {
87 1
            $this->addComment($comment);
88 2
        }
89 2
    }
90
91 2
    public function setComments($comments)
92
    {
93 2
        $this->_comments = [];
94 2
        $this->addComments($comments);
95 2
    }
96
97 1
    public function getComments()
98
    {
99 1
        return $this->_comments;
100
    }
101
102 2 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...
103
    {
104 2
        $timestamp = strtotime($value);
105 2
        if ($timestamp !== false) {
106 2
            $this->_date = $timestamp;
107 2
        }
108
109 2
        return $this;
110
    }
111
112 1
    public function getDate()
113
    {
114 1
        return $this->_date ? date('Y-m-d', $this->_date) : null;
115
    }
116
117 2
    public function setSubject($value)
118
    {
119 2
        $this->_subject = $value;
120
121 2
        return $this;
122
    }
123
124 1
    public function getSubject()
125
    {
126 1
        return $this->_subject;
127
    }
128
129 2
    public function setAuthor($value)
130
    {
131 2
        $this->_author = $value;
132
133 2
        return $this;
134
    }
135
136 1
    public function getAuthor()
137
    {
138 1
        return $this->_author;
139
    }
140
}
141