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:
Complex classes like ApnsMessage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApnsMessage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ApnsMessage extends Message |
||
23 | { |
||
24 | // Default values |
||
25 | const DEFAULT_SOUND = 'default'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $simpleAlert; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $alertTitle; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $alertBody; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $alertTitleLocKey; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $alertTitleLocArgs; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $alertActionLocKey; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $alertLocKey; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $alertLocArgs; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $alertLaunchImage; |
||
71 | |||
72 | /** |
||
73 | * @var int |
||
74 | */ |
||
75 | private $badge; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | private $sound; |
||
81 | |||
82 | /** |
||
83 | * @var int |
||
84 | */ |
||
85 | private $contentAvailable; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | private $category; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | private $data; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | private $action; |
||
101 | |||
102 | /** |
||
103 | * Constructor |
||
104 | */ |
||
105 | 44 | public function __construct() |
|
114 | |||
115 | /** |
||
116 | * Get full message payload. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | 5 | public function getPayload() |
|
152 | |||
153 | /** |
||
154 | * Get the value of Simple Alert. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | 6 | public function getSimpleAlert() |
|
162 | |||
163 | /** |
||
164 | * Set the value of Simple Alert. |
||
165 | * |
||
166 | * @param string $simpleAlert |
||
167 | * |
||
168 | * @return self |
||
169 | */ |
||
170 | 3 | public function setSimpleAlert($simpleAlert) |
|
180 | |||
181 | /** |
||
182 | * Get the value of Alert Title. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 5 | public function getAlertTitle() |
|
190 | |||
191 | /** |
||
192 | * Set the value of Alert Title. |
||
193 | * |
||
194 | * @param string $alertTitle |
||
195 | * |
||
196 | * @return self |
||
197 | */ |
||
198 | 2 | public function setAlertTitle($alertTitle) |
|
204 | |||
205 | /** |
||
206 | * Get the value of Alert Body. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | 5 | public function getAlertBody() |
|
214 | |||
215 | /** |
||
216 | * Set the value of Alert Body. |
||
217 | * |
||
218 | * @param string $alertBody |
||
219 | * |
||
220 | * @return self |
||
221 | */ |
||
222 | 2 | public function setAlertBody($alertBody) |
|
228 | |||
229 | /** |
||
230 | * Get the value of Alert Title Loc Key. |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | 5 | public function getAlertTitleLocKey() |
|
238 | |||
239 | /** |
||
240 | * Set the value of Alert Title Loc Key. |
||
241 | * |
||
242 | * @param string $alertTitleLocKey |
||
243 | * |
||
244 | * @return self |
||
245 | */ |
||
246 | 2 | public function setAlertTitleLocKey($alertTitleLocKey) |
|
252 | |||
253 | /** |
||
254 | * Get the value of Alert Title Loc Args. |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 5 | public function getAlertTitleLocArgs() |
|
262 | |||
263 | /** |
||
264 | * Set the value of Alert Title Loc Args. |
||
265 | * |
||
266 | * @param array $alertTitleLocArgs |
||
267 | * |
||
268 | * @return self |
||
269 | */ |
||
270 | 2 | public function setAlertTitleLocArgs(array $alertTitleLocArgs) |
|
276 | |||
277 | /** |
||
278 | * Get the value of Alert Action Loc Key. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | 5 | public function getAlertActionLocKey() |
|
286 | |||
287 | /** |
||
288 | * Set the value of Alert Action Loc Key. |
||
289 | * |
||
290 | * @param string $alertActionLocKey |
||
291 | * |
||
292 | * @return self |
||
293 | */ |
||
294 | 2 | public function setAlertActionLocKey($alertActionLocKey) |
|
300 | |||
301 | /** |
||
302 | * Get the value of Alert Loc Key. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | 5 | public function getAlertLocKey() |
|
310 | |||
311 | /** |
||
312 | * Set the value of Alert Loc Key. |
||
313 | * |
||
314 | * @param string $alertLocKey |
||
315 | * |
||
316 | * @return self |
||
317 | */ |
||
318 | 2 | public function setAlertLocKey($alertLocKey) |
|
324 | |||
325 | /** |
||
326 | * Get the value of Alert Loc Args. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | 5 | public function getAlertLocArgs() |
|
334 | |||
335 | /** |
||
336 | * Set the value of Alert Loc Args. |
||
337 | * |
||
338 | * @param array $alertLocArgs |
||
339 | * |
||
340 | * @return self |
||
341 | */ |
||
342 | 2 | public function setAlertLocArgs(array $alertLocArgs) |
|
348 | |||
349 | /** |
||
350 | * Get the value of Alert Launch Image. |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | 5 | public function getAlertLaunchImage() |
|
358 | |||
359 | /** |
||
360 | * Set the value of Alert Launch Image. |
||
361 | * |
||
362 | * @param string $alertLaunchImage |
||
363 | * |
||
364 | * @return self |
||
365 | */ |
||
366 | 2 | public function setAlertLaunchImage($alertLaunchImage) |
|
372 | |||
373 | /** |
||
374 | * Get the value of Badge. |
||
375 | * |
||
376 | * @return int |
||
377 | */ |
||
378 | 6 | public function getBadge() |
|
382 | |||
383 | /** |
||
384 | * Set the value of Badge. |
||
385 | * |
||
386 | * @param int $badge |
||
387 | * |
||
388 | * @return self |
||
389 | */ |
||
390 | 2 | public function setBadge($badge) |
|
396 | |||
397 | /** |
||
398 | * Get the value of Sound. |
||
399 | * |
||
400 | * @return string |
||
401 | */ |
||
402 | 7 | public function getSound() |
|
406 | |||
407 | /** |
||
408 | * Set the value of Sound. |
||
409 | * |
||
410 | * @param string $sound |
||
411 | * |
||
412 | * @return self |
||
413 | */ |
||
414 | 2 | public function setSound($sound) |
|
420 | |||
421 | /** |
||
422 | * Get the value of Content Available. |
||
423 | * |
||
424 | * @return int |
||
425 | */ |
||
426 | 6 | public function getContentAvailable() |
|
430 | |||
431 | /** |
||
432 | * Set the value of Content Available. |
||
433 | * |
||
434 | * @param int $contentAvailable |
||
435 | * |
||
436 | * @return self |
||
437 | */ |
||
438 | 2 | public function setContentAvailable($contentAvailable) |
|
444 | |||
445 | /** |
||
446 | * Get the value of Category. |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | 6 | public function getCategory() |
|
454 | |||
455 | /** |
||
456 | * Set the value of Category. |
||
457 | * |
||
458 | * @param string $category |
||
459 | * |
||
460 | * @return self |
||
461 | */ |
||
462 | 2 | public function setCategory($category) |
|
468 | |||
469 | /** |
||
470 | * Get the data array. |
||
471 | * |
||
472 | * @return array |
||
473 | */ |
||
474 | 7 | public function getData() |
|
478 | |||
479 | /** |
||
480 | * Set the data array. |
||
481 | * |
||
482 | * @param array $data |
||
483 | * |
||
484 | * @return self |
||
485 | */ |
||
486 | 3 | public function setData(array $data) |
|
492 | |||
493 | /** |
||
494 | * Set a key/value pair in the data array. |
||
495 | * |
||
496 | * @param string|int $key |
||
497 | * @param mixed $value |
||
498 | * |
||
499 | * @return self |
||
500 | */ |
||
501 | 2 | View Code Duplication | public function addData($key, $value) |
513 | |||
514 | /** |
||
515 | * Get the action array. |
||
516 | * |
||
517 | * @return array |
||
518 | */ |
||
519 | 6 | public function getAction() |
|
523 | |||
524 | /** |
||
525 | * Set the action array. |
||
526 | * |
||
527 | * @param array $action |
||
528 | * |
||
529 | * @return self |
||
530 | */ |
||
531 | 3 | public function setAction(array $action) |
|
537 | |||
538 | /** |
||
539 | * Set an array in the action array. |
||
540 | * |
||
541 | * @param array $action |
||
542 | * |
||
543 | * @return self |
||
544 | */ |
||
545 | 1 | public function addAction(array $action) |
|
553 | |||
554 | /** |
||
555 | * Get the value of the payload "alert" key. |
||
556 | * |
||
557 | * @return string|array |
||
558 | */ |
||
559 | 5 | private function getPayloadAlertKeyValue() |
|
599 | } |
||
600 |
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.