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 Attachment 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 Attachment, and based on these observations, apply Extract Interface, too.
1 | <?php namespace nyx\notify\transports\slack\message; |
||
21 | class Attachment implements core\interfaces\Serializable |
||
22 | { |
||
23 | /** |
||
24 | * The traits of an Attachment. |
||
25 | */ |
||
26 | use core\traits\Serializable; |
||
27 | |||
28 | /** |
||
29 | * The colors an Attachment can be styled with. Note: Those only represent the colors predefined by Slack. |
||
30 | * Otherwise, the Attachment::$color property accepts any hex color string. |
||
31 | */ |
||
32 | const COLOR_GOOD = 'good'; |
||
33 | const COLOR_WARNING = 'warning'; |
||
34 | const COLOR_DANGER = 'danger'; |
||
35 | |||
36 | /** |
||
37 | * @var string Required. A plaintext message displayed to users using a client/interface that does not |
||
38 | * support attachments or interactive messages. Consider leaving a URL pointing to your |
||
39 | * service if the potential message actions are representable outside of Slack. |
||
40 | * Otherwise, let folks know what they are missing. |
||
41 | */ |
||
42 | protected $fallback; |
||
43 | |||
44 | /** |
||
45 | * @var string Optional. The text that appears above the attachment block. |
||
46 | */ |
||
47 | protected $pretext; |
||
48 | |||
49 | /** |
||
50 | * @var string Optional. The text that appears within the attachment. |
||
51 | */ |
||
52 | protected $text; |
||
53 | |||
54 | /** |
||
55 | * @var string Optional. The title of the Attachment. |
||
56 | */ |
||
57 | protected $title; |
||
58 | |||
59 | /** |
||
60 | * @var string Optional. The URL the title should link to. |
||
61 | */ |
||
62 | protected $title_link; |
||
63 | |||
64 | /** |
||
65 | * @var string Optional. The image that should appear within the attachment. |
||
66 | */ |
||
67 | protected $image_url; |
||
68 | |||
69 | /** |
||
70 | * @var string Optional. The thumbnail that should appear within the attachment. |
||
71 | */ |
||
72 | protected $thumb_url; |
||
73 | |||
74 | /** |
||
75 | * @var string Optional. The name of the author. |
||
76 | */ |
||
77 | protected $author_name; |
||
78 | |||
79 | /** |
||
80 | * @var string Optional. The URL the author's name should link to. |
||
81 | */ |
||
82 | protected $author_link; |
||
83 | |||
84 | /** |
||
85 | * @var string Optional. The author's icon. |
||
86 | */ |
||
87 | protected $author_icon; |
||
88 | |||
89 | /** |
||
90 | * @var string Optional. The color used for the border along the left side of the Attachment. |
||
91 | */ |
||
92 | protected $color; |
||
93 | |||
94 | /** |
||
95 | * @var string Optional. The text to display in the footer of the Attachment. |
||
96 | */ |
||
97 | protected $footer; |
||
98 | |||
99 | /** |
||
100 | * @var string Optional. The icon to display in the footer of the Attachment. |
||
101 | */ |
||
102 | protected $footer_icon; |
||
103 | |||
104 | /** |
||
105 | * @var int Optional. The (Unix) timestamp of the Attachment. |
||
106 | */ |
||
107 | protected $ts; |
||
108 | |||
109 | /** |
||
110 | * @var array Optional. The attributes which should be parsed by Slack as its Markdown flavour. |
||
111 | */ |
||
112 | protected $mrkdwn_in = []; |
||
113 | |||
114 | /** |
||
115 | * @var attachment\Field[] The Fields of the Attachment. |
||
116 | */ |
||
117 | protected $fields = []; |
||
118 | |||
119 | /** |
||
120 | * @var string Optional. The ID of the callback to use for the attached Actions, if any. Is required |
||
121 | * When those Actions are present. |
||
122 | */ |
||
123 | protected $callbackId; |
||
124 | |||
125 | /** |
||
126 | * @var attachment\Action[] The Actions of the Attachment. 5 at most. |
||
127 | */ |
||
128 | protected $actions = []; |
||
129 | |||
130 | /** |
||
131 | * Creates a new Attachment instance. |
||
132 | * |
||
133 | * @param array $attributes |
||
134 | */ |
||
135 | public function __construct(array $attributes = null) |
||
141 | |||
142 | /** |
||
143 | * Sets the attributes of this Attachment. |
||
144 | * |
||
145 | * @param array $attributes |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function setAttributes(array $attributes) : Attachment |
||
224 | |||
225 | /** |
||
226 | * Returns the fallback text. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getFallback() |
||
234 | |||
235 | /** |
||
236 | * Sets the fallback text. |
||
237 | * |
||
238 | * @param string $fallback |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setFallback(string $fallback) : Attachment |
||
247 | |||
248 | /** |
||
249 | * Returns the text that appears above the attachment block. |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getPretext() |
||
257 | |||
258 | /** |
||
259 | * Sets the text that appears above the attachment block. |
||
260 | * |
||
261 | * @param string $pretext |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setPretext(string $pretext) : Attachment |
||
270 | |||
271 | /** |
||
272 | * Returns the text that appears within the attachment. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getText() |
||
280 | |||
281 | /** |
||
282 | * Sets the text that appears within the attachment. |
||
283 | * |
||
284 | * @param string $text |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function setText(string $text) : Attachment |
||
293 | |||
294 | /** |
||
295 | * Returns the title of the Attachment. |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getTitle() |
||
303 | |||
304 | /** |
||
305 | * Sets the title of the Attachment. |
||
306 | * |
||
307 | * @param string $title |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function setTitle(string $title) : Attachment |
||
316 | |||
317 | /** |
||
318 | * Returns the URL the title should link to. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function getTitleLink() |
||
326 | |||
327 | /** |
||
328 | * Sets the URL the title should link to. |
||
329 | * |
||
330 | * @param string $link |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function setTitleLink(string $link) : Attachment |
||
339 | |||
340 | /** |
||
341 | * Returns the image that should appear within the attachment. |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getImageUrl() |
||
349 | |||
350 | /** |
||
351 | * Sets the image that should appear within the attachment. |
||
352 | * |
||
353 | * @param string $url |
||
354 | * @return $this |
||
355 | */ |
||
356 | public function setImageUrl(string $url) : Attachment |
||
362 | |||
363 | /** |
||
364 | * Returns the thumbnail that should appear within the attachment. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getThumbUrl() |
||
372 | |||
373 | /** |
||
374 | * Sets the thumbnail that should appear within the attachment. |
||
375 | * |
||
376 | * @param string $url |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function setThumbUrl(string $url) : Attachment |
||
385 | |||
386 | /** |
||
387 | * Returns the name of the author. |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public function getAuthorName() |
||
395 | |||
396 | /** |
||
397 | * Sets the name of the author. |
||
398 | * |
||
399 | * @param string $author_name |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function setAuthorName(string $author_name) : Attachment |
||
408 | |||
409 | /** |
||
410 | * Returns the URL the author's name should link to. |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | public function getAuthorLink() |
||
418 | |||
419 | /** |
||
420 | * Sets the URL the author's name should link to. |
||
421 | * |
||
422 | * @param string $url |
||
423 | * @return $this |
||
424 | */ |
||
425 | public function setAuthorLink(string $url) : Attachment |
||
431 | |||
432 | /** |
||
433 | * Returns the author's icon. |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | public function getAuthorIcon() |
||
441 | |||
442 | /** |
||
443 | * Sets the author's icon. |
||
444 | * |
||
445 | * @param string $url |
||
446 | * @return $this |
||
447 | */ |
||
448 | public function setAuthorIcon(string $url) : Attachment |
||
454 | |||
455 | /** |
||
456 | * Returns the color used for the border along the left side of the Attachment. |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | public function getColor() |
||
464 | |||
465 | /** |
||
466 | * Sets the color used for the border along the left side of the Attachment. |
||
467 | * |
||
468 | * @param string $color One of the COLOR_* class constants or a hex color code. |
||
469 | * @return $this |
||
470 | */ |
||
471 | public function setColor(string $color) : Attachment |
||
477 | |||
478 | /** |
||
479 | * Returns the text to display in the footer of the Attachment. |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | public function getFooter() |
||
487 | |||
488 | /** |
||
489 | * Sets the text to display in the footer of the Attachment. |
||
490 | * |
||
491 | * @param string $footer |
||
492 | * @return $this |
||
493 | */ |
||
494 | public function setFooter(string $footer) : Attachment |
||
500 | |||
501 | /** |
||
502 | * Returns the icon to display in the footer of the Attachment. |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | public function getFooterIcon() |
||
510 | |||
511 | /** |
||
512 | * Sets the icon to display in the footer of the Attachment. |
||
513 | * |
||
514 | * @param string $url |
||
515 | * @return $this |
||
516 | */ |
||
517 | public function setFooterIcon(string $url) : Attachment |
||
523 | |||
524 | /** |
||
525 | * Returns the (UNIX) timestamp of the Attachment. |
||
526 | * |
||
527 | * @return int |
||
528 | */ |
||
529 | public function getTimestamp() |
||
533 | |||
534 | /** |
||
535 | * Sets the (UNIX) timestamp of the Attachment. |
||
536 | * |
||
537 | * @param int $time |
||
538 | * @return $this |
||
539 | */ |
||
540 | public function setTimestamp(int $time) : Attachment |
||
546 | |||
547 | /** |
||
548 | * Returns the attributes which should be parsed by Slack as its Markdown flavour. |
||
549 | * |
||
550 | * @return array |
||
551 | */ |
||
552 | public function getMarkdownAttributes() : array |
||
556 | |||
557 | /** |
||
558 | * Sets the attributes which should be parsed by Slack as its Markdown flavour. |
||
559 | * |
||
560 | * @param array $attributes |
||
561 | * @return $this |
||
562 | */ |
||
563 | public function setMarkdownAttributes(array $attributes) : Attachment |
||
569 | |||
570 | /** |
||
571 | * Returns the Fields of the Attachment. |
||
572 | * |
||
573 | * @return attachment\Field[] |
||
574 | */ |
||
575 | public function getFields() : array |
||
579 | |||
580 | /** |
||
581 | * Sets the Fields of the Attachment. |
||
582 | * |
||
583 | * @param attachment\Field[] $fields |
||
584 | * @return $this |
||
585 | */ |
||
586 | public function setFields(array $fields) : Attachment |
||
601 | |||
602 | /** |
||
603 | * Adds a Field to the Attachment. Acts as a factory method. |
||
604 | * |
||
605 | * @param mixed $field |
||
606 | * @param string $value If given, then $field is treated as the title of the Field and must be a string. |
||
607 | * @return $this |
||
608 | * @throws \InvalidArgumentException |
||
609 | */ |
||
610 | public function addField($field, string $value = null) : Attachment |
||
635 | |||
636 | /** |
||
637 | * Returns the callback id for the Actions of this Attachment. |
||
638 | * |
||
639 | * @return string |
||
640 | */ |
||
641 | public function getCallbackId() |
||
645 | |||
646 | /** |
||
647 | * Sets the callback id for the Actions of this Attachment. |
||
648 | * |
||
649 | * @param string $id |
||
650 | * @return $this |
||
651 | */ |
||
652 | public function setCallbackId(string $id) : Attachment |
||
658 | |||
659 | /** |
||
660 | * Returns the Actions of the Attachment. |
||
661 | * |
||
662 | * @return attachment\Action[] |
||
663 | */ |
||
664 | public function getActions() : array |
||
668 | |||
669 | /** |
||
670 | * Sets the Actions of the Attachment. |
||
671 | * |
||
672 | * @param array $actions |
||
673 | * @return $this |
||
674 | */ |
||
675 | public function setActions(array $actions) : Attachment |
||
685 | |||
686 | /** |
||
687 | * Adds an Action to the Attachment. |
||
688 | * |
||
689 | * @param array|attachment\Action $action |
||
690 | * @return $this |
||
691 | * @throws \InvalidArgumentException |
||
692 | */ |
||
693 | View Code Duplication | public function addAction($action) : Attachment |
|
709 | |||
710 | /** |
||
711 | * {@inheritDoc} |
||
712 | */ |
||
713 | public function unserialize($data) |
||
717 | |||
718 | /** |
||
719 | * {@inheritDoc} |
||
720 | */ |
||
721 | public function toArray() : array |
||
756 | } |
||
757 |
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.