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 |
||
9 | class Chat extends BaseType implements TypeInterface |
||
10 | { |
||
11 | /** |
||
12 | * {@inheritdoc} |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | static protected $requiredParams = ['id', 'type']; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | static protected $map = [ |
||
24 | 'id' => true, |
||
25 | 'type' => true, |
||
26 | 'title' => true, |
||
27 | 'username' => true, |
||
28 | 'first_name' => true, |
||
29 | 'last_name' => true, |
||
30 | 'photo' => ChatPhoto::class, |
||
31 | 'bio' => true, |
||
32 | 'description' => true, |
||
33 | 'invite_link' => true, |
||
34 | 'pinned_message' => Message::class, |
||
35 | 'permissions' => ChatPermissions::class, |
||
36 | 'slow_mode_delay' => true, |
||
37 | 'sticker_set_name' => true, |
||
38 | 'can_set_sticker_set' => true, |
||
39 | 'linked_chat_id' => true, |
||
40 | 'location' => ChatLocation::class |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Unique identifier for this chat, not exceeding 1e13 by absolute value |
||
45 | * |
||
46 | * @var int|string |
||
47 | */ |
||
48 | protected $id; |
||
49 | |||
50 | /** |
||
51 | * Type of chat, can be either “private”, “group”, “supergroup” or “channel” |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $type; |
||
56 | |||
57 | /** |
||
58 | * Optional. Title, for channels and group chats |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $title; |
||
63 | |||
64 | /** |
||
65 | * Optional. Username, for private chats and channels if available |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $username; |
||
70 | |||
71 | /** |
||
72 | * Optional. First name of the other party in a private chat |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $firstName; |
||
77 | |||
78 | /** |
||
79 | * Optional. Last name of the other party in a private chat |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $lastName; |
||
84 | |||
85 | /** |
||
86 | * Optional. Chat photo. Returned only in getChat. |
||
87 | * |
||
88 | * @var ChatPhoto |
||
89 | */ |
||
90 | protected $photo; |
||
91 | |||
92 | /** |
||
93 | * Optional. Bio of the other party in a private chat. Returned only in getChat |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $bio; |
||
98 | |||
99 | /** |
||
100 | * Optional. Description, for supergroups and channel chats. Returned only in getChat. |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $description; |
||
105 | |||
106 | /** |
||
107 | * Optional. Chat invite link, for supergroups and channel chats. Returned only in getChat. |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $inviteLink; |
||
112 | |||
113 | /** |
||
114 | * Optional. Pinned message, for supergroups. Returned only in getChat. |
||
115 | * |
||
116 | * @var Message |
||
117 | */ |
||
118 | protected $pinnedMessage; |
||
119 | |||
120 | /** |
||
121 | * Optional. Default chat member permissions, for groups and supergroups. Returned only in getChat. |
||
122 | * |
||
123 | * @var ChatPermissions |
||
124 | */ |
||
125 | protected $permissions; |
||
126 | |||
127 | /** |
||
128 | * Optional. For supergroups, the minimum allowed delay between consecutive messages sent by each unpriviledged |
||
129 | * user. Returned only in getChat. |
||
130 | * |
||
131 | * @var int |
||
132 | */ |
||
133 | protected $slowModeDelay; |
||
134 | |||
135 | /** |
||
136 | * Optional. For supergroups, name of group sticker set. Returned only in getChat. |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $stickerSetName; |
||
141 | |||
142 | /** |
||
143 | * Optional. True, if the bot can change the group sticker set. Returned only in getChat. |
||
144 | * |
||
145 | * @var bool |
||
146 | */ |
||
147 | protected $canSetStickerSet; |
||
148 | |||
149 | /** |
||
150 | * Optional. Unique identifier for the linked chat, i.e. the discussion group identifier for a channel and vice |
||
151 | * versa; for supergroups and channel chats. This identifier may be greater than 32 bits and some programming |
||
152 | * languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 |
||
153 | * bit integer or double-precision float type are safe for storing this identifier. Returned only in getChat. |
||
154 | * |
||
155 | * @var int |
||
156 | */ |
||
157 | protected $linkedChatId; |
||
158 | |||
159 | /** |
||
160 | * Optional. For supergroups, the location to which the supergroup is connected. Returned only in getChat. |
||
161 | * |
||
162 | * @var ChatLocation |
||
163 | */ |
||
164 | protected $location; |
||
165 | |||
166 | /** |
||
167 | * @return int|string |
||
168 | */ |
||
169 | 3 | public function getId() |
|
173 | |||
174 | /** |
||
175 | * @param int|string $id |
||
176 | * |
||
177 | * @throws InvalidArgumentException |
||
178 | */ |
||
179 | 22 | View Code Duplication | public function setId($id) |
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | public function getType() |
|
195 | |||
196 | /** |
||
197 | * @param string $type |
||
198 | */ |
||
199 | 19 | public function setType($type) |
|
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | 2 | public function getTitle() |
|
211 | |||
212 | /** |
||
213 | * @param string $title |
||
214 | */ |
||
215 | 13 | public function setTitle($title) |
|
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | 2 | public function getUsername() |
|
227 | |||
228 | /** |
||
229 | * @param string $username |
||
230 | */ |
||
231 | 8 | public function setUsername($username) |
|
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | 2 | public function getFirstName() |
|
243 | |||
244 | /** |
||
245 | * @param string $firstName |
||
246 | */ |
||
247 | 8 | public function setFirstName($firstName) |
|
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | 2 | public function getLastName() |
|
259 | |||
260 | /** |
||
261 | * @param string $lastName |
||
262 | */ |
||
263 | 8 | public function setLastName($lastName) |
|
267 | |||
268 | /** |
||
269 | * @return ChatPhoto |
||
270 | */ |
||
271 | 1 | public function getPhoto() |
|
275 | |||
276 | /** |
||
277 | * @param ChatPhoto $photo |
||
278 | */ |
||
279 | 2 | public function setPhoto($photo) |
|
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | 2 | public function getBio() |
|
291 | |||
292 | /** |
||
293 | * @param string $bio |
||
294 | */ |
||
295 | 3 | public function setBio($bio) |
|
299 | |||
300 | /** |
||
301 | * @return string |
||
302 | */ |
||
303 | 1 | public function getDescription() |
|
307 | |||
308 | /** |
||
309 | * @param string $description |
||
310 | */ |
||
311 | 2 | public function setDescription($description) |
|
315 | |||
316 | /** |
||
317 | * @return string |
||
318 | */ |
||
319 | public function getInviteLink() |
||
323 | |||
324 | /** |
||
325 | * @param string $inviteLink |
||
326 | */ |
||
327 | public function setInviteLink($inviteLink) |
||
331 | |||
332 | /** |
||
333 | * @return Message |
||
334 | */ |
||
335 | public function getPinnedMessage() |
||
339 | |||
340 | /** |
||
341 | * @param Message $pinnedMessage |
||
342 | */ |
||
343 | public function setPinnedMessage($pinnedMessage) |
||
347 | |||
348 | /** |
||
349 | * @return ChatPermissions |
||
350 | */ |
||
351 | public function getPermissions() |
||
355 | |||
356 | /** |
||
357 | * @param ChatPermissions $permissions |
||
358 | */ |
||
359 | public function setPermissions($permissions) |
||
363 | |||
364 | /** |
||
365 | * @return int |
||
366 | */ |
||
367 | public function getSlowModeDelay() |
||
371 | |||
372 | /** |
||
373 | * @param int $slowModeDelay |
||
374 | */ |
||
375 | public function setSlowModeDelay($slowModeDelay) |
||
379 | |||
380 | /** |
||
381 | * @return string |
||
382 | */ |
||
383 | public function getStickerSetName() |
||
387 | |||
388 | /** |
||
389 | * @param string $stickerSetName |
||
390 | */ |
||
391 | public function setStickerSetName($stickerSetName) |
||
395 | |||
396 | /** |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function isCanSetStickerSet() |
||
403 | |||
404 | /** |
||
405 | * @param bool $canSetStickerSet |
||
406 | */ |
||
407 | public function setCanSetStickerSet($canSetStickerSet) |
||
411 | |||
412 | /** |
||
413 | * @return int |
||
414 | */ |
||
415 | public function getLinkedChatId() |
||
419 | |||
420 | /** |
||
421 | * @param int $linkedChatId |
||
422 | */ |
||
423 | public function setLinkedChatId($linkedChatId) |
||
427 | |||
428 | /** |
||
429 | * @return ChatLocation |
||
430 | */ |
||
431 | public function getLocation() |
||
435 | |||
436 | /** |
||
437 | * @param ChatLocation $location |
||
438 | */ |
||
439 | public function setLocation($location) |
||
443 | } |
||
444 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.