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 | * The callback_id used by the API receiving | ||
| 124 | * the POST request to respond to interactive buttons | ||
| 125 | * | ||
| 126 | * @var string | ||
| 127 | */ | ||
| 128 | protected $callback_id; | ||
| 129 | |||
| 130 | /** | ||
| 131 | * A collection of actions (buttons) to include in the attachment. | ||
| 132 | * A maximum of 5 actions may be provided. | ||
| 133 | * | ||
| 134 | * @var array | ||
| 135 | */ | ||
| 136 | protected $actions = []; | ||
| 137 | |||
| 138 | /** | ||
| 139 | * Instantiate a new Attachment. | ||
| 140 | * | ||
| 141 | * @param array $attributes | ||
| 142 | * @return void | ||
|  | |||
| 143 | */ | ||
| 144 | public function __construct(array $attributes) | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Get the fallback text. | ||
| 221 | * | ||
| 222 | * @return string | ||
| 223 | */ | ||
| 224 | public function getFallback() | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Set the fallback text. | ||
| 231 | * | ||
| 232 | * @param string $fallback | ||
| 233 | * @return $this | ||
| 234 | */ | ||
| 235 | public function setFallback($fallback) | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Get the optional text to appear within the attachment. | ||
| 244 | * | ||
| 245 | * @return string | ||
| 246 | */ | ||
| 247 | public function getText() | ||
| 251 | |||
| 252 | /** | ||
| 253 | * Set the optional text to appear within the attachment. | ||
| 254 | * | ||
| 255 | * @param string $text | ||
| 256 | * @return $this | ||
| 257 | */ | ||
| 258 | public function setText($text) | ||
| 264 | |||
| 265 | /** | ||
| 266 | * Get the optional image to appear within the attachment. | ||
| 267 | * | ||
| 268 | * @return string | ||
| 269 | */ | ||
| 270 | public function getImageUrl() | ||
| 274 | |||
| 275 | /** | ||
| 276 | * Set the optional image to appear within the attachment. | ||
| 277 | * | ||
| 278 | * @param string $image_url | ||
| 279 | * @return $this | ||
| 280 | */ | ||
| 281 | public function setImageUrl($image_url) | ||
| 287 | |||
| 288 | /** | ||
| 289 | * Get the optional thumbnail to appear within the attachment. | ||
| 290 | * | ||
| 291 | * @return string | ||
| 292 | */ | ||
| 293 | public function getThumbUrl() | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Set the optional thumbnail to appear within the attachment. | ||
| 300 | * | ||
| 301 | * @param string $thumb_url | ||
| 302 | * @return $this | ||
| 303 | */ | ||
| 304 | public function setThumbUrl($thumb_url) | ||
| 310 | |||
| 311 | /** | ||
| 312 | * Get the text that should appear above the formatted data. | ||
| 313 | * | ||
| 314 | * @return string | ||
| 315 | */ | ||
| 316 | public function getPretext() | ||
| 320 | |||
| 321 | /** | ||
| 322 | * Set the text that should appear above the formatted data. | ||
| 323 | * | ||
| 324 | * @param string $pretext | ||
| 325 | * @return $this | ||
| 326 | */ | ||
| 327 | public function setPretext($pretext) | ||
| 333 | |||
| 334 | /** | ||
| 335 | * Get the color to use for the attachment. | ||
| 336 | * | ||
| 337 | * @return string | ||
| 338 | */ | ||
| 339 | public function getColor() | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Set the color to use for the attachment. | ||
| 346 | * | ||
| 347 | * @param string $color | ||
| 348 | * @return $this | ||
| 349 | */ | ||
| 350 | public function setColor($color) | ||
| 356 | |||
| 357 | /** | ||
| 358 | * Get the footer to use for the attachment. | ||
| 359 | * | ||
| 360 | * @return string | ||
| 361 | */ | ||
| 362 | public function getFooter() | ||
| 366 | |||
| 367 | /** | ||
| 368 | * Set the footer text to use for the attachment. | ||
| 369 | * | ||
| 370 | * @param string $footer | ||
| 371 | * @return $this | ||
| 372 | */ | ||
| 373 | public function setFooter($footer) | ||
| 379 | |||
| 380 | /** | ||
| 381 | * Get the footer icon to use for the attachment. | ||
| 382 | * | ||
| 383 | * @return string | ||
| 384 | */ | ||
| 385 | public function getFooterIcon() | ||
| 389 | |||
| 390 | /** | ||
| 391 | * Set the footer icon to use for the attachment. | ||
| 392 | * | ||
| 393 | * @param string $footerIcon | ||
| 394 | * @return $this | ||
| 395 | */ | ||
| 396 | public function setFooterIcon($footerIcon) | ||
| 402 | |||
| 403 | /** | ||
| 404 | * Get the timestamp to use for the attachment. | ||
| 405 | * | ||
| 406 | * @return \DateTime | ||
| 407 | */ | ||
| 408 | public function getTimestamp() | ||
| 412 | |||
| 413 | /** | ||
| 414 | * Set the timestamp to use for the attachment. | ||
| 415 | * | ||
| 416 | * @param \DateTime $timestamp | ||
| 417 | * @return $this | ||
| 418 | */ | ||
| 419 | public function setTimestamp($timestamp) | ||
| 425 | |||
| 426 | /** | ||
| 427 | * Get the title to use for the attachment. | ||
| 428 | * | ||
| 429 | * @return string | ||
| 430 | */ | ||
| 431 | public function getTitle() | ||
| 435 | |||
| 436 | /** | ||
| 437 | * Set the title to use for the attachment. | ||
| 438 | * | ||
| 439 | * @param string $title | ||
| 440 | * @return $this | ||
| 441 | */ | ||
| 442 | public function setTitle($title) | ||
| 448 | |||
| 449 | /** | ||
| 450 | * Get the title link to use for the attachment. | ||
| 451 | * | ||
| 452 | * @return string | ||
| 453 | */ | ||
| 454 | public function getTitleLink() | ||
| 458 | |||
| 459 | /** | ||
| 460 | * Set the title link to use for the attachment. | ||
| 461 | * | ||
| 462 | * @param string $title_link | ||
| 463 | * @return $this | ||
| 464 | */ | ||
| 465 | public function setTitleLink($title_link) | ||
| 471 | |||
| 472 | /** | ||
| 473 | * Get the author name to use for the attachment. | ||
| 474 | * | ||
| 475 | * @return string | ||
| 476 | */ | ||
| 477 | public function getAuthorName() | ||
| 481 | |||
| 482 | /** | ||
| 483 | * Set the author name to use for the attachment. | ||
| 484 | * | ||
| 485 | * @param string $author_name | ||
| 486 | * @return $this | ||
| 487 | */ | ||
| 488 | public function setAuthorName($author_name) | ||
| 494 | |||
| 495 | /** | ||
| 496 | * Get the author link to use for the attachment. | ||
| 497 | * | ||
| 498 | * @return string | ||
| 499 | */ | ||
| 500 | public function getAuthorLink() | ||
| 504 | |||
| 505 | /** | ||
| 506 | * Set the auhtor link to use for the attachment. | ||
| 507 | * | ||
| 508 | * @param string $author_link | ||
| 509 | * @return $this | ||
| 510 | */ | ||
| 511 | public function setAuthorLink($author_link) | ||
| 517 | |||
| 518 | /** | ||
| 519 | * Get the author icon to use for the attachment. | ||
| 520 | * | ||
| 521 | * @return string | ||
| 522 | */ | ||
| 523 | public function getAuthorIcon() | ||
| 527 | |||
| 528 | /** | ||
| 529 | * Set the author icon to use for the attachment. | ||
| 530 | * | ||
| 531 | * @param string $author_icon | ||
| 532 | * @return $this | ||
| 533 | */ | ||
| 534 | public function setAuthorIcon($author_icon) | ||
| 540 | |||
| 541 | /** | ||
| 542 | * Get the fields for the attachment. | ||
| 543 | * | ||
| 544 | * @return array | ||
| 545 | */ | ||
| 546 | public function getFields() | ||
| 550 | |||
| 551 | /** | ||
| 552 | * Set the fields for the attachment. | ||
| 553 | * | ||
| 554 | * @param array $fields | ||
| 555 | * @return $this | ||
| 556 | */ | ||
| 557 | public function setFields(array $fields) | ||
| 567 | |||
| 568 | /** | ||
| 569 | * Add a field to the attachment. | ||
| 570 | * | ||
| 571 | * @param mixed $field | ||
| 572 | * @return $this | ||
| 573 | */ | ||
| 574 | public function addField($field) | ||
| 588 | |||
| 589 | /** | ||
| 590 | * Clear the fields for the attachment. | ||
| 591 | * | ||
| 592 | * @return $this | ||
| 593 | */ | ||
| 594 | public function clearFields() | ||
| 600 | |||
| 601 | /** | ||
| 602 | * Clear the actions for the attachment. | ||
| 603 | * | ||
| 604 | * @return $this | ||
| 605 | */ | ||
| 606 | public function clearActions() | ||
| 612 | |||
| 613 | /** | ||
| 614 | * Get the fields Slack should interpret in its | ||
| 615 | * Markdown-like language. | ||
| 616 | * | ||
| 617 | * @return array | ||
| 618 | */ | ||
| 619 | public function getMarkdownFields() | ||
| 623 | |||
| 624 | /** | ||
| 625 | * Set the fields Slack should interpret in its | ||
| 626 | * Markdown-like language. | ||
| 627 | * | ||
| 628 | * @param array $fields | ||
| 629 | * @return $this | ||
| 630 | */ | ||
| 631 | public function setMarkdownFields(array $fields) | ||
| 637 | |||
| 638 | /** | ||
| 639 | * Get the callback_id specified for the attachment | ||
| 640 | * | ||
| 641 | * @return string | ||
| 642 | */ | ||
| 643 | public function getCallbackId() | ||
| 647 | |||
| 648 | /** | ||
| 649 | * Set the callback_id for the attachment | ||
| 650 | * | ||
| 651 | * @param string | ||
| 652 | * @return $this | ||
| 653 | */ public function setCallbackId($callback_id) | ||
| 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
@returnannotation 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.