Total Complexity | 143 |
Total Lines | 697 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Context 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.
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 Context, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Context extends \CharlotteDunois\Yasmin\Models\ClientBase { |
||
23 | /** |
||
24 | * The client which initiated the instance. |
||
25 | * @var \CharlotteDunois\Livia\Client |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | /** |
||
30 | * The message that triggers the command. |
||
31 | * @var \CharlotteDunois\Yasmin\Models\Message |
||
32 | */ |
||
33 | protected $message; |
||
34 | |||
35 | /** |
||
36 | * The command that got triggered, if any. |
||
37 | * @var \CharlotteDunois\Livia\Commands\Command|null |
||
38 | */ |
||
39 | protected $command; |
||
40 | |||
41 | /** |
||
42 | * Argument string for the command. |
||
43 | * @var string|null |
||
44 | */ |
||
45 | protected $argString; |
||
46 | |||
47 | /** |
||
48 | * Pattern matches (if from a pattern trigger). |
||
49 | * @var string[]|null |
||
50 | */ |
||
51 | protected $patternMatches; |
||
52 | |||
53 | /** |
||
54 | * Command responses, as multidimensional array (channelID|dm => Message[]). |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $responses = array(); |
||
58 | |||
59 | /** |
||
60 | * Internal command for serialization. |
||
61 | * @var string|null |
||
62 | */ |
||
63 | protected $internalCommand; |
||
64 | |||
65 | /** |
||
66 | * @internal |
||
67 | */ |
||
68 | function __construct(\CharlotteDunois\Livia\Client $client, \CharlotteDunois\Yasmin\Models\Message $message, \CharlotteDunois\Livia\Commands\Command $command = null, string $argString = null, array $patternMatches = null) { |
||
69 | $this->client = $client; |
||
70 | $this->message = $message; |
||
71 | $this->command = $command; |
||
72 | |||
73 | $this->argString = ($argString !== null ? \trim($argString) : null); |
||
74 | $this->patternMatches = $patternMatches; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $name |
||
79 | * @return bool |
||
80 | * @throws \Exception |
||
81 | * @internal |
||
82 | */ |
||
83 | function __isset($name) { |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $name |
||
97 | * @return mixed |
||
98 | * @throws \RuntimeException |
||
99 | * @internal |
||
100 | */ |
||
101 | function __get($name) { |
||
102 | if(\property_exists($this, $name)) { |
||
103 | return $this->$name; |
||
104 | } |
||
105 | |||
106 | return $this->message->$name; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return mixed |
||
111 | * @throws \RuntimeException |
||
112 | * @internal |
||
113 | */ |
||
114 | function __call($name, $args) { |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | * @internal |
||
125 | */ |
||
126 | function serialize() { |
||
127 | $cmd = $this->command; |
||
128 | $this->command = null; |
||
129 | |||
130 | if($cmd !== null) { |
||
131 | $this->internalCommand = $cmd->groupID.':'.$cmd->name; |
||
132 | } |
||
133 | |||
134 | $str = parent::serialize(); |
||
135 | $this->command = $cmd; |
||
136 | |||
137 | return $str; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return void |
||
142 | * @internal |
||
143 | */ |
||
144 | function unserialize($data) { |
||
145 | if(self::$serializeClient === null) { |
||
146 | throw new \Exception('Unable to unserialize a class without ClientBase::$serializeClient being set'); |
||
147 | } |
||
148 | |||
149 | parent::unserialize($data); |
||
150 | |||
151 | /** @var \CharlotteDunois\Livia\Client $this->client */ |
||
152 | $this->client = self::$serializeClient; |
||
153 | |||
154 | if($this->internalCommand !== null) { |
||
155 | $this->command = $this->client->registry->resolveCommand($this->internalCommand); |
||
156 | $this->internalCommand = null; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Parses the argString into usable arguments, based on the argsType and argsCount of the command. |
||
162 | * @return string|string[] |
||
163 | * @throws \LogicException |
||
164 | * @throws \RangeException |
||
165 | */ |
||
166 | function parseCommandArgs() { |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Runs the command. Resolves with an instance of Message or an array of Message instances. |
||
184 | * @return \React\Promise\ExtendedPromiseInterface |
||
185 | * @throws \LogicException |
||
186 | */ |
||
187 | function run() { |
||
400 | })); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Responds to the command message |
||
405 | * @param string $type One of plain, reply or direct. |
||
406 | * @param string $content |
||
407 | * @param array $options |
||
408 | * @param bool $fromEdit |
||
409 | * @return \React\Promise\ExtendedPromiseInterface |
||
410 | * @throws \RangeException |
||
411 | * @throws \InvalidArgumentException |
||
412 | */ |
||
413 | protected function respond(string $type, string $content, array $options = array(), bool $fromEdit = false) { |
||
414 | if($type === 'reply' && $this->message->channel instanceof \CharlotteDunois\Yasmin\Interfaces\DMChannelInterface) { |
||
415 | $type = 'plain'; |
||
416 | } elseif($type !== 'direct' && $this->message->guild !== null && !$this->message->channel->permissionsFor($this->client->user)->has('SEND_MESSAGES')) { |
||
417 | $type = 'direct'; |
||
418 | } |
||
419 | |||
420 | if(!empty($options['split']) && !\is_array($options['split'])) { |
||
421 | $options['split'] = array(); |
||
422 | } |
||
423 | |||
424 | $channelID = $this->getChannelIDOrDM($this->message->channel); |
||
425 | $shouldEdit = ( |
||
426 | !empty($this->responses) && |
||
427 | ( |
||
428 | ($type === 'direct' && !empty($this->responses['dm'])) || |
||
429 | ($type !== 'direct' && !empty($this->responses[$channelID])) |
||
430 | ) && |
||
431 | !$fromEdit && |
||
432 | empty($options['files']) |
||
433 | ); |
||
434 | |||
435 | switch($type) { |
||
436 | case 'plain': |
||
437 | if($shouldEdit) { |
||
438 | return $this->editCurrentResponse($channelID, $type, $content, $options); |
||
439 | } else { |
||
440 | return $this->message->channel->send($content, $options); |
||
441 | } |
||
442 | break; |
||
443 | case 'reply': |
||
444 | if($shouldEdit) { |
||
445 | return $this->editCurrentResponse($channelID, $type, $content, $options); |
||
446 | } else { |
||
447 | if(!empty($options['split']) && empty($options['split']['prepend'])) { |
||
448 | $options['split']['prepend'] = $this->message->author->__toString().\CharlotteDunois\Yasmin\Models\Message::$replySeparator; |
||
449 | } |
||
450 | |||
451 | return $this->message->reply($content, $options); |
||
452 | } |
||
453 | break; |
||
454 | case 'direct': |
||
455 | if($shouldEdit) { |
||
456 | return $this->editCurrentResponse($channelID, $type, $content, $options); |
||
457 | } else { |
||
458 | return $this->message->author->createDM()->then(function ($channel) use ($content, $options) { |
||
459 | return $channel->send($content, $options); |
||
460 | }); |
||
461 | } |
||
462 | break; |
||
463 | default: |
||
464 | throw new \RangeException('Unknown response type "'.$type.'"'); |
||
465 | break; |
||
466 | } |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Edits a response to the command message. Resolves with an instance of Message or an array of Message instances. |
||
471 | * @param \CharlotteDunois\Yasmin\Models\Message|\CharlotteDunois\Yasmin\Models\Message[]|null $response |
||
472 | * @param string $type |
||
473 | * @param string $content |
||
474 | * @param array $options |
||
475 | * @return \React\Promise\ExtendedPromiseInterface |
||
476 | */ |
||
477 | protected function editResponse($response, string $type, string $content, array $options = array()) { |
||
478 | if(!$response) { |
||
479 | return $this->respond($type, $content, $options); |
||
480 | } |
||
481 | |||
482 | if(!empty($options['split'])) { |
||
483 | $content = \CharlotteDunois\Yasmin\Utils\MessageHelpers::splitMessage($content, (\is_array($options['split']) ? $options['split'] : array())); |
||
484 | if(\count($content) === 1) { |
||
485 | $content = $content[0]; |
||
486 | } |
||
487 | } |
||
488 | |||
489 | $prepend = ''; |
||
490 | if($type === 'reply') { |
||
491 | $prepend = $this->message->author->__toString().\CharlotteDunois\Yasmin\Models\Message::$replySeparator; |
||
492 | } |
||
493 | |||
494 | if(\is_array($content)) { |
||
495 | $promises = array(); |
||
496 | $clength = \count($content); |
||
497 | |||
498 | if(\is_array($response)) { |
||
499 | for($i = 0; $i < $clength; $i++) { |
||
500 | if(!empty($response[$i])) { |
||
501 | $promises[] = $response[$i]->edit($prepend.$content[$i], $options); |
||
502 | } else { |
||
503 | $promises[] = $this->message->channel->send($prepend.$content[$i], $options); |
||
504 | } |
||
505 | } |
||
506 | } else { |
||
507 | $promises[] = $response->edit($prepend.$content[0], $options); |
||
508 | for($i = 1; $i < $clength; $i++) { |
||
509 | $promises[] = $this->message->channel->send($prepend.$content[$i], $options); |
||
510 | } |
||
511 | } |
||
512 | |||
513 | return \React\Promise\all($promises); |
||
514 | } else { |
||
515 | if(\is_array($response)) { |
||
516 | for($i = \count($response) - 1; $i > 0; $i--) { |
||
517 | $response[$i]->delete()->done(); |
||
518 | } |
||
519 | |||
520 | return $response[0]->edit($prepend.$content, $options); |
||
521 | } else { |
||
522 | return $response->edit($prepend.$content, $options); |
||
523 | } |
||
524 | } |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Edits the current response. |
||
529 | * @param string $id The ID of the channel the response is in ("DM" for direct messages). |
||
530 | * @param string $type |
||
531 | * @param string $content |
||
532 | * @param array $options |
||
533 | * @return \React\Promise\ExtendedPromiseInterface |
||
534 | */ |
||
535 | protected function editCurrentResponse(string $id, string $type, string $content, array $options = array()) { |
||
536 | if(empty($this->responses[$id])) { |
||
537 | $this->responses[$id] = array(); |
||
538 | } |
||
539 | |||
540 | if(!empty($this->responses[$id])) { |
||
541 | $msg = \array_shift($this->responses[$id]); |
||
542 | } else { |
||
543 | $msg = null; |
||
544 | } |
||
545 | |||
546 | return $this->editResponse($msg, $type, $content, $options); |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Responds with a plain message. Resolves with an instance of Message or an array of Message instances. |
||
551 | * @param string $content |
||
552 | * @param array $options Message Options. |
||
553 | * @return \React\Promise\ExtendedPromiseInterface |
||
554 | */ |
||
555 | function say(string $content, array $options = array()) { |
||
556 | return $this->respond('plain', $content, $options); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Responds with a reply message. Resolves with an instance of Message or an array of Message instances. |
||
561 | * @param string $content |
||
562 | * @param array $options Message Options. |
||
563 | * @return \React\Promise\ExtendedPromiseInterface |
||
564 | */ |
||
565 | function reply(string $content, array $options = array()) { |
||
566 | return $this->respond('reply', $content, $options); |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Responds with a direct message. Resolves with an instance of Message or an array of Message instances. |
||
571 | * @param string $content |
||
572 | * @param array $options Message Options. |
||
573 | * @return \React\Promise\ExtendedPromiseInterface |
||
574 | */ |
||
575 | function direct(string $content, array $options = array()) { |
||
576 | return $this->respond('direct', $content, $options); |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * Shortcut to $this->message->edit. |
||
581 | * @param string $content |
||
582 | * @param array $options Message Options. |
||
583 | * @return \React\Promise\ExtendedPromiseInterface |
||
584 | */ |
||
585 | function edit(string $content, array $options = array()) { |
||
586 | return $this->message->edit($content, $options); |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * Finalizes the command message by setting the responses and deleting any remaining prior ones. |
||
591 | * @param \CharlotteDunois\Yasmin\Models\Message|\CharlotteDunois\Yasmin\Models\Message[]|null $responses |
||
592 | * @return void |
||
593 | * @internal |
||
594 | */ |
||
595 | function finalize($responses) { |
||
624 | } |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Deletes any prior responses that haven't been updated. |
||
629 | * @return void |
||
630 | * @internal |
||
631 | */ |
||
632 | function deleteRemainingResponses() { |
||
633 | foreach($this->responses as $id => $msgs) { |
||
634 | foreach($msgs as $response) { |
||
635 | if(\is_array($response)) { |
||
636 | foreach($response as $resp) { |
||
637 | $resp->delete()->done(); |
||
638 | } |
||
639 | } else { |
||
640 | $response->delete()->done(); |
||
641 | } |
||
642 | } |
||
643 | |||
644 | $this->responses[$id] = array(); |
||
645 | } |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * @return string|int |
||
650 | */ |
||
651 | protected function getChannelIDOrDM(\CharlotteDunois\Yasmin\Interfaces\TextChannelInterface $channel) { |
||
652 | if(!($channel instanceof \CharlotteDunois\Yasmin\Interfaces\DMChannelInterface)) { |
||
653 | return $channel->id; |
||
654 | } |
||
655 | |||
656 | return 'dm'; |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * Parses an argument string into an array of arguments. |
||
661 | * @param string $argString |
||
662 | * @param int|float|null $argCount float = \INF |
||
663 | * @param bool $allowSingleQuotes |
||
664 | * @return string[] |
||
665 | */ |
||
666 | static function parseArgs(string $argString, $argCount = null, bool $allowSingleQuotes = true) { |
||
711 | } |
||
712 | |||
713 | /** |
||
714 | * @return void |
||
715 | * @internal |
||
716 | */ |
||
717 | function setResponses($responses) { |
||
719 | } |
||
720 | } |
||
721 |