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 | * A collection of actions (buttons) to include in the attachment. |
||
| 103 | * A maximum of 5 actions may be provided. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $actions = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Instantiate a new Attachment. |
||
| 111 | * |
||
| 112 | * @param array $attributes |
||
| 113 | * @return void |
||
|
|
|||
| 114 | */ |
||
| 115 | public function __construct(array $attributes) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the fallback text. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getFallback() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set the fallback text. |
||
| 186 | * |
||
| 187 | * @param string $fallback |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setFallback($fallback) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the optional text to appear within the attachment. |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getText() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the optional text to appear within the attachment. |
||
| 209 | * |
||
| 210 | * @param string $text |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setText($text) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the optional image to appear within the attachment. |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getImageUrl() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the optional image to appear within the attachment. |
||
| 232 | * |
||
| 233 | * @param string $image_url |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function setImageUrl($image_url) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the optional thumbnail to appear within the attachment. |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getThumbUrl() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set the optional thumbnail to appear within the attachment. |
||
| 255 | * |
||
| 256 | * @param string $thumb_url |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function setThumbUrl($thumb_url) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get the text that should appear above the formatted data. |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getPretext() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set the text that should appear above the formatted data. |
||
| 278 | * |
||
| 279 | * @param string $pretext |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function setPretext($pretext) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the color to use for the attachment. |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getColor() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set the color to use for the attachment. |
||
| 301 | * |
||
| 302 | * @param string $colour |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function setColor($color) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the title to use for the attachment. |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getTitle() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set the title to use for the attachment. |
||
| 324 | * |
||
| 325 | * @param string $title |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function setTitle($title) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the title link to use for the attachment. |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getTitleLink() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set the title link to use for the attachment. |
||
| 347 | * |
||
| 348 | * @param string $title_link |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | public function setTitleLink($title_link) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get the author name to use for the attachment. |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getAuthorName() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set the author name to use for the attachment. |
||
| 370 | * |
||
| 371 | * @param string $author_name |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function setAuthorName($author_name) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the author link to use for the attachment. |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function getAuthorLink() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the auhtor link to use for the attachment. |
||
| 393 | * |
||
| 394 | * @param string $author_link |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | public function setAuthorLink($author_link) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get the author icon to use for the attachment. |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function getAuthorIcon() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Set the author icon to use for the attachment. |
||
| 416 | * |
||
| 417 | * @param string $author_icon |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function setAuthorIcon($author_icon) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get the fields for the attachment. |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | public function getFields() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set the fields for the attachment. |
||
| 439 | * |
||
| 440 | * @param array $fields |
||
| 441 | * @return void |
||
| 442 | */ |
||
| 443 | public function setFields(array $fields) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Add a field to the attachment. |
||
| 456 | * |
||
| 457 | * @param mixed $field |
||
| 458 | * @return $this |
||
| 459 | */ |
||
| 460 | public function addField($field) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Clear the fields for the attachment. |
||
| 477 | * |
||
| 478 | * @return $this |
||
| 479 | */ |
||
| 480 | public function clearFields() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Clear the actions for the attachment. |
||
| 489 | * |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | public function clearActions() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the fields Slack should interpret in its |
||
| 501 | * Markdown-like language. |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | public function getMarkdownFields() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Set the fields Slack should interpret in its |
||
| 512 | * Markdown-like language. |
||
| 513 | * |
||
| 514 | * @param array $fields |
||
| 515 | * @return $this |
||
| 516 | */ |
||
| 517 | public function setMarkdownFields(array $fields) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get the collection of actions (buttons) to include in the attachment. |
||
| 526 | * |
||
| 527 | * @return AttachmentAction[] |
||
| 528 | */ |
||
| 529 | public function getActions() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Set the collection of actions (buttons) to include in the attachment. |
||
| 536 | * |
||
| 537 | * @param array $actions |
||
| 538 | * @return Attachment |
||
| 539 | */ |
||
| 540 | public function setActions($actions) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Add an action to the attachment. |
||
| 553 | * |
||
| 554 | * @param mixed $action |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function addAction($action) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Convert this attachment to its array representation. |
||
| 574 | * |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | public function toArray() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Iterates over all fields in this attachment and returns |
||
| 602 | * them in their array form. |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | protected function getFieldsAsArrays() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Iterates over all actions in this attachment and returns |
||
| 619 | * them in their array form. |
||
| 620 | * |
||
| 621 | * @return array |
||
| 622 | */ |
||
| 623 | protected function getActionsAsArrays() |
||
| 633 | } |
||
| 634 |
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.