Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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'; |
|
|
|||
64 | 1 | } elseif ($this->id < 0) { |
|
65 | 1 | $this->type = 'group'; |
|
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() |
||
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() |
||
207 | } |
||
208 |
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..