Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Message implements JsonSerializable |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The BearyChat client for sending message. |
||
| 11 | * |
||
| 12 | * @var \ElfSundae\BearyChat\Client |
||
| 13 | */ |
||
| 14 | protected $client; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The text to be sent with the message. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $text; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The notification for the text. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $notification; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Indicates the text field should be parsed as markdown syntax. |
||
| 32 | * |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $markdown; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The channel that the message should be sent to. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $channel; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The user that the message should be sent to. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $user; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The attachments to be sent. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $attachments = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The default values for each attachment. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $attachmentDefaults = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Create a new message. |
||
| 67 | * |
||
| 68 | * @param \ElfSundae\BearyChat\Client|null $client |
||
| 69 | */ |
||
| 70 | 24 | public function __construct(Client $client = null) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get the BearyChat client for sending message. |
||
| 77 | * |
||
| 78 | * @return \ElfSundae\BearyChat\Client |
||
| 79 | */ |
||
| 80 | 3 | public function getClient() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set the BearyChat Client instance. |
||
| 87 | * |
||
| 88 | * @param \ElfSundae\BearyChat\Client|null $client |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | 24 | public function setClient($client) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Get the text. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 8 | public function getText() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Set the text. |
||
| 114 | * |
||
| 115 | * @param string $text |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | 7 | public function setText($text) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Set the text. |
||
| 127 | * |
||
| 128 | * @param string $text |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | 4 | public function text($text) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get the notification. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 8 | public function getNotification() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set the notification. |
||
| 148 | * |
||
| 149 | * @param string $notification |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | 5 | public function setNotification($notification) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Set the notification. |
||
| 161 | * |
||
| 162 | * @param string $notification |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | 1 | public function notification($notification) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Get the markdown. |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 9 | public function getMarkdown() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Set the markdown. |
||
| 182 | * |
||
| 183 | * @param bool $markdown |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | 5 | public function setMarkdown($markdown) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Set the markdown. |
||
| 195 | * |
||
| 196 | * @param bool $markdown |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | 1 | public function markdown($markdown = true) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get the channel which the message should be sent to. |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 11 | public function getChannel() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Set the channel which the message should be sent to. |
||
| 216 | * |
||
| 217 | * @param string $channel |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | 6 | public function setChannel($channel) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Set the channel which the message should be sent to. |
||
| 233 | * |
||
| 234 | * @param string $channel |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | 4 | public function channel($channel) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get the user which the message should be sent to. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 12 | public function getUser() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Set the user which the message should be sent to. |
||
| 254 | * |
||
| 255 | * @param string $user |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | 11 | public function setUser($user) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Set the user which the message should be sent to. |
||
| 271 | * |
||
| 272 | * @param string $user |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | 4 | public function user($user) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get the target that the message should be sent to: |
||
| 282 | * `#channel` or `@user` or null. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 5 | public function getTarget() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Set the target (user or channel) that the message should be sent to. |
||
| 299 | * |
||
| 300 | * @param string $target @user, #channel, channel, null |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | 3 | public function setTarget($target) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Remove the target, then this message will be sent to |
||
| 325 | * the webhook defined target. |
||
| 326 | * |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | 11 | public function removeTarget() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Set the target. |
||
| 338 | * |
||
| 339 | * @param string $target |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | 1 | public function target($target) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Set the target. |
||
| 349 | * |
||
| 350 | * @param string $target |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | 3 | public function to($target) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Get the attachments defaults. |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | 1 | public function getAttachmentDefaults() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Set the attachments defaults. |
||
| 370 | * |
||
| 371 | * @param array $defaults |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | 9 | public function setAttachmentDefaults($defaults) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get the attachments for the message. |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | 11 | public function getAttachments() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Set the attachments for the message. |
||
| 397 | * |
||
| 398 | * @param mixed $attachments |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | 6 | public function setAttachments($attachments) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Set the attachments for the message. |
||
| 416 | * |
||
| 417 | * @param mixed $attachments |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | 1 | public function attachments($attachments) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Add an attachment to the message. |
||
| 427 | * |
||
| 428 | * The parameter can be an payload array that contains all of attachment's fields. |
||
| 429 | * The parameters can also be attachment's fields that in order of |
||
| 430 | * text, title, images and color. Except the text, other parameters |
||
| 431 | * can be ignored. |
||
| 432 | * |
||
| 433 | * @param mixed $attachment |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | 10 | public function addAttachment($attachment) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get payload for an attachment. |
||
| 458 | * |
||
| 459 | * @param mixed $text |
||
| 460 | * @param mixed $title |
||
| 461 | * @param mixed $images |
||
| 462 | * @param mixed $color |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | 9 | protected function getAttachmentPayload($text = null, $title = null, $images = null, $color = null) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Get payload for images. |
||
| 490 | * |
||
| 491 | * @param mixed $value |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | 9 | protected function getImagesPayload($value) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Convert any type to string. |
||
| 513 | * |
||
| 514 | * @param mixed $value |
||
| 515 | * @param int $jsonOptions |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | 9 | protected function stringValue($value, $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Add an attachment to the message. |
||
| 537 | * It alias to `addAttachment`. |
||
| 538 | * |
||
| 539 | * @return $this |
||
| 540 | */ |
||
| 541 | 6 | public function add() |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Add an image attachment to the message. |
||
| 548 | * |
||
| 549 | * @param string|string[] $image |
||
| 550 | * @param string $desc |
||
| 551 | * @param string $title |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | 1 | public function addImage($image, $desc = null, $title = null) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Remove attachment[s] for the message. |
||
| 561 | * |
||
| 562 | * @return $this |
||
| 563 | */ |
||
| 564 | 7 | public function removeAttachments() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Remove attachment[s] for the message. |
||
| 583 | * It alias to `removeAttachments`. |
||
| 584 | * |
||
| 585 | * @return $this |
||
| 586 | */ |
||
| 587 | 1 | public function remove() |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Configure message defaults. |
||
| 594 | * |
||
| 595 | * @param array $defaults |
||
| 596 | * @param bool $force |
||
| 597 | * @return $this |
||
| 598 | */ |
||
| 599 | 7 | public function configureDefaults(array $defaults, $force = false) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Fill with message defaults. |
||
| 624 | * |
||
| 625 | * @param string $key |
||
| 626 | * @param mixed $value |
||
| 627 | * @return void |
||
| 628 | */ |
||
| 629 | 4 | protected function fillDefaults($key, $value) |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Convert a string to studly caps case. |
||
| 653 | * |
||
| 654 | * @param string $string |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | 4 | protected function studlyCase($string) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Conveniently set message content. |
||
| 664 | * |
||
| 665 | * The parameters may be: |
||
| 666 | * `($text, $markdown, $notification)` |
||
| 667 | * or `($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)`. |
||
| 668 | * |
||
| 669 | * @return $this |
||
| 670 | */ |
||
| 671 | 3 | public function content() |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Convert the message to an array. |
||
| 697 | * |
||
| 698 | * @return array |
||
| 699 | */ |
||
| 700 | 7 | public function toArray() |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Convert the message to JSON string. |
||
| 724 | * |
||
| 725 | * @param int $options |
||
| 726 | * @return string |
||
| 727 | */ |
||
| 728 | 1 | public function toJson($options = 0) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Serializes the object to a value that can be serialized natively by json_encode(). |
||
| 735 | * |
||
| 736 | * @return array |
||
| 737 | */ |
||
| 738 | 1 | public function jsonSerialize() |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Send the message. |
||
| 745 | * |
||
| 746 | * The parameters accepts the same format of `content` method. |
||
| 747 | * |
||
| 748 | * @return bool |
||
| 749 | */ |
||
| 750 | 2 | public function send() |
|
| 769 | |||
| 770 | /** |
||
| 771 | * Send the message to the given target. |
||
| 772 | * |
||
| 773 | * @param mixed $target |
||
| 774 | * @return bool |
||
| 775 | */ |
||
| 776 | 1 | public function sendTo($target) |
|
| 782 | |||
| 783 | /** |
||
| 784 | * Convert the message to its string representation. |
||
| 785 | * |
||
| 786 | * @return string |
||
| 787 | */ |
||
| 788 | 1 | public function __toString() |
|
| 792 | } |
||
| 793 |