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 agent name. |
||
423 | * Expected type is string, except the string of number. |
||
424 | * |
||
425 | * @param string $name |
||
426 | * |
||
427 | * @throws PhpSmsException |
||
428 | */ |
||
429 | 12 | protected static function validateAgentName($name) |
|
435 | |||
436 | /** |
||
437 | * Tear down scheme. |
||
438 | */ |
||
439 | 6 | public static function cleanScheme() |
|
443 | |||
444 | /** |
||
445 | * Tear down config information. |
||
446 | */ |
||
447 | 6 | public static function cleanConfig() |
|
451 | |||
452 | /** |
||
453 | * Create a instance for send sms. |
||
454 | * |
||
455 | * @param mixed $agentName |
||
456 | * @param mixed $tempId |
||
457 | * |
||
458 | * @return Sms |
||
459 | */ |
||
460 | public static function make($agentName = null, $tempId = null) |
||
476 | |||
477 | /** |
||
478 | * Create a instance for send voice. |
||
479 | * |
||
480 | * @param int|string|null $code |
||
481 | * |
||
482 | * @return Sms |
||
483 | */ |
||
484 | 3 | public static function voice($code = null) |
|
492 | |||
493 | /** |
||
494 | * Set whether to use the queue system, |
||
495 | * and define how to use it. |
||
496 | * |
||
497 | * @param bool|\Closure|null $enable |
||
498 | * @param \Closure|null $handler |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | 3 | public static function queue($enable = null, $handler = null) |
|
518 | |||
519 | /** |
||
520 | * Set the type of Sms instance. |
||
521 | * |
||
522 | * @param $type |
||
523 | * |
||
524 | * @throws PhpSmsException |
||
525 | * |
||
526 | * @return $this |
||
527 | */ |
||
528 | 3 | public function type($type) |
|
537 | |||
538 | /** |
||
539 | * Set the recipient`s mobile number. |
||
540 | * |
||
541 | * @param string $mobile |
||
542 | * |
||
543 | * @return $this |
||
544 | */ |
||
545 | 6 | public function to($mobile) |
|
551 | |||
552 | /** |
||
553 | * Set the sms content. |
||
554 | * |
||
555 | * @param string $content |
||
556 | * |
||
557 | * @return $this |
||
558 | */ |
||
559 | 3 | public function content($content) |
|
565 | |||
566 | /** |
||
567 | * Set the template ids. |
||
568 | * |
||
569 | * @param mixed $name |
||
570 | * @param mixed $tempId |
||
571 | * |
||
572 | * @return $this |
||
573 | */ |
||
574 | 3 | public function template($name, $tempId = null) |
|
580 | |||
581 | /** |
||
582 | * Set the template data. |
||
583 | * |
||
584 | * @param mixed $key |
||
585 | * @param mixed $value |
||
586 | * |
||
587 | * @return $this |
||
588 | */ |
||
589 | 3 | public function data($key, $value = null) |
|
595 | |||
596 | /** |
||
597 | * Set the voice code. |
||
598 | * |
||
599 | * @param string|int $code |
||
600 | * |
||
601 | * @return $this |
||
602 | */ |
||
603 | 3 | public function code($code) |
|
609 | |||
610 | /** |
||
611 | * Set voice files. |
||
612 | * |
||
613 | * @param string|array $name |
||
614 | * @param string|int $id |
||
615 | * |
||
616 | * @return $this |
||
617 | */ |
||
618 | public function files($name, $id = null) |
||
624 | |||
625 | /** |
||
626 | * Set params of agent. |
||
627 | * |
||
628 | * @param string|array $name |
||
629 | * @param array|bool|null $params |
||
630 | * @param bool $override |
||
631 | * |
||
632 | * @return $this |
||
633 | */ |
||
634 | public function params($name, $params = null, $override = false) |
||
651 | |||
652 | /** |
||
653 | * Set the first agent. |
||
654 | * |
||
655 | * @param string $name |
||
656 | * |
||
657 | * @throws PhpSmsException |
||
658 | * |
||
659 | * @return $this |
||
660 | */ |
||
661 | 3 | public function agent($name) |
|
670 | |||
671 | /** |
||
672 | * Start send. |
||
673 | * |
||
674 | * If call with a `true` parameter, this system will immediately start request to send sms whatever whether to use the queue. |
||
675 | * if the current instance has pushed to the queue, you can recall this method in queue system without any parameter, |
||
676 | * so this mechanism in order to make you convenient to use this method in queue system. |
||
677 | * |
||
678 | * @param bool $immediately |
||
679 | * |
||
680 | * @return mixed |
||
681 | */ |
||
682 | 18 | public function send($immediately = false) |
|
693 | |||
694 | /** |
||
695 | * Push to the queue system. |
||
696 | * |
||
697 | * @throws \Exception | PhpSmsException |
||
698 | * |
||
699 | * @return mixed |
||
700 | */ |
||
701 | 3 | public function push() |
|
715 | |||
716 | /** |
||
717 | * Get all of the data. |
||
718 | * |
||
719 | * @param null|string $key |
||
720 | * |
||
721 | * @return mixed |
||
722 | */ |
||
723 | 36 | public function all($key = null) |
|
731 | |||
732 | /** |
||
733 | * Define the static hook methods by overload static method. |
||
734 | * |
||
735 | * @param string $name |
||
736 | * @param array $args |
||
737 | * |
||
738 | * @throws PhpSmsException |
||
739 | */ |
||
740 | 9 | public static function __callStatic($name, $args) |
|
753 | |||
754 | /** |
||
755 | * Define the hook methods by overload method. |
||
756 | * |
||
757 | * @param string $name |
||
758 | * @param array $args |
||
759 | * |
||
760 | * @throws PhpSmsException |
||
761 | * @throws \Exception |
||
762 | */ |
||
763 | 3 | public function __call($name, $args) |
|
771 | |||
772 | /** |
||
773 | * Serialize magic method. |
||
774 | * |
||
775 | * @return array |
||
776 | */ |
||
777 | 3 | public function __sleep() |
|
789 | |||
790 | /** |
||
791 | * Deserialize magic method. |
||
792 | */ |
||
793 | 3 | public function __wakeup() |
|
802 | |||
803 | /** |
||
804 | * Serialize or deserialize the scheme. |
||
805 | * |
||
806 | * @param array $scheme |
||
807 | * |
||
808 | * @return array |
||
809 | */ |
||
810 | 3 | protected static function toggleSerializeScheme(array $scheme) |
|
822 | |||
823 | /** |
||
824 | * Serialize the hooks' handlers. |
||
825 | * |
||
826 | * @return array |
||
827 | */ |
||
828 | 3 | protected static function serializeHandlers() |
|
839 | |||
840 | /** |
||
841 | * Reinstall hooks' handlers. |
||
842 | * |
||
843 | * @param array $handlers |
||
844 | */ |
||
845 | 3 | protected static function reinstallHandlers(array $handlers) |
|
857 | |||
858 | /** |
||
859 | * Serialize or deserialize the specified closure and then replace the original value. |
||
860 | * |
||
861 | * @param array $options |
||
862 | * @param int|string $key |
||
863 | */ |
||
864 | 3 | protected static function toggleSerializeClosure(array &$options, $key) |
|
876 | } |
||
877 |