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 | * Agent instances. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $agents = []; |
||
26 | |||
27 | /** |
||
28 | * The dispatch scheme of agents. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected static $scheme = []; |
||
33 | |||
34 | /** |
||
35 | * The configuration information of agents. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected static $agentsConfig = []; |
||
40 | |||
41 | /** |
||
42 | * Whether to use the queue. |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected static $enableQueue = false; |
||
47 | |||
48 | /** |
||
49 | * How to use the queue system. |
||
50 | * |
||
51 | * @var \Closure |
||
52 | */ |
||
53 | protected static $howToUseQueue = null; |
||
54 | |||
55 | /** |
||
56 | * Available hooks. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected static $availableHooks = [ |
||
61 | 'beforeRun', |
||
62 | 'beforeDriverRun', |
||
63 | 'afterDriverRun', |
||
64 | 'afterRun', |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Closure serializer. |
||
69 | * |
||
70 | * @var Serializer |
||
71 | */ |
||
72 | protected static $serializer = null; |
||
73 | |||
74 | /** |
||
75 | * Data container. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $smsData = [ |
||
80 | 'type' => self::TYPE_SMS, |
||
81 | 'to' => null, |
||
82 | 'templates' => [], |
||
83 | 'templateData' => [], |
||
84 | 'content' => null, |
||
85 | 'voiceCode' => null, |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * The name of first agent. |
||
90 | * |
||
91 | * @var string|null |
||
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 | public function __construct($autoBoot = true) |
||
120 | |||
121 | 6 | /** |
|
122 | * Bootstrap the task. |
||
123 | 6 | */ |
|
124 | 3 | public static function bootstrap() |
|
131 | 6 | ||
132 | /** |
||
133 | 6 | * Whether the task initialized. |
|
134 | 3 | * |
|
135 | 3 | * Note: 判断drivers是否为空不能用'empty',因为在TaskBalance库的中Task类的drivers属性是受保护的(不可访问), |
|
136 | 3 | * 虽然通过魔术方法可以获取到其值,但在其目前版本(v0.4.2)其内部却并没有使用'__isset'魔术方法对'empty'或'isset'函数进行逻辑补救. |
|
137 | 6 | * |
|
138 | * @return bool |
||
139 | */ |
||
140 | protected static function taskInitialized() |
||
146 | |||
147 | 12 | /** |
|
148 | * Get the task instance. |
||
149 | 12 | * |
|
150 | * @return Task |
||
151 | 12 | */ |
|
152 | public static function getTask() |
||
160 | |||
161 | 21 | /** |
|
162 | 9 | * Configuration. |
|
163 | 9 | */ |
|
164 | protected static function configuration() |
||
174 | 6 | ||
175 | 3 | /** |
|
176 | 3 | * Initialize the dispatch scheme. |
|
177 | 6 | * |
|
178 | 6 | * @param array $config |
|
179 | 6 | */ |
|
180 | 6 | protected static function initScheme(array &$config) |
|
186 | |||
187 | 3 | /** |
|
188 | * Initialize the configuration information. |
||
189 | 3 | * |
|
190 | 3 | * @param array $agents |
|
191 | 3 | * @param array $config |
|
192 | 3 | */ |
|
193 | protected static function initAgentsConfig(array $agents, array &$config) |
||
205 | 3 | ||
206 | 3 | /** |
|
207 | 3 | * Validate the configuration. |
|
208 | 3 | * |
|
209 | 3 | * @throws PhpSmsException |
|
210 | 3 | */ |
|
211 | 3 | protected static function validateConfig() |
|
217 | |||
218 | 6 | /** |
|
219 | * Initialize the task. |
||
220 | 6 | */ |
|
221 | protected static function initTask() |
||
251 | 18 | ||
252 | /** |
||
253 | 18 | * Parse the higher-order dispatch scheme. |
|
254 | 3 | * |
|
255 | 3 | * @param array $options |
|
256 | 3 | * |
|
257 | * @return array |
||
258 | */ |
||
259 | protected static function parseScheme(array $options) |
||
269 | 9 | ||
270 | 9 | /** |
|
271 | 9 | * Get the agent instance by name. |
|
272 | * |
||
273 | 9 | * @param string $name |
|
274 | * @param array $options |
||
275 | * |
||
276 | * @throws PhpSmsException |
||
277 | * |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public static function getAgent($name, array $options = []) |
||
308 | |||
309 | /** |
||
310 | * Whether has the specified agent. |
||
311 | * |
||
312 | 36 | * @param string $name |
|
313 | * |
||
314 | 36 | * @return bool |
|
315 | */ |
||
316 | public static function hasAgent($name) |
||
320 | |||
321 | /** |
||
322 | * Set or get the dispatch scheme. |
||
323 | * |
||
324 | * @param mixed $name |
||
325 | 18 | * @param mixed $scheme |
|
326 | * |
||
327 | * @return mixed |
||
328 | 6 | */ |
|
329 | 3 | public static function scheme($name = null, $scheme = null) |
|
339 | |||
340 | /** |
||
341 | * Modify the dispatch scheme of agent. |
||
342 | * |
||
343 | * @param $key |
||
344 | 6 | * @param $value |
|
345 | * |
||
346 | 6 | * @throws PhpSmsException |
|
347 | */ |
||
348 | protected static function modifyScheme($key, $value) |
||
356 | |||
357 | /** |
||
358 | * Set or get the configuration information. |
||
359 | * |
||
360 | * @param mixed $name |
||
361 | * @param mixed $config |
||
362 | * @param bool $override |
||
363 | * |
||
364 | 18 | * @throws PhpSmsException |
|
365 | * |
||
366 | 18 | * @return array |
|
367 | 6 | */ |
|
368 | 6 | public static function config($name = null, $config = null, $override = false) |
|
387 | |||
388 | /** |
||
389 | * Modify the configuration information. |
||
390 | * |
||
391 | * @param string $key |
||
392 | 9 | * @param array $value |
|
393 | * |
||
394 | 9 | * @throws PhpSmsException |
|
395 | 9 | */ |
|
396 | 9 | protected static function modifyConfig($key, array $value) |
|
404 | |||
405 | /** |
||
406 | * Validate the agent name. |
||
407 | * Expected type is string, except the string of number. |
||
408 | * |
||
409 | 12 | * @param string $name |
|
410 | * |
||
411 | 12 | * @throws PhpSmsException |
|
412 | */ |
||
413 | protected static function validateAgentName($name) |
||
419 | |||
420 | 6 | /** |
|
421 | * Tear down agent use scheme and prepare to create and start a new task, |
||
422 | 6 | * so before do it must destroy old task instance. |
|
423 | 6 | */ |
|
424 | 6 | public static function cleanScheme() |
|
429 | 6 | ||
430 | /** |
||
431 | 6 | * Tear down all the configuration information of agent. |
|
432 | 6 | */ |
|
433 | public static function cleanConfig() |
||
437 | |||
438 | /** |
||
439 | * Create a instance for send sms, |
||
440 | * you can also set templates or content at the same time. |
||
441 | * |
||
442 | * @param mixed $agentName |
||
443 | * @param mixed $tempId |
||
444 | * |
||
445 | * @return Sms |
||
446 | */ |
||
447 | public static function make($agentName = null, $tempId = null) |
||
463 | |||
464 | /** |
||
465 | * Create a instance for send voice verify code, |
||
466 | * you can also set verify code at the same time. |
||
467 | * |
||
468 | 3 | * @param int|string|null $code |
|
469 | * |
||
470 | 3 | * @return Sms |
|
471 | 3 | */ |
|
472 | 3 | public static function voice($code = null) |
|
480 | |||
481 | /** |
||
482 | * Set whether to use the queue system, |
||
483 | * and define how to use it. |
||
484 | * |
||
485 | * @param mixed $enable |
||
486 | 3 | * @param mixed $handler |
|
487 | * |
||
488 | 3 | * @return bool |
|
489 | 3 | */ |
|
490 | public static function queue($enable = null, $handler = null) |
||
506 | |||
507 | /** |
||
508 | * Set the recipient`s mobile number. |
||
509 | * |
||
510 | 6 | * @param string $mobile |
|
511 | * |
||
512 | 6 | * @return $this |
|
513 | */ |
||
514 | 6 | public function to($mobile) |
|
520 | |||
521 | /** |
||
522 | * Set the sms content. |
||
523 | * |
||
524 | 3 | * @param string $content |
|
525 | * |
||
526 | 3 | * @return $this |
|
527 | */ |
||
528 | 3 | public function content($content) |
|
534 | |||
535 | /** |
||
536 | * Set the template ids. |
||
537 | * |
||
538 | * @param mixed $name |
||
539 | 3 | * @param mixed $tempId |
|
540 | * |
||
541 | 3 | * @return $this |
|
542 | */ |
||
543 | 3 | public function template($name, $tempId = null) |
|
549 | |||
550 | /** |
||
551 | * Set the template data. |
||
552 | * |
||
553 | 3 | * @param mixed $name |
|
554 | * @param mixed $value |
||
555 | 3 | * |
|
556 | * @return $this |
||
557 | 3 | */ |
|
558 | public function data($name, $value = null) |
||
564 | |||
565 | /** |
||
566 | * Set the first agent. |
||
567 | 3 | * |
|
568 | * @param string $name |
||
569 | 3 | * |
|
570 | * @return $this |
||
571 | 3 | */ |
|
572 | public function agent($name) |
||
578 | |||
579 | /** |
||
580 | * Start send. |
||
581 | * |
||
582 | * If call with a `true` parameter, this system will immediately start request to send sms whatever whether to use the queue. |
||
583 | * if the current instance has pushed to the queue, you can recall this method in queue system without any parameter, |
||
584 | * so this mechanism in order to make you convenient to use this method in queue system. |
||
585 | 18 | * |
|
586 | * @param bool $immediately |
||
587 | 18 | * |
|
588 | 18 | * @return mixed |
|
589 | 18 | */ |
|
590 | 18 | public function send($immediately = false) |
|
604 | |||
605 | /** |
||
606 | * Push to the queue system. |
||
607 | * |
||
608 | * @throws \Exception | PhpSmsException |
||
609 | 3 | * |
|
610 | * @return mixed |
||
611 | 3 | */ |
|
612 | public function push() |
||
626 | |||
627 | /** |
||
628 | * Get all of the data. |
||
629 | * |
||
630 | * @param null|string $key |
||
631 | * |
||
632 | 36 | * @return mixed |
|
633 | */ |
||
634 | 36 | public function all($key = null) |
|
642 | |||
643 | /** |
||
644 | * The alias of `all` method. |
||
645 | * |
||
646 | * @param null|string $key |
||
647 | * |
||
648 | * @return mixed |
||
649 | 9 | */ |
|
650 | public function getData($key = null) |
||
654 | 9 | ||
655 | 9 | /** |
|
656 | 9 | * Define the static hook methods by overload static method. |
|
657 | 9 | * |
|
658 | 9 | * @param string $name |
|
659 | 9 | * @param array $args |
|
660 | 9 | * |
|
661 | 9 | * @throws PhpSmsException |
|
662 | */ |
||
663 | public static function __callStatic($name, $args) |
||
680 | |||
681 | 3 | /** |
|
682 | 3 | * Define the hook methods by overload method. |
|
683 | * |
||
684 | * @param string $name |
||
685 | 3 | * @param array $args |
|
686 | * |
||
687 | * @throws PhpSmsException |
||
688 | * @throws \Exception |
||
689 | */ |
||
690 | public function __call($name, $args) |
||
698 | 3 | ||
699 | /** |
||
700 | * Serialize magic method. |
||
701 | * |
||
702 | 3 | * @return array |
|
703 | */ |
||
704 | public function __sleep() |
||
716 | 3 | ||
717 | 3 | /** |
|
718 | 3 | * Deserialize magic method. |
|
719 | 3 | */ |
|
720 | public function __wakeup() |
||
731 | |||
732 | 3 | /** |
|
733 | * Get a closure serializer. |
||
734 | * |
||
735 | * @return Serializer |
||
736 | */ |
||
737 | protected static function getSerializer() |
||
745 | 3 | ||
746 | 3 | /** |
|
747 | 3 | * Serialize or deserialize the scheme. |
|
748 | 3 | * |
|
749 | 3 | * @param array $scheme |
|
750 | * |
||
751 | 3 | * @return array |
|
752 | */ |
||
753 | protected static function serializeOrDeserializeScheme(array $scheme) |
||
764 | 3 | ||
765 | 3 | /** |
|
766 | 3 | * Serialize the hooks` handlers. |
|
767 | 3 | * |
|
768 | * @return array |
||
769 | 3 | */ |
|
770 | protected static function serializeHandlers() |
||
782 | 3 | ||
783 | 3 | /** |
|
784 | 3 | * Reinstall hooks` handlers. |
|
785 | 3 | * |
|
786 | 3 | * @param array $handlers |
|
787 | 3 | */ |
|
788 | 3 | protected static function reinstallHandlers(array $handlers) |
|
800 | |||
801 | 3 | /** |
|
802 | 3 | * Serialize or deserialize the specified closure and then replace the original value. |
|
803 | 3 | * |
|
804 | 3 | * @param array $options |
|
805 | 3 | * @param int|string $key |
|
806 | 3 | */ |
|
807 | 3 | protected static function serializeOrDeserializeClosureAndReplace(array &$options, $key) |
|
819 | } |
||
820 |
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..