Complex classes like Sms 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 Sms, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Sms |
||
14 | { |
||
15 | const TYPE_SMS = 1; |
||
16 | const TYPE_VOICE = 2; |
||
17 | |||
18 | /** |
||
19 | * Task instance. |
||
20 | * |
||
21 | * @var Task |
||
22 | */ |
||
23 | protected static $task = null; |
||
24 | |||
25 | /** |
||
26 | * Agent instances. |
||
27 | * |
||
28 | * @var Agent[] |
||
29 | */ |
||
30 | protected static $agents = []; |
||
31 | |||
32 | /** |
||
33 | * Dispatch scheme of agents. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $scheme = []; |
||
38 | |||
39 | /** |
||
40 | * Configuration information of agents. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected static $agentsConfig = []; |
||
45 | |||
46 | /** |
||
47 | * Whether to use the queue system. |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected static $enableQueue = false; |
||
52 | |||
53 | /** |
||
54 | * How to use the queue system. |
||
55 | * |
||
56 | * @var \Closure |
||
57 | */ |
||
58 | protected static $howToUseQueue = null; |
||
59 | |||
60 | /** |
||
61 | * Available hooks. |
||
62 | * |
||
63 | * @var string[] |
||
64 | */ |
||
65 | protected static $availableHooks = [ |
||
66 | 'beforeRun', |
||
67 | 'beforeDriverRun', |
||
68 | 'afterDriverRun', |
||
69 | 'afterRun', |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * Data container. |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $smsData = [ |
||
78 | 'type' => self::TYPE_SMS, |
||
79 | 'to' => null, |
||
80 | 'templates' => [], |
||
81 | 'data' => [], |
||
82 | 'content' => null, |
||
83 | 'code' => null, |
||
84 | 'files' => [], |
||
85 | 'params' => [], |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * The name of first agent. |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $firstAgent = null; |
||
94 | |||
95 | /** |
||
96 | * Whether pushed to the queue system. |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | protected $pushedToQueue = false; |
||
101 | |||
102 | /** |
||
103 | * State container. |
||
104 | * |
||
105 | * @var array |
||
106 | */ |
||
107 | protected $state = []; |
||
108 | |||
109 | /** |
||
110 | * Constructor |
||
111 | * |
||
112 | * @param bool $autoBoot |
||
113 | */ |
||
114 | 6 | public function __construct($autoBoot = true) |
|
120 | |||
121 | /** |
||
122 | * Bootstrap the task. |
||
123 | */ |
||
124 | 3 | public static function bootTask() |
|
133 | |||
134 | /** |
||
135 | * Is task has been booted. |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | 15 | protected static function isTaskBooted() |
|
143 | |||
144 | /** |
||
145 | * Get the task instance. |
||
146 | * |
||
147 | * @return Task |
||
148 | */ |
||
149 | 24 | public static function getTask() |
|
157 | |||
158 | /** |
||
159 | * Configure. |
||
160 | * |
||
161 | * @throws PhpSmsException |
||
162 | */ |
||
163 | 3 | protected static function configure() |
|
175 | |||
176 | /** |
||
177 | * Initialize the dispatch scheme. |
||
178 | * |
||
179 | * @param array $config |
||
180 | */ |
||
181 | 3 | protected static function initScheme(array &$config) |
|
187 | |||
188 | /** |
||
189 | * Initialize the configuration information. |
||
190 | * |
||
191 | * @param array $agents |
||
192 | * @param array $config |
||
193 | */ |
||
194 | 3 | protected static function initAgentsConfig(array $agents, array &$config) |
|
206 | |||
207 | /** |
||
208 | * register driver. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * @param string|array $scheme |
||
212 | */ |
||
213 | 15 | protected static function registerDriver($name, $scheme) |
|
242 | |||
243 | /** |
||
244 | * Parse the higher-order dispatch scheme. |
||
245 | * |
||
246 | * @param array $options |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | protected static function parseScheme(array $options) |
||
262 | |||
263 | /** |
||
264 | * Get the agent instance by name. |
||
265 | * |
||
266 | * @param string $name |
||
267 | * @param array $options |
||
268 | * |
||
269 | * @throws PhpSmsException |
||
270 | * |
||
271 | * @return Agent |
||
272 | */ |
||
273 | 24 | public static function getAgent($name, array $options = []) |
|
300 | |||
301 | /** |
||
302 | * Whether has the specified agent. |
||
303 | * |
||
304 | * @param string $name |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | 33 | public static function hasAgent($name) |
|
312 | |||
313 | /** |
||
314 | * Set or get the dispatch scheme. |
||
315 | * |
||
316 | * @param string|array|null $name |
||
317 | * @param string|array|bool|null $scheme |
||
318 | * @param bool $override |
||
319 | * |
||
320 | * @return mixed |
||
321 | */ |
||
322 | 18 | public static function scheme($name = null, $scheme = null, $override = false) |
|
342 | |||
343 | /** |
||
344 | * Modify the dispatch scheme of agent. |
||
345 | * |
||
346 | * @param string $name |
||
347 | * @param string|array $scheme |
||
348 | * |
||
349 | * @throws PhpSmsException |
||
350 | */ |
||
351 | 6 | protected static function modifyScheme($name, $scheme) |
|
368 | |||
369 | /** |
||
370 | * Set or get the configuration information. |
||
371 | * |
||
372 | * @param string|array|null $name |
||
373 | * @param array|bool|null $config |
||
374 | * @param bool $override |
||
375 | * |
||
376 | * @throws PhpSmsException |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | 18 | public static function config($name = null, $config = null, $override = false) |
|
394 | |||
395 | /** |
||
396 | * Modify the configuration information. |
||
397 | * |
||
398 | * @param string $name |
||
399 | * @param array $config |
||
400 | * @param bool $override |
||
401 | * |
||
402 | * @throws PhpSmsException |
||
403 | */ |
||
404 | 9 | protected static function modifyConfig($name, array $config, $override = false) |
|
420 | |||
421 | /** |
||
422 | * Validate the name of agent. |
||
423 | * |
||
424 | * @param string $name |
||
425 | * |
||
426 | * @throws PhpSmsException |
||
427 | */ |
||
428 | 12 | protected static function validateAgentName($name) |
|
434 | |||
435 | /** |
||
436 | * Tear down scheme. |
||
437 | */ |
||
438 | 6 | public static function cleanScheme() |
|
442 | |||
443 | /** |
||
444 | * Tear down config information. |
||
445 | */ |
||
446 | 6 | public static function cleanConfig() |
|
450 | |||
451 | /** |
||
452 | * Create a instance for send sms. |
||
453 | * |
||
454 | * @param mixed $agentName |
||
455 | * @param mixed $tempId |
||
456 | * |
||
457 | * @return Sms |
||
458 | */ |
||
459 | public static function make($agentName = null, $tempId = null) |
||
475 | |||
476 | /** |
||
477 | * Create a instance for send voice. |
||
478 | * |
||
479 | * @param int|string|null $code |
||
480 | * |
||
481 | * @return Sms |
||
482 | */ |
||
483 | 3 | public static function voice($code = null) |
|
491 | |||
492 | /** |
||
493 | * Set whether to use the queue system, |
||
494 | * and define how to use it. |
||
495 | * |
||
496 | * @param bool|\Closure|null $enable |
||
497 | * @param \Closure|null $handler |
||
498 | * |
||
499 | * @return bool |
||
500 | */ |
||
501 | 3 | public static function queue($enable = null, $handler = null) |
|
517 | |||
518 | /** |
||
519 | * Set the type of Sms instance. |
||
520 | * |
||
521 | * @param $type |
||
522 | * |
||
523 | * @throws PhpSmsException |
||
524 | * |
||
525 | * @return $this |
||
526 | */ |
||
527 | 3 | public function type($type) |
|
536 | |||
537 | /** |
||
538 | * Set the recipient`s mobile number. |
||
539 | * |
||
540 | * @param string|array $mobile |
||
541 | * |
||
542 | * @return $this |
||
543 | */ |
||
544 | 6 | public function to($mobile) |
|
553 | |||
554 | /** |
||
555 | * Set the sms content. |
||
556 | * |
||
557 | * @param string $content |
||
558 | * |
||
559 | * @return $this |
||
560 | */ |
||
561 | 3 | public function content($content) |
|
567 | |||
568 | /** |
||
569 | * Set the template ids. |
||
570 | * |
||
571 | * @param mixed $name |
||
572 | * @param mixed $tempId |
||
573 | * |
||
574 | * @return $this |
||
575 | */ |
||
576 | 3 | public function template($name, $tempId = null) |
|
582 | |||
583 | /** |
||
584 | * Set the template data. |
||
585 | * |
||
586 | * @param mixed $key |
||
587 | * @param mixed $value |
||
588 | * |
||
589 | * @return $this |
||
590 | */ |
||
591 | 3 | public function data($key, $value = null) |
|
597 | |||
598 | /** |
||
599 | * Set the voice code. |
||
600 | * |
||
601 | * @param string|int $code |
||
602 | * |
||
603 | * @return $this |
||
604 | */ |
||
605 | 3 | public function code($code) |
|
611 | |||
612 | /** |
||
613 | * Set voice file. |
||
614 | * |
||
615 | * @param string|array $name |
||
616 | * @param string|int $id |
||
617 | * |
||
618 | * @return $this |
||
619 | */ |
||
620 | public function file($name, $id = null) |
||
621 | { |
||
622 | Util::operateArray($this->smsData['files'], $name, $id); |
||
623 | |||
624 | return $this; |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Set params of agent. |
||
629 | * |
||
630 | * @param string|array $name |
||
631 | * @param array|bool|null $params |
||
632 | * @param bool $override |
||
633 | * |
||
634 | * @return $this |
||
635 | */ |
||
636 | public function params($name, $params = null, $override = false) |
||
653 | |||
654 | /** |
||
655 | * Set the first agent. |
||
656 | * |
||
657 | * @param string $name |
||
658 | * |
||
659 | * @throws PhpSmsException |
||
660 | * |
||
661 | * @return $this |
||
662 | */ |
||
663 | 3 | public function agent($name) |
|
672 | |||
673 | /** |
||
674 | * Start send. |
||
675 | * |
||
676 | * If call with a `true` parameter, this system will immediately start request to send sms whatever whether to use the queue. |
||
677 | * if the current instance has pushed to the queue, you can recall this method in queue system without any parameter, |
||
678 | * so this mechanism in order to make you convenient to use this method in queue system. |
||
679 | * |
||
680 | * @param bool $immediately |
||
681 | * |
||
682 | * @return mixed |
||
683 | */ |
||
684 | 18 | public function send($immediately = false) |
|
695 | |||
696 | /** |
||
697 | * Push to the queue system. |
||
698 | * |
||
699 | * @throws \Exception | PhpSmsException |
||
700 | * |
||
701 | * @return mixed |
||
702 | */ |
||
703 | 3 | public function push() |
|
717 | |||
718 | /** |
||
719 | * Get all of the data. |
||
720 | * |
||
721 | * @param null|string $key |
||
722 | * |
||
723 | * @return mixed |
||
724 | */ |
||
725 | 36 | public function all($key = null) |
|
733 | |||
734 | /** |
||
735 | * Define the static hook methods by overload static method. |
||
736 | * |
||
737 | * @param string $name |
||
738 | * @param array $args |
||
739 | * |
||
740 | * @throws PhpSmsException |
||
741 | */ |
||
742 | 9 | public static function __callStatic($name, $args) |
|
755 | |||
756 | /** |
||
757 | * Define the hook methods by overload method. |
||
758 | * |
||
759 | * @param string $name |
||
760 | * @param array $args |
||
761 | * |
||
762 | * @throws PhpSmsException |
||
763 | * @throws \Exception |
||
764 | */ |
||
765 | 3 | public function __call($name, $args) |
|
773 | |||
774 | /** |
||
775 | * Serialize magic method. |
||
776 | * |
||
777 | * @return array |
||
778 | */ |
||
779 | 3 | public function __sleep() |
|
791 | |||
792 | /** |
||
793 | * Deserialize magic method. |
||
794 | */ |
||
795 | 3 | public function __wakeup() |
|
804 | |||
805 | /** |
||
806 | * Serialize or deserialize the scheme. |
||
807 | * |
||
808 | * @param array $scheme |
||
809 | * |
||
810 | * @return array |
||
811 | */ |
||
812 | 3 | protected static function toggleSerializeScheme(array $scheme) |
|
824 | |||
825 | /** |
||
826 | * Serialize the hooks' handlers. |
||
827 | * |
||
828 | * @return array |
||
829 | */ |
||
830 | 3 | protected static function serializeHandlers() |
|
841 | |||
842 | /** |
||
843 | * Reinstall hooks' handlers. |
||
844 | * |
||
845 | * @param array $handlers |
||
846 | */ |
||
847 | 3 | protected static function reinstallHandlers(array $handlers) |
|
859 | |||
860 | /** |
||
861 | * Serialize or deserialize the specified closure and then replace the original value. |
||
862 | * |
||
863 | * @param array $options |
||
864 | * @param int|string $key |
||
865 | */ |
||
866 | 3 | protected static function toggleSerializeClosure(array &$options, $key) |
|
878 | } |
||
879 |