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 | 18 | public function __construct(Client $client = null) |
|
71 | { |
||
72 | 18 | if ($this->client = $client) { |
|
73 | 4 | $this->configureDefaults($client->getMessageDefaults()); |
|
74 | 4 | } |
|
75 | 18 | } |
|
76 | |||
77 | /** |
||
78 | * Get the BearyChat client for sending message. |
||
79 | * |
||
80 | * @return \ElfSundae\BearyChat\Client |
||
81 | */ |
||
82 | 1 | public function getClient() |
|
83 | { |
||
84 | 1 | return $this->client; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get the text. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 6 | public function getText() |
|
96 | |||
97 | /** |
||
98 | * Set the text. |
||
99 | * |
||
100 | * @param string $text |
||
101 | * @return $this |
||
102 | */ |
||
103 | 5 | public function setText($text) |
|
109 | |||
110 | /** |
||
111 | * Set the text. |
||
112 | * |
||
113 | * @param string $text |
||
114 | * @return $this |
||
115 | */ |
||
116 | 3 | public function text($text) |
|
120 | |||
121 | /** |
||
122 | * Get the notification. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 6 | public function getNotification() |
|
130 | |||
131 | /** |
||
132 | * Set the notification. |
||
133 | * |
||
134 | * @param string $notification |
||
135 | * @return $this |
||
136 | */ |
||
137 | 3 | 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 | 6 | 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 | 7 | 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 | 4 | public function setChannel($channel) |
|
211 | |||
212 | /** |
||
213 | * Set the channel which the message should be sent to. |
||
214 | * |
||
215 | * @param string $channel |
||
216 | * @return $this |
||
217 | */ |
||
218 | 1 | public function channel($channel) |
|
222 | |||
223 | /** |
||
224 | * Get the user which the message should be sent to. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 7 | public function getUser() |
|
232 | |||
233 | /** |
||
234 | * Set the user which the message should be sent to. |
||
235 | * |
||
236 | * @param string $user |
||
237 | * @return $this |
||
238 | */ |
||
239 | 6 | public function setUser($user) |
|
245 | |||
246 | /** |
||
247 | * Set the user which the message should be sent to. |
||
248 | * |
||
249 | * @param string $user |
||
250 | * @return $this |
||
251 | */ |
||
252 | 1 | public function user($user) |
|
256 | |||
257 | /** |
||
258 | * Set the target (user or channel) that the message should be sent to. |
||
259 | * |
||
260 | * The target may be started with '@' for sending to user, and the channel's |
||
261 | * starter mark '#' is optional. |
||
262 | * |
||
263 | * It will remove all targets if the given target is null. |
||
264 | * |
||
265 | * @param string $target |
||
266 | * @return $this |
||
267 | */ |
||
268 | 3 | public function to($target) |
|
269 | { |
||
270 | 3 | $this->setChannel(null); |
|
271 | 3 | $this->setUser(null); |
|
272 | |||
273 | 3 | if (! empty($target)) { |
|
274 | 3 | $target = (string) $target; |
|
275 | |||
276 | 3 | $mark = mb_substr($target, 0, 1); |
|
277 | 3 | $to = mb_substr($target, 1); |
|
278 | |||
279 | 3 | if ($mark === '@' && ! empty($to)) { |
|
280 | 3 | $this->setUser($to); |
|
281 | 3 | } elseif ($mark === '#' && ! empty($to)) { |
|
282 | 1 | $this->setChannel($to); |
|
283 | 1 | } else { |
|
284 | 2 | $this->setChannel($target); |
|
285 | } |
||
286 | 3 | } |
|
287 | |||
288 | 3 | return $this; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Get the attachments for the message. |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 9 | public function getAttachments() |
|
300 | |||
301 | /** |
||
302 | * Set the attachments for the message. |
||
303 | * |
||
304 | * @param mixed $attachments |
||
305 | * @return $this |
||
306 | */ |
||
307 | 1 | public function setAttachments($attachments) |
|
308 | { |
||
309 | 1 | $this->removeAttachments(); |
|
310 | |||
311 | 1 | if (is_array($attachments)) { |
|
312 | 1 | foreach ($attachments as $attachment) { |
|
313 | 1 | $this->addAttachment($attachment); |
|
314 | 1 | } |
|
315 | 1 | } |
|
316 | |||
317 | 1 | return $this; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * Set the attachments for the message. |
||
322 | * |
||
323 | * @param mixed $attachments |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function attachments($attachments) |
||
330 | |||
331 | /** |
||
332 | * Add an attachment to the message. |
||
333 | * |
||
334 | * The parameter can be an payload array that contains all of attachment's fields. |
||
335 | * The parameters can also be attachment's fields that in order of |
||
336 | * text, title, images and color. Except the text, other parameters |
||
337 | * can be ignored. |
||
338 | * |
||
339 | * @param mixed $attachment |
||
340 | * @return $this |
||
341 | */ |
||
342 | 8 | public function addAttachment($attachment) |
|
343 | { |
||
344 | 8 | if (! is_array($attachment)) { |
|
345 | 7 | $attachment = $this->getAttachmentPayloadFromArguments(func_get_args()); |
|
346 | 7 | } |
|
347 | |||
348 | 8 | if (! empty($attachment)) { |
|
349 | 8 | $attachment += $this->attachmentDefaults; |
|
350 | |||
351 | 8 | $this->attachments[] = $attachment; |
|
352 | 8 | } |
|
353 | |||
354 | 8 | return $this; |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * Convert arguments list to attachment payload. |
||
359 | * |
||
360 | * @param array $arguments |
||
361 | * @return array |
||
362 | */ |
||
363 | 7 | protected function getAttachmentPayloadFromArguments($arguments) |
|
364 | { |
||
365 | 7 | $attachment = []; |
|
366 | |||
367 | 7 | foreach ($arguments as $index => $value) { |
|
368 | 7 | if (empty($value)) { |
|
369 | 2 | continue; |
|
370 | } |
||
371 | |||
372 | 7 | if ($index === 0) { |
|
373 | 7 | $attachment['text'] = $this->stringValue($value); |
|
374 | 7 | } elseif ($index === 1) { |
|
375 | 2 | $attachment['title'] = $this->stringValue($value); |
|
376 | 3 | } elseif ($index === 2) { |
|
377 | 3 | $images = []; |
|
378 | 3 | foreach ((array) $value as $img) { |
|
379 | 3 | if (is_array($img) && isset($img['url'])) { |
|
380 | $img = $img['url']; |
||
381 | } |
||
382 | 3 | if (is_string($img) && ! empty($img)) { |
|
383 | 3 | $images[] = ['url' => $img]; |
|
384 | 3 | } |
|
385 | 3 | } |
|
386 | 3 | if (! empty($images)) { |
|
387 | 3 | $attachment['images'] = $images; |
|
388 | 3 | } |
|
389 | 3 | } elseif ($index === 3) { |
|
390 | 2 | $attachment['color'] = (string) $value; |
|
391 | 2 | } |
|
392 | 7 | } |
|
393 | |||
394 | 7 | return $attachment; |
|
395 | } |
||
396 | |||
397 | /** |
||
398 | * Get the attachments' defaults. |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | 1 | public function getAttachmentDefaults() |
|
406 | |||
407 | /** |
||
408 | * Set the attachments' defaults. |
||
409 | * |
||
410 | * @param array $defaults |
||
411 | * @return $this |
||
412 | */ |
||
413 | 2 | public function setAttachmentDefaults(array $defaults) |
|
419 | |||
420 | /** |
||
421 | * Add an attachment to the message. |
||
422 | * It alias to `addAttachment`. |
||
423 | * |
||
424 | * @return $this |
||
425 | */ |
||
426 | 4 | public function add() |
|
430 | |||
431 | /** |
||
432 | * Add an image attachment to the message. |
||
433 | * |
||
434 | * @param string|string[] $image |
||
435 | * @param string $desc |
||
436 | * @return $this |
||
437 | */ |
||
438 | 1 | public function addImage($image, $desc = null) |
|
442 | |||
443 | /** |
||
444 | * Remove attachment[s] for the message. |
||
445 | * |
||
446 | * @return $this |
||
447 | */ |
||
448 | 2 | public function removeAttachments() |
|
449 | { |
||
450 | 2 | if (func_num_args() > 0) { |
|
451 | 1 | $indices = is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args(); |
|
452 | |||
453 | 1 | foreach ($indices as $index) { |
|
454 | 1 | unset($this->attachments[$index]); |
|
455 | 1 | } |
|
456 | |||
457 | 1 | $this->attachments = array_values($this->attachments); |
|
458 | 1 | } else { |
|
459 | 2 | $this->attachments = []; |
|
460 | } |
||
461 | |||
462 | 2 | return $this; |
|
463 | } |
||
464 | |||
465 | /** |
||
466 | * Remove attachment[s] for the message. |
||
467 | * It alias to `removeAttachments`. |
||
468 | * |
||
469 | * @return $this |
||
470 | */ |
||
471 | 1 | public function remove() |
|
475 | |||
476 | /** |
||
477 | * Configure message defaults. |
||
478 | * |
||
479 | * @param array $defaults |
||
480 | */ |
||
481 | 4 | protected function configureDefaults(array $defaults) |
|
482 | { |
||
483 | 4 | if (isset($defaults[MessageDefaults::CHANNEL])) { |
|
484 | $this->setChannel($defaults[MessageDefaults::CHANNEL]); |
||
485 | } |
||
486 | 4 | if (isset($defaults[MessageDefaults::USER])) { |
|
487 | 2 | $this->setUser($defaults[MessageDefaults::USER]); |
|
488 | 2 | } |
|
489 | 4 | if (isset($defaults[MessageDefaults::MARKDOWN])) { |
|
490 | $this->setMarkdown($defaults[MessageDefaults::MARKDOWN]); |
||
491 | } |
||
492 | 4 | if (isset($defaults[MessageDefaults::NOTIFICATION])) { |
|
493 | 2 | $this->setNotification($defaults[MessageDefaults::NOTIFICATION]); |
|
494 | 2 | } |
|
495 | 4 | if (isset($defaults[MessageDefaults::ATTACHMENT_COLOR])) { |
|
496 | 2 | $this->attachmentDefaults['color'] = $defaults[MessageDefaults::ATTACHMENT_COLOR]; |
|
497 | 2 | } |
|
498 | 4 | } |
|
499 | |||
500 | /** |
||
501 | * Convert any type to string. |
||
502 | * |
||
503 | * @param mixed $value |
||
504 | * @param int $jsonOptions |
||
505 | * @return string |
||
506 | */ |
||
507 | 7 | protected function stringValue($value, $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
508 | { |
||
509 | 7 | if (is_object($value)) { |
|
510 | if (method_exists($value, '__toString')) { |
||
511 | return (string) $value; |
||
512 | } |
||
513 | |||
514 | if (method_exists($value, 'toArray')) { |
||
515 | $value = $value->toArray(); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | 7 | return is_string($value) ? $value : json_encode($value, $jsonOptions); |
|
520 | } |
||
521 | |||
522 | /** |
||
523 | * Convert the message to an array. |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | 5 | public function toArray() |
|
528 | { |
||
529 | 5 | return array_filter( |
|
530 | [ |
||
531 | 5 | 'text' => $this->getText(), |
|
532 | 5 | 'notification' => $this->getNotification(), |
|
533 | 5 | 'markdown' => $this->getMarkdown(), |
|
534 | 5 | 'channel' => $this->getChannel(), |
|
535 | 5 | 'user' => $this->getUser(), |
|
536 | 5 | 'attachments' => $this->getAttachments(), |
|
537 | 5 | ], |
|
538 | 5 | function ($value, $key) { |
|
539 | return ! ( |
||
540 | 5 | is_null($value) || |
|
541 | 5 | ($key === 'markdown' && $value === true) || |
|
542 | 5 | (is_array($value) && empty($value)) |
|
543 | 5 | ); |
|
544 | 5 | }, |
|
545 | ARRAY_FILTER_USE_BOTH |
||
546 | 5 | ); |
|
547 | } |
||
548 | |||
549 | /** |
||
550 | * Convert the message to JSON string. |
||
551 | * |
||
552 | * @param int $options |
||
553 | * @return string |
||
554 | */ |
||
555 | public function toJson($options = 0) |
||
559 | |||
560 | /** |
||
561 | * Serializes the object to a value that can be serialized natively by json_encode(). |
||
562 | * |
||
563 | * @return array |
||
564 | */ |
||
565 | public function jsonSerialize() |
||
569 | |||
570 | /** |
||
571 | * Send the message. |
||
572 | * |
||
573 | * The parameters can be `($text, $markdown, $notification)`, and the $text and |
||
574 | * the $notification can be `null` that does not modify the exist field. |
||
575 | * The parameters can also be |
||
576 | * `($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)`. |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | 2 | public function send() |
|
581 | { |
||
582 | 2 | if (! $this->client) { |
|
583 | 1 | return false; |
|
584 | } |
||
585 | |||
586 | 2 | if ($count = func_num_args()) { |
|
587 | 2 | $firstArg = func_get_arg(0); |
|
588 | |||
589 | 2 | if (1 === $count && (is_array($firstArg) || is_object($firstArg))) { |
|
590 | 1 | return $this->client->sendMessage($firstArg); |
|
591 | } |
||
592 | |||
593 | 2 | if (! is_null($firstArg)) { |
|
594 | 2 | $this->setText($firstArg); |
|
595 | 2 | } |
|
596 | |||
597 | 2 | if ($count > 1 && is_bool(func_get_arg(1))) { |
|
598 | 2 | $this->setMarkdown(func_get_arg(1)); |
|
599 | |||
600 | 2 | if ($count > 2 && ! is_null(func_get_arg(2))) { |
|
601 | 1 | $this->setNotification(func_get_arg(2)); |
|
602 | 1 | } |
|
603 | 2 | } elseif ($count > 1) { |
|
604 | 1 | call_user_func_array( |
|
605 | 1 | [$this, 'addAttachment'], |
|
606 | 1 | array_slice(func_get_args(), 1) |
|
607 | 1 | ); |
|
608 | 1 | } |
|
609 | 2 | } |
|
610 | |||
611 | 2 | return $this->client->sendMessage($this); |
|
612 | } |
||
613 | |||
614 | /** |
||
615 | * Send the message to the given target. |
||
616 | * |
||
617 | * @param mixed $target |
||
618 | * @return bool |
||
619 | */ |
||
620 | 1 | public function sendTo($target) |
|
626 | |||
627 | /** |
||
628 | * Convert the message to its string representation. |
||
629 | * |
||
630 | * @return string |
||
631 | */ |
||
632 | public function __toString() |
||
636 | } |
||
637 |