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 | * Optional text that should appear within the attachment. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $text; |
||
22 | |||
23 | /** |
||
24 | * Optional image that should appear within the attachment. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $image_url; |
||
29 | |||
30 | /** |
||
31 | * Optional thumbnail that should appear within the attachment. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $thumb_url; |
||
36 | |||
37 | /** |
||
38 | * Optional text that should appear above the formatted data. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $pretext; |
||
43 | |||
44 | /** |
||
45 | * Optional title for the attachment. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $title; |
||
50 | |||
51 | /** |
||
52 | * Optional title link for the attachment. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $title_link; |
||
57 | |||
58 | /** |
||
59 | * Optional author name for the attachment. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $author_name; |
||
64 | |||
65 | /** |
||
66 | * Optional author link for the attachment. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $author_link; |
||
71 | |||
72 | /** |
||
73 | * Optional author icon for the attachment. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $author_icon; |
||
78 | |||
79 | /** |
||
80 | * The color to use for the attachment. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $color = 'good'; |
||
85 | |||
86 | /** |
||
87 | * The text to use for the attachment footer. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $footer; |
||
92 | |||
93 | /** |
||
94 | * The icon to use for the attachment footer. |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $footer_icon; |
||
99 | |||
100 | /** |
||
101 | * The timestamp to use for the attachment. |
||
102 | * |
||
103 | * @var \DateTime |
||
104 | */ |
||
105 | protected $timestamp; |
||
106 | |||
107 | /** |
||
108 | * The fields of the attachment. |
||
109 | * |
||
110 | * @var array |
||
111 | */ |
||
112 | protected $fields = []; |
||
113 | |||
114 | /** |
||
115 | * The fields of the attachment that Slack should interpret |
||
116 | * with its Markdown-like language. |
||
117 | * |
||
118 | * @var array |
||
119 | */ |
||
120 | protected $markdown_fields = []; |
||
121 | |||
122 | /** |
||
123 | * A collection of actions (buttons) to include in the attachment. |
||
124 | * A maximum of 5 actions may be provided. |
||
125 | * |
||
126 | * @var array |
||
127 | */ |
||
128 | protected $actions = []; |
||
129 | |||
130 | /** |
||
131 | * Instantiate a new Attachment. |
||
132 | * |
||
133 | * @param array $attributes |
||
134 | * @return void |
||
|
|||
135 | */ |
||
136 | public function __construct(array $attributes) |
||
206 | |||
207 | /** |
||
208 | * Get the fallback text. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getFallback() |
||
216 | |||
217 | /** |
||
218 | * Set the fallback text. |
||
219 | * |
||
220 | * @param string $fallback |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function setFallback($fallback) |
||
229 | |||
230 | /** |
||
231 | * Get the optional text to appear within the attachment. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getText() |
||
239 | |||
240 | /** |
||
241 | * Set the optional text to appear within the attachment. |
||
242 | * |
||
243 | * @param string $text |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function setText($text) |
||
252 | |||
253 | /** |
||
254 | * Get the optional image to appear within the attachment. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getImageUrl() |
||
262 | |||
263 | /** |
||
264 | * Set the optional image to appear within the attachment. |
||
265 | * |
||
266 | * @param string $image_url |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function setImageUrl($image_url) |
||
275 | |||
276 | /** |
||
277 | * Get the optional thumbnail to appear within the attachment. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getThumbUrl() |
||
285 | |||
286 | /** |
||
287 | * Set the optional thumbnail to appear within the attachment. |
||
288 | * |
||
289 | * @param string $thumb_url |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function setThumbUrl($thumb_url) |
||
298 | |||
299 | /** |
||
300 | * Get the text that should appear above the formatted data. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getPretext() |
||
308 | |||
309 | /** |
||
310 | * Set the text that should appear above the formatted data. |
||
311 | * |
||
312 | * @param string $pretext |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function setPretext($pretext) |
||
321 | |||
322 | /** |
||
323 | * Get the color to use for the attachment. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getColor() |
||
331 | |||
332 | /** |
||
333 | * Set the color to use for the attachment. |
||
334 | * |
||
335 | * @param string $color |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function setColor($color) |
||
344 | |||
345 | /** |
||
346 | * Get the footer to use for the attachment. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | public function getFooter() |
||
354 | |||
355 | /** |
||
356 | * Set the footer text to use for the attachment. |
||
357 | * |
||
358 | * @param string $footer |
||
359 | * @return $this |
||
360 | */ |
||
361 | public function setFooter($footer) |
||
367 | |||
368 | /** |
||
369 | * Get the footer icon to use for the attachment. |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | public function getFooterIcon() |
||
377 | |||
378 | /** |
||
379 | * Set the footer icon to use for the attachment. |
||
380 | * |
||
381 | * @param string $footerIcon |
||
382 | * @return $this |
||
383 | */ |
||
384 | public function setFooterIcon($footerIcon) |
||
390 | |||
391 | /** |
||
392 | * Get the timestamp to use for the attachment. |
||
393 | * |
||
394 | * @return \DateTime |
||
395 | */ |
||
396 | public function getTimestamp() |
||
400 | |||
401 | /** |
||
402 | * Set the timestamp to use for the attachment. |
||
403 | * |
||
404 | * @param \DateTime $timestamp |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function setTimestamp($timestamp) |
||
413 | |||
414 | /** |
||
415 | * Get the title to use for the attachment. |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public function getTitle() |
||
423 | |||
424 | /** |
||
425 | * Set the title to use for the attachment. |
||
426 | * |
||
427 | * @param string $title |
||
428 | * @return $this |
||
429 | */ |
||
430 | public function setTitle($title) |
||
436 | |||
437 | /** |
||
438 | * Get the title link to use for the attachment. |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | public function getTitleLink() |
||
446 | |||
447 | /** |
||
448 | * Set the title link to use for the attachment. |
||
449 | * |
||
450 | * @param string $title_link |
||
451 | * @return $this |
||
452 | */ |
||
453 | public function setTitleLink($title_link) |
||
459 | |||
460 | /** |
||
461 | * Get the author name to use for the attachment. |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | public function getAuthorName() |
||
469 | |||
470 | /** |
||
471 | * Set the author name to use for the attachment. |
||
472 | * |
||
473 | * @param string $author_name |
||
474 | * @return $this |
||
475 | */ |
||
476 | public function setAuthorName($author_name) |
||
482 | |||
483 | /** |
||
484 | * Get the author link to use for the attachment. |
||
485 | * |
||
486 | * @return string |
||
487 | */ |
||
488 | public function getAuthorLink() |
||
492 | |||
493 | /** |
||
494 | * Set the auhtor link to use for the attachment. |
||
495 | * |
||
496 | * @param string $author_link |
||
497 | * @return $this |
||
498 | */ |
||
499 | public function setAuthorLink($author_link) |
||
505 | |||
506 | /** |
||
507 | * Get the author icon to use for the attachment. |
||
508 | * |
||
509 | * @return string |
||
510 | */ |
||
511 | public function getAuthorIcon() |
||
515 | |||
516 | /** |
||
517 | * Set the author icon to use for the attachment. |
||
518 | * |
||
519 | * @param string $author_icon |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function setAuthorIcon($author_icon) |
||
528 | |||
529 | /** |
||
530 | * Get the fields for the attachment. |
||
531 | * |
||
532 | * @return array |
||
533 | */ |
||
534 | public function getFields() |
||
538 | |||
539 | /** |
||
540 | * Set the fields for the attachment. |
||
541 | * |
||
542 | * @param array $fields |
||
543 | * @return $this |
||
544 | */ |
||
545 | public function setFields(array $fields) |
||
555 | |||
556 | /** |
||
557 | * Add a field to the attachment. |
||
558 | * |
||
559 | * @param mixed $field |
||
560 | * @return $this |
||
561 | */ |
||
562 | public function addField($field) |
||
576 | |||
577 | /** |
||
578 | * Clear the fields for the attachment. |
||
579 | * |
||
580 | * @return $this |
||
581 | */ |
||
582 | public function clearFields() |
||
588 | |||
589 | /** |
||
590 | * Clear the actions for the attachment. |
||
591 | * |
||
592 | * @return $this |
||
593 | */ |
||
594 | public function clearActions() |
||
600 | |||
601 | /** |
||
602 | * Get the fields Slack should interpret in its |
||
603 | * Markdown-like language. |
||
604 | * |
||
605 | * @return array |
||
606 | */ |
||
607 | public function getMarkdownFields() |
||
611 | |||
612 | /** |
||
613 | * Set the fields Slack should interpret in its |
||
614 | * Markdown-like language. |
||
615 | * |
||
616 | * @param array $fields |
||
617 | * @return $this |
||
618 | */ |
||
619 | public function setMarkdownFields(array $fields) |
||
625 | |||
626 | /** |
||
627 | * Get the collection of actions (buttons) to include in the attachment. |
||
628 | * |
||
629 | * @return AttachmentAction[] |
||
630 | */ |
||
631 | public function getActions() |
||
635 | |||
636 | /** |
||
637 | * Set the collection of actions (buttons) to include in the attachment. |
||
638 | * |
||
639 | * @param array $actions |
||
640 | * @return Attachment |
||
641 | */ |
||
642 | public function setActions($actions) |
||
652 | |||
653 | /** |
||
654 | * Add an action to the attachment. |
||
655 | * |
||
656 | * @param mixed $action |
||
657 | * @return $this |
||
658 | */ |
||
659 | public function addAction($action) |
||
673 | |||
674 | /** |
||
675 | * Convert this attachment to its array representation. |
||
676 | * |
||
677 | * @return array |
||
678 | */ |
||
679 | public function toArray() |
||
704 | |||
705 | /** |
||
706 | * Iterates over all fields in this attachment and returns |
||
707 | * them in their array form. |
||
708 | * |
||
709 | * @return array |
||
710 | */ |
||
711 | protected function getFieldsAsArrays() |
||
721 | |||
722 | /** |
||
723 | * Iterates over all actions in this attachment and returns |
||
724 | * them in their array form. |
||
725 | * |
||
726 | * @return array |
||
727 | */ |
||
728 | protected function getActionsAsArrays() |
||
738 | } |
||
739 |
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.