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 | 22 | public function __construct(Client $client = null) |
|
| 71 | { |
||
| 72 | 22 | if ($this->client = $client) { |
|
| 73 | 5 | $this->configureDefaults($client->getMessageDefaults(), true); |
|
| 74 | 5 | } |
|
| 75 | 22 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get the BearyChat client for sending message. |
||
| 79 | * |
||
| 80 | * @return \ElfSundae\BearyChat\Client |
||
| 81 | */ |
||
| 82 | 2 | public function getClient() |
|
| 83 | { |
||
| 84 | 2 | return $this->client; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the text. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 7 | public function getText() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Set the text. |
||
| 99 | * |
||
| 100 | * @param string $text |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | 6 | public function setText($text) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Set the text. |
||
| 112 | * |
||
| 113 | * @param string $text |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | 4 | public function text($text) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get the notification. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 7 | public function getNotification() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Set the notification. |
||
| 133 | * |
||
| 134 | * @param string $notification |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | 4 | public function setNotification($notification) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Set the notification. |
||
| 146 | * |
||
| 147 | * @param string $notification |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | 1 | public function notification($notification) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get the markdown. |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | 7 | public function getMarkdown() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Set the markdown. |
||
| 167 | * |
||
| 168 | * @param bool $markdown |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | 3 | public function setMarkdown($markdown) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Set the markdown. |
||
| 180 | * |
||
| 181 | * @param bool $markdown |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | 1 | public function markdown($markdown = true) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get the channel which the message should be sent to. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | 10 | public function getChannel() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Set the channel which the message should be sent to. |
||
| 201 | * |
||
| 202 | * @param string $channel |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | 6 | public function setChannel($channel) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Set the channel which the message should be sent to. |
||
| 218 | * |
||
| 219 | * @param string $channel |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | 4 | public function channel($channel) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Get the user which the message should be sent to. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 10 | public function getUser() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Set the user which the message should be sent to. |
||
| 239 | * |
||
| 240 | * @param string $user |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | 10 | public function setUser($user) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Set the user which the message should be sent to. |
||
| 256 | * |
||
| 257 | * @param string $user |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | 4 | public function user($user) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get the target that the message should be sent to: |
||
| 267 | * `#channel` or `@user` or null. |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 4 | public function getTarget() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Set the target (user or channel) that the message should be sent to. |
||
| 284 | * |
||
| 285 | * @param string $target @user, #channel, channel, null |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | 3 | public function setTarget($target) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Remove the target, then this message will be sent to |
||
| 310 | * the webhook defined target. |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | 10 | public function removeTarget() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Set the target. |
||
| 323 | * |
||
| 324 | * @param string $target |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | 1 | public function target($target) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set the target. |
||
| 334 | * |
||
| 335 | * @param string $target |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | 3 | public function to($target) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Get the attachments defaults. |
||
| 345 | * |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | 1 | public function getAttachmentDefaults() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Set the attachments defaults. |
||
| 355 | * |
||
| 356 | * @param array $defaults |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | 2 | public function setAttachmentDefaults($defaults) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get the attachments for the message. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | 10 | public function getAttachments() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Set the attachments for the message. |
||
| 378 | * |
||
| 379 | * @param mixed $attachments |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | 4 | public function setAttachments($attachments) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Set the attachments for the message. |
||
| 397 | * |
||
| 398 | * @param mixed $attachments |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | 1 | public function attachments($attachments) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Add an attachment to the message. |
||
| 408 | * |
||
| 409 | * The parameter can be an payload array that contains all of attachment's fields. |
||
| 410 | * The parameters can also be attachment's fields that in order of |
||
| 411 | * text, title, images and color. Except the text, other parameters |
||
| 412 | * can be ignored. |
||
| 413 | * |
||
| 414 | * @param mixed $attachment |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | 9 | public function addAttachment($attachment) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Get payload for an attachment. |
||
| 439 | * |
||
| 440 | * @param mixed $text |
||
| 441 | * @param mixed $title |
||
| 442 | * @param mixed $images |
||
| 443 | * @param mixed $color |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | 8 | protected function getAttachmentPayload($text = null, $title = null, $images = null, $color = null) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Get payload for images. |
||
| 471 | * |
||
| 472 | * @param mixed $value |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | 8 | protected function getImagesPayload($value) |
|
| 476 | { |
||
| 477 | 8 | $images = []; |
|
| 478 | |||
| 479 | 8 | foreach ((array) $value as $img) { |
|
| 480 | 3 | if (! empty($img['url'])) { |
|
| 481 | $img = $img['url']; |
||
| 482 | } |
||
| 483 | |||
| 484 | 3 | if (is_string($img) && ! empty($img)) { |
|
| 485 | 3 | $images[] = ['url' => $img]; |
|
| 486 | 3 | } |
|
| 487 | 8 | } |
|
| 488 | |||
| 489 | 8 | return $images; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Convert any type to string. |
||
| 494 | * |
||
| 495 | * @param mixed $value |
||
| 496 | * @param int $jsonOptions |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | 8 | protected function stringValue($value, $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
| 500 | { |
||
| 501 | 8 | if (is_string($value)) { |
|
| 502 | 8 | return $value; |
|
| 503 | } |
||
| 504 | |||
| 505 | 1 | if (method_exists($value, '__toString')) { |
|
| 506 | return (string) $value; |
||
| 507 | } |
||
| 508 | |||
| 509 | 1 | if (method_exists($value, 'toArray')) { |
|
| 510 | $value = $value->toArray(); |
||
| 511 | } |
||
| 512 | |||
| 513 | 1 | return json_encode($value, $jsonOptions); |
|
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Add an attachment to the message. |
||
| 518 | * It alias to `addAttachment`. |
||
| 519 | * |
||
| 520 | * @return $this |
||
| 521 | */ |
||
| 522 | 5 | public function add() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Add an image attachment to the message. |
||
| 529 | * |
||
| 530 | * @param string|string[] $image |
||
| 531 | * @param string $desc |
||
| 532 | * @param string $title |
||
| 533 | * @return $this |
||
| 534 | */ |
||
| 535 | 1 | public function addImage($image, $desc = null, $title = null) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Remove attachment[s] for the message. |
||
| 542 | * |
||
| 543 | * @return $this |
||
| 544 | */ |
||
| 545 | 5 | public function removeAttachments() |
|
| 546 | { |
||
| 547 | 5 | if (func_num_args() > 0) { |
|
| 548 | 1 | $indices = is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args(); |
|
| 549 | |||
| 550 | 1 | foreach ($indices as $index) { |
|
| 551 | 1 | unset($this->attachments[$index]); |
|
| 552 | 1 | } |
|
| 553 | |||
| 554 | 1 | $this->attachments = array_values($this->attachments); |
|
| 555 | 1 | } else { |
|
| 556 | 5 | $this->attachments = []; |
|
| 557 | } |
||
| 558 | |||
| 559 | 5 | return $this; |
|
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Remove attachment[s] for the message. |
||
| 564 | * It alias to `removeAttachments`. |
||
| 565 | * |
||
| 566 | * @return $this |
||
| 567 | */ |
||
| 568 | 1 | public function remove() |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Configure message defaults. |
||
| 575 | * |
||
| 576 | * @param array $defaults |
||
| 577 | * @param bool $force |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | 6 | public function configureDefaults(array $defaults, $force = false) |
|
| 581 | { |
||
| 582 | 6 | if (! $force && ! empty($this->toArray())) { |
|
| 583 | 1 | return $this; |
|
| 584 | } |
||
| 585 | |||
| 586 | 6 | $attachmentDefaults = $this->attachmentDefaults; |
|
| 587 | |||
| 588 | 6 | foreach (MessageDefaults::allKeys() as $key) { |
|
| 589 | 6 | if (! isset($defaults[$key]) || is_null($value = $defaults[$key])) { |
|
| 590 | 6 | continue; |
|
| 591 | } |
||
| 592 | |||
| 593 | 3 | if (strpos($key, 'attachment_') !== false) { |
|
| 594 | 3 | if ($key = substr($key, strlen('attachment_'))) { |
|
| 595 | 3 | $attachmentDefaults[$key] = $value; |
|
| 596 | 3 | } |
|
| 597 | 3 | } else { |
|
| 598 | 3 | if (! is_null($this->getTarget()) && |
|
| 599 | 3 | ($key == MessageDefaults::USER || $key == MessageDefaults::CHANNEL) |
|
| 600 | 3 | ) { |
|
| 601 | 1 | continue; |
|
| 602 | } |
||
| 603 | |||
| 604 | 3 | if ($suffix = $this->studlyCase($key)) { |
|
| 605 | 3 | $getMethod = 'get'.$suffix; |
|
| 606 | 3 | $setMethod = 'set'.$suffix; |
|
| 607 | if ( |
||
| 608 | 3 | method_exists($this, $getMethod) && |
|
| 609 | 3 | is_null($this->{$getMethod}()) && |
|
| 610 | 3 | method_exists($this, $setMethod) |
|
| 611 | 3 | ) { |
|
| 612 | 3 | $this->{$setMethod}($value); |
|
| 613 | 3 | } |
|
| 614 | 3 | } |
|
| 615 | } |
||
| 616 | 6 | } |
|
| 617 | |||
| 618 | 6 | if ($attachmentDefaults != $this->attachmentDefaults) { |
|
| 619 | 3 | $this->attachmentDefaults = $attachmentDefaults; |
|
| 620 | 3 | $this->setAttachments($this->attachments); |
|
| 621 | 3 | } |
|
| 622 | |||
| 623 | 6 | return $this; |
|
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Convert a string to studly caps case. |
||
| 628 | * |
||
| 629 | * @param string $string |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | 3 | protected function studlyCase($string) |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Conveniently set message content. |
||
| 639 | * |
||
| 640 | * The parameters may be: |
||
| 641 | * `($text, $markdown, $notification)` |
||
| 642 | * or `($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)`. |
||
| 643 | * |
||
| 644 | * @return $this |
||
| 645 | */ |
||
| 646 | 2 | public function content() |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Convert the message to an array. |
||
| 672 | * |
||
| 673 | * @return array |
||
| 674 | */ |
||
| 675 | 6 | public function toArray() |
|
| 696 | |||
| 697 | /** |
||
| 698 | * Convert the message to JSON string. |
||
| 699 | * |
||
| 700 | * @param int $options |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | 1 | public function toJson($options = 0) |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Serializes the object to a value that can be serialized natively by json_encode(). |
||
| 710 | * |
||
| 711 | * @return array |
||
| 712 | */ |
||
| 713 | 1 | public function jsonSerialize() |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Send the message. |
||
| 720 | * |
||
| 721 | * The parameters accepts the same format of `content` method. |
||
| 722 | * |
||
| 723 | * @return bool |
||
| 724 | */ |
||
| 725 | 2 | public function send() |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Send the message to the given target. |
||
| 747 | * |
||
| 748 | * @param mixed $target |
||
| 749 | * @return bool |
||
| 750 | */ |
||
| 751 | 1 | public function sendTo($target) |
|
| 757 | |||
| 758 | /** |
||
| 759 | * Convert the message to its string representation. |
||
| 760 | * |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | 1 | public function __toString() |
|
| 767 | } |
||
| 768 |