Completed
Push — feature/improve-code ( e986a1...36787d )
by Avtandil
14:14
created

Chat::__construct()   D

Complexity

Conditions 10
Paths 130

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.1953

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 4.9032
ccs 14
cts 16
cp 0.875
cc 10
eloc 17
nc 130
nop 1
crap 10.1953

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
use Longman\TelegramBot\Exception\TelegramException;
14
15
class Chat extends Entity
16
{
17
    /**
18
     * @var mixed|null
19
     */
20
    protected $id;
21
    /**
22
     * @var null
23
     */
24
    protected $type;
25
26
    /**
27
     * @var mixed|null
28
     */
29
    protected $title;
30
31
    /**
32
     * @var mixed|null
33
     */
34
    protected $username;
35
36
    /**
37
     * @var mixed|null
38
     */
39
    protected $first_name;
40
41
    /**
42
     * @var mixed|null
43
     */
44
    protected $last_name;
45
46
    /**
47
     * Chat constructor.
48
     *
49
     * @param array $data
50
     * @throws \Longman\TelegramBot\Exception\TelegramException
51
     */
52 19
    public function __construct(array $data)
53
    {
54 19
        $this->id = isset($data['id']) ? $data['id'] : null;
55 19
        if (empty($this->id)) {
56
            throw new TelegramException('id is empty!');
57
        }
58
59 19
        if (isset($data['type'])) {
60 15
            $this->type = $data['type'];
61
        } else {
62 10
            if ($this->id > 0) {
63 9
                $this->type = 'private';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'private' of type string is incompatible with the declared type null of property $type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64 1
            } elseif ($this->id < 0) {
65 1
                $this->type = 'group';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'group' of type string is incompatible with the declared type null of property $type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
            } else {
67
                $this->type = null;
68
            }
69
        }
70
71 19
        $this->title      = isset($data['title']) ? $data['title'] : null;
72 19
        $this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
73 19
        $this->last_name  = isset($data['last_name']) ? $data['last_name'] : null;
74 19
        $this->username   = isset($data['username']) ? $data['username'] : null;
75 19
    }
76
77
    /**
78
     * Check if is group chat
79
     *
80
     * @return bool
81
     */
82
    public function isGroupChat()
83
    {
84
        if ($this->type == 'group' || $this->id < 0) {
85
            return true;
86
        }
87
        return false;
88
    }
89
90
    /**
91
     * Check if is private chat
92
     *
93
     * @return bool
94
     */
95
    public function isPrivateChat()
96
    {
97
        if ($this->type == 'private') {
98
            return true;
99
        }
100
        return false;
101
    }
102
103
    /**
104
     * Check if is super group
105
     *
106
     * @return bool
107
     */
108
    public function isSuperGroup()
109
    {
110
        if ($this->type == 'supergroup') {
111
            return true;
112
        }
113
        return false;
114
    }
115
116
    /**
117
     * Check if is channel
118
     *
119
     * @return bool
120
     */
121
    public function isChannel()
122
    {
123
        if ($this->type == 'channel') {
124
            return true;
125
        }
126
        return false;
127
    }
128
129
    /**
130
     * Get id
131
     *
132
     * @return mixed|null
133
     */
134 13
    public function getId()
135
    {
136 13
        return $this->id;
137
    }
138
139
    /**
140
     * Get type
141
     *
142
     * @return null
143
     */
144 7
    public function getType()
145
    {
146 7
        return $this->type;
147
    }
148
149
    /**
150
     * Get title
151
     *
152
     * @return mixed|null
153
     */
154 6
    public function getTitle()
155
    {
156 6
        return $this->title;
157
    }
158
159
    /**
160
     * Get first name
161
     *
162
     * @return mixed|null
163
     */
164 2
    public function getFirstName()
165
    {
166 2
        return $this->first_name;
167
    }
168
169
    /**
170
     * Get last name
171
     *
172
     * @return mixed|null
173
     */
174
    public function getLastName()
175
    {
176
        return $this->last_name;
177
    }
178
179
    /**
180
     * Get username
181
     *
182
     * @return mixed|null
183
     */
184 2
    public function getUsername()
185
    {
186 2
        return $this->username;
187
    }
188
189
    /**
190
     * Try mention
191
     *
192
     * @return mixed|null|string
193
     */
194
    public function tryMention()
195
    {
196
        if ($this->isPrivateChat()) {
197 View Code Duplication
            if (is_null($this->username)) {
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...
198
                if (!is_null($this->last_name)) {
199
                    return $this->first_name . ' ' . $this->last_name;
200
                }
201
                return $this->first_name;
202
            }
203
            return '@' . $this->username;
204
        }
205
        return $this->getTitle();
206
    }
207
}
208