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 |
||
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) |
||
144 | { |
||
145 | if (isset($attributes['fallback'])) { |
||
146 | $this->setFallback($attributes['fallback']); |
||
147 | } |
||
148 | |||
149 | if (isset($attributes['callback_id'])) { |
||
150 | $this->setCallbackId($attributes['callback_id']); |
||
151 | } |
||
152 | |||
153 | if (isset($attributes['text'])) { |
||
154 | $this->setText($attributes['text']); |
||
155 | } |
||
156 | |||
157 | if (isset($attributes['image_url'])) { |
||
158 | $this->setImageUrl($attributes['image_url']); |
||
159 | } |
||
160 | |||
161 | if (isset($attributes['thumb_url'])) { |
||
162 | $this->setThumbUrl($attributes['thumb_url']); |
||
163 | } |
||
164 | |||
165 | if (isset($attributes['pretext'])) { |
||
166 | $this->setPretext($attributes['pretext']); |
||
167 | } |
||
168 | |||
169 | if (isset($attributes['color'])) { |
||
170 | $this->setColor($attributes['color']); |
||
171 | } |
||
172 | |||
173 | if (isset($attributes['footer'])) { |
||
174 | $this->setFooter($attributes['footer']); |
||
175 | } |
||
176 | |||
177 | if (isset($attributes['footer_icon'])) { |
||
178 | $this->setFooterIcon($attributes['footer_icon']); |
||
179 | } |
||
180 | |||
181 | if (isset($attributes['timestamp'])) { |
||
182 | $this->setTimestamp($attributes['timestamp']); |
||
183 | } |
||
184 | |||
185 | if (isset($attributes['fields'])) { |
||
186 | $this->setFields($attributes['fields']); |
||
187 | } |
||
188 | |||
189 | if (isset($attributes['mrkdwn_in'])) { |
||
190 | $this->setMarkdownFields($attributes['mrkdwn_in']); |
||
191 | } |
||
192 | |||
193 | if (isset($attributes['title'])) { |
||
194 | $this->setTitle($attributes['title']); |
||
195 | } |
||
196 | |||
197 | if (isset($attributes['title_link'])) { |
||
198 | $this->setTitleLink($attributes['title_link']); |
||
199 | } |
||
200 | |||
201 | if (isset($attributes['author_name'])) { |
||
202 | $this->setAuthorName($attributes['author_name']); |
||
203 | } |
||
204 | |||
205 | if (isset($attributes['author_link'])) { |
||
206 | $this->setAuthorLink($attributes['author_link']); |
||
207 | } |
||
208 | |||
209 | if (isset($attributes['author_icon'])) { |
||
210 | $this->setAuthorIcon($attributes['author_icon']); |
||
211 | } |
||
212 | |||
213 | if (isset($attributes['actions'])) { |
||
214 | $this->setActions($attributes['actions']); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get the fallback text. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getFallback() |
||
224 | { |
||
225 | return $this->fallback; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Set the fallback text. |
||
230 | * |
||
231 | * @param string $fallback |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setFallback($fallback) |
||
235 | { |
||
236 | $this->fallback = $fallback; |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Get the callback_id. |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getCallbackId() { |
||
248 | return $this->callback_id; |
||
249 | } |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Set the callback_id. |
||
254 | * |
||
255 | * @param string $fallback |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function setCallbackId($callback_id) { |
||
259 | $this->callback_id = $callback_id; |
||
260 | |||
261 | return $this; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get the optional text to appear within the attachment. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getText() |
||
270 | { |
||
271 | return $this->text; |
||
272 | } |
||
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) |
||
281 | { |
||
282 | $this->text = $text; |
||
283 | |||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Get the optional image to appear within the attachment. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getImageUrl() |
||
293 | { |
||
294 | return $this->image_url; |
||
295 | } |
||
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) |
||
304 | { |
||
305 | $this->image_url = $image_url; |
||
306 | |||
307 | return $this; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Get the optional thumbnail to appear within the attachment. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getThumbUrl() |
||
316 | { |
||
317 | return $this->thumb_url; |
||
318 | } |
||
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) |
||
327 | { |
||
328 | $this->thumb_url = $thumb_url; |
||
329 | |||
330 | return $this; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Get the text that should appear above the formatted data. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getPretext() |
||
339 | { |
||
340 | return $this->pretext; |
||
341 | } |
||
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) |
||
350 | { |
||
351 | $this->pretext = $pretext; |
||
352 | |||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Get the color to use for the attachment. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function getColor() |
||
362 | { |
||
363 | return $this->color; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Set the color to use for the attachment. |
||
368 | * |
||
369 | * @param string $color |
||
370 | * @return $this |
||
371 | */ |
||
372 | public function setColor($color) |
||
373 | { |
||
374 | $this->color = $color; |
||
375 | |||
376 | return $this; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Get the footer to use for the attachment. |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getFooter() |
||
385 | { |
||
386 | return $this->footer; |
||
387 | } |
||
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) |
||
396 | { |
||
397 | $this->footer = $footer; |
||
398 | |||
399 | return $this; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Get the footer icon to use for the attachment. |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getFooterIcon() |
||
408 | { |
||
409 | return $this->footer_icon; |
||
410 | } |
||
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) |
||
419 | { |
||
420 | $this->footer_icon = $footerIcon; |
||
421 | |||
422 | return $this; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Get the timestamp to use for the attachment. |
||
427 | * |
||
428 | * @return \DateTime |
||
429 | */ |
||
430 | public function getTimestamp() |
||
431 | { |
||
432 | return $this->timestamp; |
||
433 | } |
||
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) |
||
442 | { |
||
443 | $this->timestamp = $timestamp; |
||
444 | |||
445 | return $this; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Get the title to use for the attachment. |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getTitle() |
||
454 | { |
||
455 | return $this->title; |
||
456 | } |
||
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) |
||
465 | { |
||
466 | $this->title = $title; |
||
467 | |||
468 | return $this; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Get the title link to use for the attachment. |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | public function getTitleLink() |
||
477 | { |
||
478 | return $this->title_link; |
||
479 | } |
||
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) |
||
488 | { |
||
489 | $this->title_link = $title_link; |
||
490 | |||
491 | return $this; |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Get the author name to use for the attachment. |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getAuthorName() |
||
500 | { |
||
501 | return $this->author_name; |
||
502 | } |
||
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) |
||
511 | { |
||
512 | $this->author_name = $author_name; |
||
513 | |||
514 | return $this; |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Get the author link to use for the attachment. |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | public function getAuthorLink() |
||
523 | { |
||
524 | return $this->author_link; |
||
525 | } |
||
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) |
||
534 | { |
||
535 | $this->author_link = $author_link; |
||
536 | |||
537 | return $this; |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Get the author icon to use for the attachment. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | public function getAuthorIcon() |
||
546 | { |
||
547 | return $this->author_icon; |
||
548 | } |
||
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) |
||
557 | { |
||
558 | $this->author_icon = $author_icon; |
||
559 | |||
560 | return $this; |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Get the fields for the attachment. |
||
565 | * |
||
566 | * @return array |
||
567 | */ |
||
568 | public function getFields() |
||
569 | { |
||
570 | return $this->fields; |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Set the fields for the attachment. |
||
575 | * |
||
576 | * @param array $fields |
||
577 | * @return $this |
||
578 | */ |
||
579 | public function setFields(array $fields) |
||
580 | { |
||
581 | $this->clearFields(); |
||
582 | |||
583 | foreach ($fields as $field) { |
||
584 | $this->addField($field); |
||
585 | } |
||
586 | |||
587 | return $this; |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Add a field to the attachment. |
||
592 | * |
||
593 | * @param mixed $field |
||
594 | * @return $this |
||
595 | */ |
||
596 | public function addField($field) |
||
597 | { |
||
598 | if ($field instanceof AttachmentField) { |
||
599 | $this->fields[] = $field; |
||
600 | |||
601 | return $this; |
||
602 | } elseif (is_array($field)) { |
||
603 | $this->fields[] = new AttachmentField($field); |
||
604 | |||
605 | return $this; |
||
606 | } |
||
607 | |||
608 | throw new InvalidArgumentException('The attachment field must be an instance of Maknz\Slack\AttachmentField or a keyed array'); |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * Clear the fields for the attachment. |
||
613 | * |
||
614 | * @return $this |
||
615 | */ |
||
616 | public function clearFields() |
||
617 | { |
||
618 | $this->fields = []; |
||
619 | |||
620 | return $this; |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Clear the actions for the attachment. |
||
625 | * |
||
626 | * @return $this |
||
627 | */ |
||
628 | public function clearActions() |
||
629 | { |
||
630 | $this->actions = []; |
||
631 | |||
632 | return $this; |
||
633 | } |
||
634 | |||
635 | /** |
||
636 | * Get the fields Slack should interpret in its |
||
637 | * Markdown-like language. |
||
638 | * |
||
639 | * @return array |
||
640 | */ |
||
641 | public function getMarkdownFields() |
||
642 | { |
||
643 | return $this->markdown_fields; |
||
644 | } |
||
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) |
||
654 | { |
||
655 | $this->markdown_fields = $fields; |
||
656 | |||
657 | return $this; |
||
658 | } |
||
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() |
||
714 | { |
||
715 | $data = [ |
||
716 | 'fallback' => $this->getFallback(), |
||
717 | 'callback_id' => $this->getCallbackId(), |
||
718 | 'text' => $this->getText(), |
||
719 | 'pretext' => $this->getPretext(), |
||
720 | 'color' => $this->getColor(), |
||
721 | 'footer' => $this->getFooter(), |
||
722 | 'footer_icon' => $this->getFooterIcon(), |
||
723 | 'ts' => $this->getTimestamp() ? $this->getTimestamp()->getTimestamp() : null, |
||
724 | 'mrkdwn_in' => $this->getMarkdownFields(), |
||
725 | 'image_url' => $this->getImageUrl(), |
||
726 | 'thumb_url' => $this->getThumbUrl(), |
||
727 | 'title' => $this->getTitle(), |
||
728 | 'title_link' => $this->getTitleLink(), |
||
729 | 'author_name' => $this->getAuthorName(), |
||
730 | 'author_link' => $this->getAuthorLink(), |
||
731 | 'author_icon' => $this->getAuthorIcon(), |
||
732 | ]; |
||
733 | |||
734 | $data['fields'] = $this->getFieldsAsArrays(); |
||
735 | $data['actions'] = $this->getActionsAsArrays(); |
||
736 | |||
737 | $data = array_filter($data, function($item) { |
||
738 | return !empty($item); |
||
739 | }); |
||
740 | |||
741 | return $data; |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * Iterates over all fields in this attachment and returns |
||
746 | * them in their array form. |
||
747 | * |
||
748 | * @return array |
||
749 | */ |
||
750 | protected function getFieldsAsArrays() |
||
760 | |||
761 | /** |
||
762 | * Iterates over all actions in this attachment and returns |
||
763 | * them in their array form. |
||
764 | * |
||
765 | * @return array |
||
766 | */ |
||
767 | protected function getActionsAsArrays() |
||
777 | } |
||
778 |
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.