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 |
||
10 | class Sms |
||
11 | { |
||
12 | /** |
||
13 | * sms send task name |
||
14 | */ |
||
15 | const TASK = 'PhpSms'; |
||
16 | |||
17 | /** |
||
18 | * agents instance |
||
19 | */ |
||
20 | protected static $agents; |
||
21 | |||
22 | /** |
||
23 | * agents`s name |
||
24 | * |
||
25 | * @var |
||
26 | */ |
||
27 | protected static $agentsName = []; |
||
28 | |||
29 | /** |
||
30 | * agents`s config |
||
31 | * |
||
32 | * @var |
||
33 | */ |
||
34 | protected static $agentsConfig = []; |
||
35 | |||
36 | /** |
||
37 | * whether to enable queue |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected static $enableQueue = false; |
||
42 | |||
43 | /** |
||
44 | * queue work |
||
45 | * |
||
46 | * @var \Closure |
||
47 | */ |
||
48 | protected static $howToUseQueue = null; |
||
49 | |||
50 | /** |
||
51 | * sms already pushed to queue |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $pushedToQueue = false; |
||
56 | |||
57 | /** |
||
58 | * hook handlers |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected static $enableHooks = [ |
||
63 | 'beforeRun', |
||
64 | 'beforeDriverRun', |
||
65 | 'afterDriverRun', |
||
66 | 'afterRun', |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * sms data |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $smsData = [ |
||
75 | 'to' => null, |
||
76 | 'templates' => [], |
||
77 | 'content' => '', |
||
78 | 'templateData' => [], |
||
79 | 'voiceCode' => null, |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * first agent for send sms/voice verify |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $firstAgent = null; |
||
88 | |||
89 | /** |
||
90 | * construct |
||
91 | * |
||
92 | * @param bool $autoBoot |
||
93 | */ |
||
94 | 9 | public function __construct($autoBoot = true) |
|
100 | |||
101 | /** |
||
102 | * create sms instance and set templates |
||
103 | * |
||
104 | * @param mixed $agentName |
||
105 | * @param mixed $tempId |
||
106 | * |
||
107 | * @return Sms |
||
108 | */ |
||
109 | 3 | public static function make($agentName = null, $tempId = null) |
|
124 | |||
125 | /** |
||
126 | * send voice verify |
||
127 | * |
||
128 | * @param $code |
||
129 | * |
||
130 | * @return Sms |
||
131 | */ |
||
132 | 3 | public static function voice($code) |
|
139 | |||
140 | /** |
||
141 | * set how to use queue. |
||
142 | * |
||
143 | * @param $enable |
||
144 | * @param $handler |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 3 | public static function queue($enable = null, $handler = null) |
|
164 | |||
165 | /** |
||
166 | * set the mobile number |
||
167 | * |
||
168 | * @param $mobile |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | 6 | public function to($mobile) |
|
178 | |||
179 | /** |
||
180 | * set content for content sms |
||
181 | * |
||
182 | * @param $content |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | 3 | public function content($content) |
|
192 | |||
193 | /** |
||
194 | * set template id for template sms |
||
195 | * |
||
196 | * @param $agentName |
||
197 | * @param $tempId |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | 3 | public function template($agentName, $tempId = null) |
|
216 | |||
217 | /** |
||
218 | * set data for template sms |
||
219 | * |
||
220 | * @param array $data |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | 11 | public function data(array $data) |
|
230 | |||
231 | /** |
||
232 | * set the first agent |
||
233 | * |
||
234 | * @param $name |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | 3 | public function agent($name) |
|
244 | |||
245 | /** |
||
246 | * start send |
||
247 | * |
||
248 | * @param bool $immediately |
||
249 | * |
||
250 | * @return mixed |
||
251 | */ |
||
252 | 15 | public function send($immediately = false) |
|
284 | |||
285 | /** |
||
286 | * push sms send task to queue |
||
287 | * |
||
288 | * @throws \Exception | PhpSmsException |
||
289 | * |
||
290 | * @return mixed |
||
291 | */ |
||
292 | 3 | protected function push() |
|
307 | |||
308 | /** |
||
309 | * get sms data |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | 33 | public function getData() |
|
317 | |||
318 | /** |
||
319 | * bootstrap |
||
320 | */ |
||
321 | 9 | public static function bootstrap() |
|
329 | |||
330 | /** |
||
331 | * generator a sms send task |
||
332 | * |
||
333 | * @return object |
||
334 | */ |
||
335 | 18 | public static function generatorTask() |
|
343 | |||
344 | /** |
||
345 | * configuration |
||
346 | */ |
||
347 | 3 | protected static function configuration() |
|
354 | |||
355 | /** |
||
356 | * generate enabled agents name |
||
357 | * |
||
358 | * @param array $config |
||
359 | */ |
||
360 | 3 | protected static function generatorAgentsName(&$config) |
|
368 | |||
369 | /** |
||
370 | * generator agents config |
||
371 | * |
||
372 | * @param array $config |
||
373 | */ |
||
374 | 3 | protected static function generatorAgentsConfig(&$config) |
|
387 | |||
388 | /** |
||
389 | * config value validator |
||
390 | * |
||
391 | * @throws PhpSmsException |
||
392 | */ |
||
393 | 3 | protected static function configValidator() |
|
399 | |||
400 | /** |
||
401 | * create drivers for sms send task |
||
402 | * |
||
403 | * @param $task |
||
404 | */ |
||
405 | 15 | protected static function createDrivers($task) |
|
439 | |||
440 | /** |
||
441 | * 解析可用代理器的数组模式的调度配置 |
||
442 | * |
||
443 | * @param array $options |
||
444 | * |
||
445 | * @return array |
||
446 | */ |
||
447 | protected static function parseAgentArrayOptions(array $options) |
||
457 | |||
458 | /** |
||
459 | * 从调度配置中拉取指定数据 |
||
460 | * |
||
461 | * @param array $options |
||
462 | * @param string $name |
||
463 | * |
||
464 | * @return null|string |
||
465 | */ |
||
466 | protected static function pullAgentOptionByName(array &$options, $name) |
||
476 | |||
477 | /** |
||
478 | * get agent config data by name |
||
479 | * |
||
480 | * @param $name |
||
481 | * |
||
482 | * @return array |
||
483 | */ |
||
484 | 3 | protected static function getAgentConfigData($name) |
|
489 | |||
490 | /** |
||
491 | * get a sms agent instance, |
||
492 | * if null, will create a new agent instance |
||
493 | * |
||
494 | * @param $name |
||
495 | * @param array $configData |
||
496 | * |
||
497 | * @throws PhpSmsException |
||
498 | * |
||
499 | * @return mixed |
||
500 | */ |
||
501 | 18 | public static function getSmsAgent($name, array $configData) |
|
522 | |||
523 | /** |
||
524 | * validate |
||
525 | * |
||
526 | * @throws PhpSmsException |
||
527 | */ |
||
528 | 18 | protected function validator() |
|
536 | |||
537 | /** |
||
538 | * set enable agents |
||
539 | * |
||
540 | * @param $agentName |
||
541 | * @param null $options |
||
542 | */ |
||
543 | 6 | public static function enable($agentName, $options = null) |
|
557 | |||
558 | /** |
||
559 | * set config for available agents |
||
560 | * |
||
561 | * @param $agentName |
||
562 | * @param array $config |
||
563 | * |
||
564 | * @throws PhpSmsException |
||
565 | */ |
||
566 | 6 | public static function agents($agentName, array $config = []) |
|
579 | |||
580 | /** |
||
581 | * get enable agents |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | 9 | public static function getEnableAgents() |
|
589 | |||
590 | /** |
||
591 | * get agents config info |
||
592 | * |
||
593 | * @return array |
||
594 | */ |
||
595 | 9 | public static function getAgentsConfig() |
|
599 | |||
600 | /** |
||
601 | * tear down enable agents |
||
602 | */ |
||
603 | 3 | public static function cleanEnableAgents() |
|
607 | |||
608 | /** |
||
609 | * tear down agents config |
||
610 | */ |
||
611 | 3 | public static function cleanAgentsConfig() |
|
615 | |||
616 | /** |
||
617 | * overload static method |
||
618 | * |
||
619 | * @param $name |
||
620 | * @param $args |
||
621 | * |
||
622 | * @throws PhpSmsException |
||
623 | */ |
||
624 | 6 | public static function __callStatic($name, $args) |
|
643 | |||
644 | /** |
||
645 | * overload method |
||
646 | * |
||
647 | * @param $name |
||
648 | * @param $args |
||
649 | * |
||
650 | * @throws PhpSmsException |
||
651 | * @throws \Exception |
||
652 | */ |
||
653 | 3 | public function __call($name, $args) |
|
661 | } |
||
662 |
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..