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 fields of the attachment. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $fields = []; |
||
92 | |||
93 | /** |
||
94 | * The fields of the attachment that Slack should interpret |
||
95 | * with its Markdown-like language. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $markdown_fields = []; |
||
100 | |||
101 | /** |
||
102 | * The text to use for the footer attachment. |
||
103 | * |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $footer; |
||
107 | |||
108 | /** |
||
109 | * The icon to use for the footer attachment. |
||
110 | * |
||
111 | * @var string |
||
112 | */ |
||
113 | protected $footer_icon; |
||
114 | |||
115 | /** |
||
116 | * The timestamp to use for the footer attachment. |
||
117 | * |
||
118 | * @var integer |
||
119 | */ |
||
120 | protected $ts; |
||
121 | |||
122 | /** |
||
123 | * Instantiate a new Attachment. |
||
124 | * |
||
125 | * @param array $attributes |
||
126 | * @return void |
||
|
|||
127 | */ |
||
128 | public function __construct(array $attributes) |
||
194 | |||
195 | /** |
||
196 | * Get the fallback text. |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getFallback() |
||
204 | |||
205 | /** |
||
206 | * Set the fallback text. |
||
207 | * |
||
208 | * @param string $fallback |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function setFallback($fallback) |
||
217 | |||
218 | /** |
||
219 | * Get the optional text to appear within the attachment. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getText() |
||
227 | |||
228 | /** |
||
229 | * Set the optional text to appear within the attachment. |
||
230 | * |
||
231 | * @param string $text |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setText($text) |
||
240 | |||
241 | /** |
||
242 | * Get the optional image to appear within the attachment. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getImageUrl() |
||
250 | |||
251 | /** |
||
252 | * Set the optional image to appear within the attachment. |
||
253 | * |
||
254 | * @param string $image_url |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function setImageUrl($image_url) |
||
263 | |||
264 | /** |
||
265 | * Get the optional thumbnail to appear within the attachment. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getThumbUrl() |
||
273 | |||
274 | /** |
||
275 | * Set the optional thumbnail to appear within the attachment. |
||
276 | * |
||
277 | * @param string $thumb_url |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function setThumbUrl($thumb_url) |
||
286 | |||
287 | /** |
||
288 | * Get the text that should appear above the formatted data. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getPretext() |
||
296 | |||
297 | /** |
||
298 | * Set the text that should appear above the formatted data. |
||
299 | * |
||
300 | * @param string $pretext |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function setPretext($pretext) |
||
309 | |||
310 | /** |
||
311 | * Get the color to use for the attachment. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getColor() |
||
319 | |||
320 | /** |
||
321 | * Set the color to use for the attachment. |
||
322 | * |
||
323 | * @param string $colour |
||
324 | * @return void |
||
325 | */ |
||
326 | public function setColor($color) |
||
332 | |||
333 | /** |
||
334 | * Get the footer to use for the attachment. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getFooter() |
||
342 | |||
343 | /** |
||
344 | * Set the footer text to use for the attachment. |
||
345 | * |
||
346 | * @param string $footer |
||
347 | * @return void |
||
348 | */ |
||
349 | public function setFooter($footer) |
||
355 | |||
356 | /** |
||
357 | * Get the footer icon to use for the attachment. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function getFooterIcon() |
||
365 | |||
366 | /** |
||
367 | * Set the footer icon to use for the attachment. |
||
368 | * |
||
369 | * @param string $footerIcon |
||
370 | * @return void |
||
371 | */ |
||
372 | public function setFooterIcon($footerIcon) |
||
378 | |||
379 | /** |
||
380 | * Get the timestamp to use for the attachment. |
||
381 | * |
||
382 | * @return integer |
||
383 | */ |
||
384 | public function getTs() |
||
388 | |||
389 | /** |
||
390 | * Set the timestamp to use for the attachment. |
||
391 | * |
||
392 | * @param integer $ts |
||
393 | * @return void |
||
394 | */ |
||
395 | public function setTs($ts) |
||
401 | |||
402 | /** |
||
403 | * Get the title to use for the attachment. |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getTitle() |
||
411 | |||
412 | /** |
||
413 | * Set the title to use for the attachment. |
||
414 | * |
||
415 | * @param string $title |
||
416 | * @return void |
||
417 | */ |
||
418 | public function setTitle($title) |
||
424 | |||
425 | /** |
||
426 | * Get the title link to use for the attachment. |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | public function getTitleLink() |
||
434 | |||
435 | /** |
||
436 | * Set the title link to use for the attachment. |
||
437 | * |
||
438 | * @param string $title_link |
||
439 | * @return void |
||
440 | */ |
||
441 | public function setTitleLink($title_link) |
||
447 | |||
448 | /** |
||
449 | * Get the author name to use for the attachment. |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getAuthorName() |
||
457 | |||
458 | /** |
||
459 | * Set the author name to use for the attachment. |
||
460 | * |
||
461 | * @param string $author_name |
||
462 | * @return void |
||
463 | */ |
||
464 | public function setAuthorName($author_name) |
||
470 | |||
471 | /** |
||
472 | * Get the author link to use for the attachment. |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | public function getAuthorLink() |
||
480 | |||
481 | /** |
||
482 | * Set the auhtor link to use for the attachment. |
||
483 | * |
||
484 | * @param string $author_link |
||
485 | * @return void |
||
486 | */ |
||
487 | public function setAuthorLink($author_link) |
||
493 | |||
494 | /** |
||
495 | * Get the author icon to use for the attachment. |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getAuthorIcon() |
||
503 | |||
504 | /** |
||
505 | * Set the author icon to use for the attachment. |
||
506 | * |
||
507 | * @param string $author_icon |
||
508 | * @return void |
||
509 | */ |
||
510 | public function setAuthorIcon($author_icon) |
||
516 | |||
517 | /** |
||
518 | * Get the fields for the attachment. |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | public function getFields() |
||
526 | |||
527 | /** |
||
528 | * Set the fields for the attachment. |
||
529 | * |
||
530 | * @param array $fields |
||
531 | * @return void |
||
532 | */ |
||
533 | public function setFields(array $fields) |
||
543 | |||
544 | /** |
||
545 | * Add a field to the attachment. |
||
546 | * |
||
547 | * @param mixed $field |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function addField($field) |
||
564 | |||
565 | /** |
||
566 | * Clear the fields for the attachment. |
||
567 | * |
||
568 | * @return $this |
||
569 | */ |
||
570 | public function clearFields() |
||
576 | |||
577 | /** |
||
578 | * Get the fields Slack should interpret in its |
||
579 | * Markdown-like language. |
||
580 | * |
||
581 | * @return array |
||
582 | */ |
||
583 | public function getMarkdownFields() |
||
587 | |||
588 | /** |
||
589 | * Set the fields Slack should interpret in its |
||
590 | * Markdown-like language. |
||
591 | * |
||
592 | * @param array $fields |
||
593 | * @return $this |
||
594 | */ |
||
595 | public function setMarkdownFields(array $fields) |
||
601 | |||
602 | /** |
||
603 | * Convert this attachment to its array representation. |
||
604 | * |
||
605 | * @return array |
||
606 | */ |
||
607 | public function toArray() |
||
628 | |||
629 | /** |
||
630 | * Iterates over all fields in this attachment and returns |
||
631 | * them in their array form. |
||
632 | * |
||
633 | * @return array |
||
634 | */ |
||
635 | protected function getFieldsAsArrays() |
||
645 | } |
||
646 |
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.