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 |
||
7 | class Attachment |
||
8 | { |
||
9 | /** |
||
10 | * The fallback text to use for clients that don't support attachments. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $fallback; |
||
15 | |||
16 | /** |
||
17 | * The callback_id for the button group. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $callback_id; |
||
22 | |||
23 | /** |
||
24 | * Optional text that should appear within the attachment. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $text; |
||
29 | |||
30 | /** |
||
31 | * Optional image that should appear within the attachment. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $image_url; |
||
36 | |||
37 | /** |
||
38 | * Optional thumbnail that should appear within the attachment. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $thumb_url; |
||
43 | |||
44 | /** |
||
45 | * Optional text that should appear above the formatted data. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $pretext; |
||
50 | |||
51 | /** |
||
52 | * Optional title for the attachment. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $title; |
||
57 | |||
58 | /** |
||
59 | * Optional title link for the attachment. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $title_link; |
||
64 | |||
65 | /** |
||
66 | * Optional author name for the attachment. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $author_name; |
||
71 | |||
72 | /** |
||
73 | * Optional author link for the attachment. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $author_link; |
||
78 | |||
79 | /** |
||
80 | * Optional author icon for the attachment. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $author_icon; |
||
85 | |||
86 | /** |
||
87 | * The color to use for the attachment. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $color = 'good'; |
||
92 | |||
93 | /** |
||
94 | * The text to use for the attachment footer. |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $footer; |
||
99 | |||
100 | /** |
||
101 | * The icon to use for the attachment footer. |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $footer_icon; |
||
106 | |||
107 | /** |
||
108 | * The timestamp to use for the attachment. |
||
109 | * |
||
110 | * @var \DateTime |
||
111 | */ |
||
112 | protected $timestamp; |
||
113 | |||
114 | /** |
||
115 | * The fields of the attachment. |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | protected $fields = []; |
||
120 | |||
121 | /** |
||
122 | * The fields of the attachment that Slack should interpret |
||
123 | * with its Markdown-like language. |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | protected $markdown_fields = []; |
||
128 | |||
129 | /** |
||
130 | * A collection of actions (buttons) to include in the attachment. |
||
131 | * A maximum of 5 actions may be provided. |
||
132 | * |
||
133 | * @var array |
||
134 | */ |
||
135 | protected $actions = []; |
||
136 | |||
137 | /** |
||
138 | * Instantiate a new Attachment. |
||
139 | * |
||
140 | * @param array $attributes |
||
141 | * @return void |
||
|
|||
142 | */ |
||
143 | public function __construct(array $attributes) |
||
217 | |||
218 | /** |
||
219 | * Get the fallback text. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getFallback() |
||
227 | |||
228 | /** |
||
229 | * Set the fallback text. |
||
230 | * |
||
231 | * @param string $fallback |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setFallback($fallback) |
||
240 | |||
241 | /** |
||
242 | * Get the callback_id text. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getCallback() |
||
250 | |||
251 | /** |
||
252 | * Set the callback_id. |
||
253 | * |
||
254 | * @param string $callback_id |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function setCallback($callback_id) |
||
263 | |||
264 | /** |
||
265 | * Get the optional text to appear within the attachment. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getText() |
||
273 | |||
274 | /** |
||
275 | * Set the optional text to appear within the attachment. |
||
276 | * |
||
277 | * @param string $text |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function setText($text) |
||
286 | |||
287 | /** |
||
288 | * Get the optional image to appear within the attachment. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getImageUrl() |
||
296 | |||
297 | /** |
||
298 | * Set the optional image to appear within the attachment. |
||
299 | * |
||
300 | * @param string $image_url |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function setImageUrl($image_url) |
||
309 | |||
310 | /** |
||
311 | * Get the optional thumbnail to appear within the attachment. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getThumbUrl() |
||
319 | |||
320 | /** |
||
321 | * Set the optional thumbnail to appear within the attachment. |
||
322 | * |
||
323 | * @param string $thumb_url |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function setThumbUrl($thumb_url) |
||
332 | |||
333 | /** |
||
334 | * Get the text that should appear above the formatted data. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getPretext() |
||
342 | |||
343 | /** |
||
344 | * Set the text that should appear above the formatted data. |
||
345 | * |
||
346 | * @param string $pretext |
||
347 | * @return $this |
||
348 | */ |
||
349 | public function setPretext($pretext) |
||
355 | |||
356 | /** |
||
357 | * Get the color to use for the attachment. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function getColor() |
||
365 | |||
366 | /** |
||
367 | * Set the color to use for the attachment. |
||
368 | * |
||
369 | * @param string $colour |
||
370 | * @return $this |
||
371 | */ |
||
372 | public function setColor($color) |
||
378 | |||
379 | /** |
||
380 | * Get the footer to use for the attachment. |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getFooter() |
||
388 | |||
389 | /** |
||
390 | * Set the footer text to use for the attachment. |
||
391 | * |
||
392 | * @param string $footer |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setFooter($footer) |
||
401 | |||
402 | /** |
||
403 | * Get the footer icon to use for the attachment. |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getFooterIcon() |
||
411 | |||
412 | /** |
||
413 | * Set the footer icon to use for the attachment. |
||
414 | * |
||
415 | * @param string $footerIcon |
||
416 | * @return $this |
||
417 | */ |
||
418 | public function setFooterIcon($footerIcon) |
||
424 | |||
425 | /** |
||
426 | * Get the timestamp to use for the attachment. |
||
427 | * |
||
428 | * @return \DateTime |
||
429 | */ |
||
430 | public function getTimestamp() |
||
434 | |||
435 | /** |
||
436 | * Set the timestamp to use for the attachment. |
||
437 | * |
||
438 | * @param \DateTime $timestamp |
||
439 | * @return $this |
||
440 | */ |
||
441 | public function setTimestamp($timestamp) |
||
447 | |||
448 | /** |
||
449 | * Get the title to use for the attachment. |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getTitle() |
||
457 | |||
458 | /** |
||
459 | * Set the title to use for the attachment. |
||
460 | * |
||
461 | * @param string $title |
||
462 | * @return $this |
||
463 | */ |
||
464 | public function setTitle($title) |
||
470 | |||
471 | /** |
||
472 | * Get the title link to use for the attachment. |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | public function getTitleLink() |
||
480 | |||
481 | /** |
||
482 | * Set the title link to use for the attachment. |
||
483 | * |
||
484 | * @param string $title_link |
||
485 | * @return $this |
||
486 | */ |
||
487 | public function setTitleLink($title_link) |
||
493 | |||
494 | /** |
||
495 | * Get the author name to use for the attachment. |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getAuthorName() |
||
503 | |||
504 | /** |
||
505 | * Set the author name to use for the attachment. |
||
506 | * |
||
507 | * @param string $author_name |
||
508 | * @return $this |
||
509 | */ |
||
510 | public function setAuthorName($author_name) |
||
516 | |||
517 | /** |
||
518 | * Get the author link to use for the attachment. |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | public function getAuthorLink() |
||
526 | |||
527 | /** |
||
528 | * Set the auhtor link to use for the attachment. |
||
529 | * |
||
530 | * @param string $author_link |
||
531 | * @return $this |
||
532 | */ |
||
533 | public function setAuthorLink($author_link) |
||
539 | |||
540 | /** |
||
541 | * Get the author icon to use for the attachment. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | public function getAuthorIcon() |
||
549 | |||
550 | /** |
||
551 | * Set the author icon to use for the attachment. |
||
552 | * |
||
553 | * @param string $author_icon |
||
554 | * @return $this |
||
555 | */ |
||
556 | public function setAuthorIcon($author_icon) |
||
562 | |||
563 | /** |
||
564 | * Get the fields for the attachment. |
||
565 | * |
||
566 | * @return array |
||
567 | */ |
||
568 | public function getFields() |
||
572 | |||
573 | /** |
||
574 | * Set the fields for the attachment. |
||
575 | * |
||
576 | * @param array $fields |
||
577 | * @return $this |
||
578 | */ |
||
579 | public function setFields(array $fields) |
||
589 | |||
590 | /** |
||
591 | * Add a field to the attachment. |
||
592 | * |
||
593 | * @param mixed $field |
||
594 | * @return $this |
||
595 | */ |
||
596 | public function addField($field) |
||
610 | |||
611 | /** |
||
612 | * Clear the fields for the attachment. |
||
613 | * |
||
614 | * @return $this |
||
615 | */ |
||
616 | public function clearFields() |
||
622 | |||
623 | /** |
||
624 | * Clear the actions for the attachment. |
||
625 | * |
||
626 | * @return $this |
||
627 | */ |
||
628 | public function clearActions() |
||
634 | |||
635 | /** |
||
636 | * Get the fields Slack should interpret in its |
||
637 | * Markdown-like language. |
||
638 | * |
||
639 | * @return array |
||
640 | */ |
||
641 | public function getMarkdownFields() |
||
645 | |||
646 | /** |
||
647 | * Set the fields Slack should interpret in its |
||
648 | * Markdown-like language. |
||
649 | * |
||
650 | * @param array $fields |
||
651 | * @return $this |
||
652 | */ |
||
653 | public function setMarkdownFields(array $fields) |
||
659 | |||
660 | /** |
||
661 | * Get the collection of actions (buttons) to include in the attachment. |
||
662 | * |
||
663 | * @return AttachmentAction[] |
||
664 | */ |
||
665 | public function getActions() |
||
669 | |||
670 | /** |
||
671 | * Set the collection of actions (buttons) to include in the attachment. |
||
672 | * |
||
673 | * @param array $actions |
||
674 | * @return Attachment |
||
675 | */ |
||
676 | public function setActions($actions) |
||
686 | |||
687 | /** |
||
688 | * Add an action to the attachment. |
||
689 | * |
||
690 | * @param mixed $action |
||
691 | * @return $this |
||
692 | */ |
||
693 | public function addAction($action) |
||
707 | |||
708 | /** |
||
709 | * Convert this attachment to its array representation. |
||
710 | * |
||
711 | * @return array |
||
712 | */ |
||
713 | public function toArray() |
||
739 | |||
740 | /** |
||
741 | * Iterates over all fields in this attachment and returns |
||
742 | * them in their array form. |
||
743 | * |
||
744 | * @return array |
||
745 | */ |
||
746 | protected function getFieldsAsArrays() |
||
756 | |||
757 | /** |
||
758 | * Iterates over all actions in this attachment and returns |
||
759 | * them in their array form. |
||
760 | * |
||
761 | * @return array |
||
762 | */ |
||
763 | protected function getActionsAsArrays() |
||
773 | } |
||
774 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.