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 | 6 | public function __construct($autoBoot = true) |
|
120 | |||
121 | /** |
||
122 | * Bootstrap the task. |
||
123 | */ |
||
124 | 6 | public static function bootstrap() |
|
131 | |||
132 | /** |
||
133 | * Whether the task initialized. |
||
134 | * |
||
135 | * Note: 判断drivers是否为空不能用'empty',因为在TaskBalance库的中Task类的drivers属性是受保护的(不可访问), |
||
136 | * 虽然通过魔术方法可以获取到其值,但在其目前版本(v0.4.2)其内部却并没有使用'__isset'魔术方法对'empty'或'isset'函数进行逻辑补救. |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | 12 | protected static function taskInitialized() |
|
146 | |||
147 | /** |
||
148 | * Get the task instance. |
||
149 | * |
||
150 | * @return Task |
||
151 | */ |
||
152 | 21 | public static function getTask() |
|
160 | |||
161 | /** |
||
162 | * Configuration. |
||
163 | */ |
||
164 | 6 | protected static function configuration() |
|
174 | |||
175 | /** |
||
176 | * Initialize the dispatch scheme. |
||
177 | * |
||
178 | * @param array $config |
||
179 | */ |
||
180 | 3 | protected static function initScheme(array &$config) |
|
186 | |||
187 | /** |
||
188 | * Initialize the configuration information. |
||
189 | * |
||
190 | * @param array $agents |
||
191 | * @param array $config |
||
192 | */ |
||
193 | 6 | protected static function initAgentsConfig(array $agents, array &$config) |
|
205 | |||
206 | /** |
||
207 | * Validate the configuration. |
||
208 | * |
||
209 | * @throws PhpSmsException |
||
210 | */ |
||
211 | 6 | protected static function validateConfig() |
|
217 | |||
218 | /** |
||
219 | * Initialize the task. |
||
220 | */ |
||
221 | 18 | protected static function initTask() |
|
251 | |||
252 | /** |
||
253 | * Parse the higher-order dispatch scheme. |
||
254 | * |
||
255 | * @param array $options |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | 3 | protected static function parseScheme(array $options) |
|
269 | |||
270 | /** |
||
271 | * Get the agent instance by name. |
||
272 | * |
||
273 | * @param string $name |
||
274 | * @param array $options |
||
275 | * |
||
276 | * @throws PhpSmsException |
||
277 | * |
||
278 | * @return mixed |
||
279 | */ |
||
280 | 27 | public static function getAgent($name, array $options = []) |
|
308 | |||
309 | /** |
||
310 | * Whether has the specified agent. |
||
311 | * |
||
312 | * @param string $name |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | 36 | public static function hasAgent($name) |
|
320 | |||
321 | /** |
||
322 | * Set or get the dispatch scheme. |
||
323 | * |
||
324 | * @param mixed $name |
||
325 | * @param mixed $scheme |
||
326 | * |
||
327 | * @return mixed |
||
328 | */ |
||
329 | 18 | public static function scheme($name = null, $scheme = null) |
|
339 | |||
340 | /** |
||
341 | * Modify the dispatch scheme of agent. |
||
342 | * |
||
343 | * @param $key |
||
344 | * @param $value |
||
345 | * |
||
346 | * @throws PhpSmsException |
||
347 | */ |
||
348 | 6 | 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 | * @throws PhpSmsException |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | 18 | public static function config($name = null, $config = null, $override = false) |
|
387 | |||
388 | /** |
||
389 | * Modify the configuration information. |
||
390 | * |
||
391 | * @param string $key |
||
392 | * @param array $value |
||
393 | * |
||
394 | * @throws PhpSmsException |
||
395 | */ |
||
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 | * @param string $name |
||
410 | * |
||
411 | * @throws PhpSmsException |
||
412 | */ |
||
413 | 12 | protected static function validateAgentName($name) |
|
419 | |||
420 | /** |
||
421 | * Tear down agent use scheme and prepare to create and start a new task, |
||
422 | * so before do it must destroy old task instance. |
||
423 | */ |
||
424 | 6 | public static function cleanScheme() |
|
429 | |||
430 | /** |
||
431 | * Tear down all the configuration information of agent. |
||
432 | */ |
||
433 | 6 | 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 | * @param int|string|null $code |
||
469 | * |
||
470 | * @return Sms |
||
471 | */ |
||
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 | * @param mixed $handler |
||
487 | * |
||
488 | * @return bool |
||
489 | */ |
||
490 | 3 | public static function queue($enable = null, $handler = null) |
|
506 | |||
507 | /** |
||
508 | * Set the recipient`s mobile number. |
||
509 | * |
||
510 | * @param string $mobile |
||
511 | * |
||
512 | * @return $this |
||
513 | */ |
||
514 | 6 | public function to($mobile) |
|
520 | |||
521 | /** |
||
522 | * Set the sms content. |
||
523 | * |
||
524 | * @param string $content |
||
525 | * |
||
526 | * @return $this |
||
527 | */ |
||
528 | 3 | public function content($content) |
|
534 | |||
535 | /** |
||
536 | * Set the template ids. |
||
537 | * |
||
538 | * @param mixed $name |
||
539 | * @param mixed $tempId |
||
540 | * |
||
541 | * @return $this |
||
542 | */ |
||
543 | 3 | public function template($name, $tempId = null) |
|
549 | |||
550 | /** |
||
551 | * Set the template data. |
||
552 | * |
||
553 | * @param mixed $name |
||
554 | * @param mixed $value |
||
555 | * |
||
556 | * @return $this |
||
557 | */ |
||
558 | 3 | public function data($name, $value = null) |
|
564 | |||
565 | /** |
||
566 | * Set the first agent. |
||
567 | * |
||
568 | * @param string $name |
||
569 | * |
||
570 | * @return $this |
||
571 | */ |
||
572 | 3 | 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 | * |
||
586 | * @param bool $immediately |
||
587 | * |
||
588 | * @return mixed |
||
589 | */ |
||
590 | 18 | public function send($immediately = false) |
|
604 | |||
605 | /** |
||
606 | * Push to the queue system. |
||
607 | * |
||
608 | * @throws \Exception | PhpSmsException |
||
609 | * |
||
610 | * @return mixed |
||
611 | */ |
||
612 | 3 | public function push() |
|
626 | |||
627 | /** |
||
628 | * Get all of the data. |
||
629 | * |
||
630 | * @param null|string $key |
||
631 | * |
||
632 | * @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 | */ |
||
650 | 36 | public function getData($key = null) |
|
654 | |||
655 | /** |
||
656 | * Define the static hook methods by overload static method. |
||
657 | * |
||
658 | * @param string $name |
||
659 | * @param array $args |
||
660 | * |
||
661 | * @throws PhpSmsException |
||
662 | */ |
||
663 | 9 | public static function __callStatic($name, $args) |
|
680 | |||
681 | /** |
||
682 | * Define the hook methods by overload method. |
||
683 | * |
||
684 | * @param string $name |
||
685 | * @param array $args |
||
686 | * |
||
687 | * @throws PhpSmsException |
||
688 | * @throws \Exception |
||
689 | */ |
||
690 | 3 | public function __call($name, $args) |
|
698 | |||
699 | /** |
||
700 | * Serialize magic method. |
||
701 | * |
||
702 | * @return array |
||
703 | */ |
||
704 | 3 | public function __sleep() |
|
716 | |||
717 | /** |
||
718 | * Deserialize magic method. |
||
719 | */ |
||
720 | 3 | public function __wakeup() |
|
731 | |||
732 | /** |
||
733 | * Get a closure serializer. |
||
734 | * |
||
735 | * @return Serializer |
||
736 | */ |
||
737 | 3 | protected static function getSerializer() |
|
745 | |||
746 | /** |
||
747 | * Serialize or deserialize the scheme. |
||
748 | * |
||
749 | * @param array $scheme |
||
750 | * |
||
751 | * @return array |
||
752 | */ |
||
753 | 3 | protected static function serializeOrDeserializeScheme(array $scheme) |
|
764 | |||
765 | /** |
||
766 | * Serialize the hooks` handlers. |
||
767 | * |
||
768 | * @return array |
||
769 | */ |
||
770 | 3 | protected static function serializeHandlers() |
|
782 | |||
783 | /** |
||
784 | * Reinstall hooks` handlers. |
||
785 | * |
||
786 | * @param array $handlers |
||
787 | */ |
||
788 | 3 | protected static function reinstallHandlers(array $handlers) |
|
800 | |||
801 | /** |
||
802 | * Serialize or deserialize the specified closure and then replace the original value. |
||
803 | * |
||
804 | * @param array $options |
||
805 | * @param int|string $key |
||
806 | */ |
||
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..