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 |
||
14 | class Sms |
||
15 | { |
||
16 | const TASK_NAME = 'PhpSms'; |
||
17 | const TYPE_SMS = 1; |
||
18 | const TYPE_VOICE = 2; |
||
19 | |||
20 | /** |
||
21 | * The instances of Agent. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $agents = []; |
||
26 | |||
27 | /** |
||
28 | * The dispatch scheme of agent, |
||
29 | * and these agents are available. |
||
30 | * example: |
||
31 | * [ |
||
32 | * 'Agent1' => '10 backup', |
||
33 | * 'Agent2' => '20 backup', |
||
34 | * ] |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected static $scheme = []; |
||
39 | |||
40 | /** |
||
41 | * The configuration information of agents. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected static $agentsConfig = []; |
||
46 | |||
47 | /** |
||
48 | * Whether to use the queue. |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected static $enableQueue = false; |
||
53 | |||
54 | /** |
||
55 | * How to use the queue. |
||
56 | * |
||
57 | * @var \Closure |
||
58 | */ |
||
59 | protected static $howToUseQueue = null; |
||
60 | |||
61 | /** |
||
62 | * The available hooks for balancing task. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected static $availableHooks = [ |
||
67 | 'beforeRun', |
||
68 | 'beforeDriverRun', |
||
69 | 'afterDriverRun', |
||
70 | 'afterRun', |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * An instance of class [SuperClosure\Serializer] for serialize closure objects. |
||
75 | * |
||
76 | * @var Serializer |
||
77 | */ |
||
78 | protected static $serializer = null; |
||
79 | |||
80 | /** |
||
81 | * The data container of SMS/voice verify. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $smsData = [ |
||
86 | 'type' => self::TYPE_SMS, |
||
87 | 'to' => null, |
||
88 | 'templates' => [], |
||
89 | 'templateData' => [], |
||
90 | 'content' => null, |
||
91 | 'voiceCode' => null, |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * The name of first agent. |
||
96 | * |
||
97 | * @var string|null |
||
98 | */ |
||
99 | protected $firstAgent = null; |
||
100 | |||
101 | /** |
||
102 | * Whether the current instance has already pushed to the queue system. |
||
103 | * |
||
104 | * @var bool |
||
105 | */ |
||
106 | protected $pushedToQueue = false; |
||
107 | |||
108 | /** |
||
109 | * Status container, |
||
110 | * store some configuration information before serialize current instance(before enqueue). |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | protected $_status_before_enqueue_ = []; |
||
115 | |||
116 | /** |
||
117 | * Constructor |
||
118 | * |
||
119 | * @param bool $autoBoot |
||
120 | */ |
||
121 | 6 | public function __construct($autoBoot = true) |
|
127 | |||
128 | /** |
||
129 | * Boot balancing task for send SMS/voice verify. |
||
130 | */ |
||
131 | public static function bootstrap() |
||
138 | 3 | ||
139 | 3 | /** |
|
140 | 2 | * Whether task initialized. |
|
141 | 6 | * |
|
142 | * Note: 判断drivers是否为空不能用'empty',因为在TaskBalance库的中Task类的drivers属性是受保护的(不可访问), |
||
143 | * 虽然通过魔术方法可以获取到其值,但在其目前版本(v0.4.2)其内部却并没有使用'__isset'魔术方法对'empty'或'isset'函数进行逻辑补救. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected static function taskInitialized() |
||
153 | |||
154 | 15 | /** |
|
155 | * Get or generate a balancing task instance for send SMS/voice verify. |
||
156 | * |
||
157 | * @return Task |
||
158 | */ |
||
159 | public static function getTask() |
||
167 | 6 | ||
168 | 6 | /** |
|
169 | 6 | * Configuration. |
|
170 | */ |
||
171 | protected static function configuration() |
||
181 | 3 | ||
182 | /** |
||
183 | * Try to read the dispatch scheme of agent from config file. |
||
184 | * |
||
185 | * @param array $config |
||
186 | */ |
||
187 | protected static function initScheme(array &$config) |
||
193 | |||
194 | 3 | /** |
|
195 | 3 | * Try to initialize the specified agents` configuration information. |
|
196 | 3 | * |
|
197 | 3 | * @param array $agents |
|
198 | 3 | * @param array $config |
|
199 | 2 | */ |
|
200 | 3 | protected static function initAgentsConfig(array $agents, array &$config) |
|
212 | 6 | ||
213 | /** |
||
214 | * validate configuration. |
||
215 | * |
||
216 | * @throws PhpSmsException |
||
217 | */ |
||
218 | protected static function validateConfig() |
||
224 | 3 | ||
225 | 3 | /** |
|
226 | 2 | * Initialize the task. |
|
227 | */ |
||
228 | protected static function initTask() |
||
257 | 9 | ||
258 | /** |
||
259 | 9 | * Parsing the dispatch scheme. |
|
260 | 9 | * 解析代理器的数组模式的调度配置 |
|
261 | 9 | * |
|
262 | 9 | * @param array $options |
|
263 | 9 | * |
|
264 | * @return array |
||
265 | 9 | */ |
|
266 | protected static function parseScheme(array $options) |
||
276 | |||
277 | /** |
||
278 | 27 | * Get a sms agent instance by agent name, |
|
279 | * if null, will try to create a new agent instance. |
||
280 | 27 | * |
|
281 | 9 | * @param string $name |
|
282 | 9 | * |
|
283 | 9 | * @throws PhpSmsException |
|
284 | 9 | * |
|
285 | 9 | * @return mixed |
|
286 | 3 | */ |
|
287 | 8 | public static function getAgent($name) |
|
305 | |||
306 | 36 | /** |
|
307 | * Whether to has specified agent. |
||
308 | * |
||
309 | * @param string $name |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public static function hasAgent($name) |
||
317 | 18 | ||
318 | /** |
||
319 | * Set or get the dispatch scheme of agent by name. |
||
320 | 6 | * |
|
321 | 3 | * @param mixed $name |
|
322 | 6 | * @param mixed $scheme |
|
323 | 6 | * |
|
324 | 4 | * @return mixed |
|
325 | 18 | */ |
|
326 | public static function scheme($name = null, $scheme = null) |
||
336 | 6 | ||
337 | /** |
||
338 | 6 | * Modify the dispatch scheme of agent by name. |
|
339 | 6 | * |
|
340 | 6 | * @param $key |
|
341 | * @param $value |
||
342 | * |
||
343 | * @throws PhpSmsException |
||
344 | */ |
||
345 | protected static function modifyScheme($key, $value) |
||
353 | 18 | ||
354 | /** |
||
355 | 18 | * Set or get configuration information by agent name. |
|
356 | 6 | * |
|
357 | 4 | * @param mixed $name |
|
358 | * @param mixed $config |
||
359 | * @param bool $override |
||
360 | 9 | * |
|
361 | 9 | * @throws PhpSmsException |
|
362 | 6 | * |
|
363 | 18 | * @return array |
|
364 | 6 | */ |
|
365 | 6 | public static function config($name = null, $config = null, $override = false) |
|
384 | 9 | ||
385 | 9 | /** |
|
386 | 3 | * Modify the configuration information of agent by name. |
|
387 | 2 | * |
|
388 | 9 | * @param string $key |
|
389 | * @param array $value |
||
390 | * |
||
391 | * @throws PhpSmsException |
||
392 | */ |
||
393 | protected static function modifyConfig($key, array $value) |
||
401 | |||
402 | /** |
||
403 | 12 | * Validate the agent name. |
|
404 | * Agent name must be a string, but not be a number string |
||
405 | * |
||
406 | * @param string $name |
||
407 | * |
||
408 | * @throws PhpSmsException |
||
409 | 6 | */ |
|
410 | protected static function validateAgentName($name) |
||
416 | |||
417 | /** |
||
418 | 6 | * Tear down agent use scheme and prepare to create and start a new balancing task, |
|
419 | * so before do it must destroy old task instance. |
||
420 | 6 | */ |
|
421 | 6 | public static function cleanScheme() |
|
426 | |||
427 | /** |
||
428 | * Tear down all the configuration information of agent. |
||
429 | */ |
||
430 | public static function cleanConfig() |
||
434 | |||
435 | /** |
||
436 | * Create a sms instance send SMS, |
||
437 | * your can also set SMS templates or content at the same time. |
||
438 | * |
||
439 | * @param mixed $agentName |
||
440 | * @param mixed $tempId |
||
441 | * |
||
442 | * @return Sms |
||
443 | */ |
||
444 | public static function make($agentName = null, $tempId = null) |
||
460 | 3 | ||
461 | 3 | /** |
|
462 | * Create a sms instance send voice verify, |
||
463 | 3 | * your can also set verify code at the same time. |
|
464 | * |
||
465 | * @param int|string|null $code |
||
466 | * |
||
467 | * @return Sms |
||
468 | */ |
||
469 | public static function voice($code = null) |
||
477 | 3 | ||
478 | /** |
||
479 | 3 | * Set whether to use the queue system, and define how to use it. |
|
480 | 3 | * |
|
481 | 3 | * @param mixed $enable |
|
482 | 2 | * @param mixed $handler |
|
483 | 3 | * |
|
484 | 3 | * @return bool |
|
485 | 3 | */ |
|
486 | 2 | public static function queue($enable = null, $handler = null) |
|
502 | 6 | ||
503 | /** |
||
504 | * Set the recipient`s mobile number. |
||
505 | * |
||
506 | * @param string $mobile |
||
507 | * |
||
508 | * @return $this |
||
509 | */ |
||
510 | public function to($mobile) |
||
516 | 3 | ||
517 | /** |
||
518 | * Set the content for content SMS. |
||
519 | * |
||
520 | * @param string $content |
||
521 | * |
||
522 | * @return $this |
||
523 | */ |
||
524 | public function content($content) |
||
530 | |||
531 | 3 | /** |
|
532 | * Set the template id for template SMS. |
||
533 | * |
||
534 | * @param mixed $name |
||
535 | * @param mixed $tempId |
||
536 | * |
||
537 | * @return $this |
||
538 | */ |
||
539 | public function template($name, $tempId = null) |
||
545 | 3 | ||
546 | /** |
||
547 | * Set the template data for template SMS. |
||
548 | * |
||
549 | * @param array $data |
||
550 | * |
||
551 | * @return $this |
||
552 | */ |
||
553 | public function data(array $data) |
||
559 | 3 | ||
560 | /** |
||
561 | * Set the first agent by name. |
||
562 | * |
||
563 | * @param string $name |
||
564 | * |
||
565 | * @return $this |
||
566 | */ |
||
567 | public function agent($name) |
||
573 | 18 | ||
574 | /** |
||
575 | 18 | * Start send SMS/voice verify. |
|
576 | 18 | * |
|
577 | 12 | * If give a true parameter, this system will immediately start request to send SMS/voice verify whatever whether to use the queue. |
|
578 | 18 | * if you are already pushed sms instance to the queue, you can recall the method `send()` in queue system without `true` parameter, |
|
579 | 18 | * so this mechanism in order to make you convenient use the method `send()` in queue system. |
|
580 | 18 | * |
|
581 | 18 | * @param bool $immediately |
|
582 | 12 | * |
|
583 | 12 | * @return mixed |
|
584 | 3 | */ |
|
585 | public function send($immediately = false) |
||
601 | 3 | ||
602 | /** |
||
603 | 3 | * Push to the queue by a custom method. |
|
604 | * |
||
605 | * @throws \Exception | PhpSmsException |
||
606 | * |
||
607 | * @return mixed |
||
608 | */ |
||
609 | public function push() |
||
624 | |||
625 | /** |
||
626 | 36 | * Get all the data of SMS/voice verify. |
|
627 | * |
||
628 | * @param null|string $name |
||
629 | * |
||
630 | * @return mixed |
||
631 | */ |
||
632 | public function getData($name = null) |
||
640 | 9 | ||
641 | 9 | /** |
|
642 | 9 | * Overload static method. |
|
643 | 9 | * |
|
644 | 9 | * @param string $name |
|
645 | 9 | * @param array $args |
|
646 | 9 | * |
|
647 | 9 | * @throws PhpSmsException |
|
648 | 9 | */ |
|
649 | 6 | public static function __callStatic($name, $args) |
|
668 | |||
669 | 3 | /** |
|
670 | 2 | * Overload method. |
|
671 | * |
||
672 | * @param string $name |
||
673 | 3 | * @param array $args |
|
674 | * |
||
675 | * @throws PhpSmsException |
||
676 | * @throws \Exception |
||
677 | */ |
||
678 | public function __call($name, $args) |
||
686 | 2 | ||
687 | /** |
||
688 | * Serialize magic method. |
||
689 | * |
||
690 | 3 | * @return array |
|
691 | */ |
||
692 | public function __sleep() |
||
704 | 3 | ||
705 | 3 | /** |
|
706 | 3 | * Deserialize magic method. |
|
707 | 3 | */ |
|
708 | public function __wakeup() |
||
720 | 3 | ||
721 | /** |
||
722 | * Get a closure serializer. |
||
723 | * |
||
724 | * @return Serializer |
||
725 | */ |
||
726 | protected static function getSerializer() |
||
734 | 3 | ||
735 | 3 | /** |
|
736 | 2 | * Serialize or deserialize the agent use scheme. |
|
737 | 2 | * |
|
738 | * @param array $scheme |
||
739 | 3 | * |
|
740 | * @return array |
||
741 | */ |
||
742 | protected static function serializeOrDeserializeScheme(array $scheme) |
||
753 | 3 | ||
754 | 2 | /** |
|
755 | 2 | * Serialize the hooks` handlers of balancing task |
|
756 | * |
||
757 | 3 | * @return array |
|
758 | */ |
||
759 | protected static function serializeHandlers() |
||
771 | 3 | ||
772 | 2 | /** |
|
773 | 3 | * Reinstall hooks` handlers for balancing task. |
|
774 | 2 | * |
|
775 | 2 | * @param array $handlers |
|
776 | 3 | */ |
|
777 | protected static function reinstallHandlers(array $handlers) |
||
789 | 3 | ||
790 | 3 | /** |
|
791 | 3 | * Serialize/deserialize the specified closure and replace the origin value. |
|
792 | 3 | * |
|
793 | 3 | * @param array $options |
|
794 | 2 | * @param int|string $key |
|
795 | 3 | */ |
|
796 | protected static function serializeOrDeserializeClosureAndReplace(array &$options, $key) |
||
808 | } |
||
809 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..