Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
11 | class Sms |
||
12 | { |
||
13 | /** |
||
14 | * sms send task name |
||
15 | */ |
||
16 | const TASK = 'PhpSms'; |
||
17 | |||
18 | /** |
||
19 | * agents instance |
||
20 | */ |
||
21 | protected static $agents = []; |
||
22 | |||
23 | /** |
||
24 | * agents`s name |
||
25 | * |
||
26 | * @var |
||
27 | */ |
||
28 | protected static $agentsName = []; |
||
29 | |||
30 | /** |
||
31 | * agents`s config |
||
32 | * |
||
33 | * @var |
||
34 | */ |
||
35 | protected static $agentsConfig = []; |
||
36 | |||
37 | /** |
||
38 | * whether to enable queue |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected static $enableQueue = false; |
||
43 | |||
44 | /** |
||
45 | * queue work |
||
46 | * |
||
47 | * @var \Closure |
||
48 | */ |
||
49 | protected static $howToUseQueue = null; |
||
50 | |||
51 | /** |
||
52 | * sms already pushed to queue |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $pushedToQueue = false; |
||
57 | |||
58 | /** |
||
59 | * hook handlers |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected static $enableHooks = [ |
||
64 | 'beforeRun', |
||
65 | 'beforeDriverRun', |
||
66 | 'afterDriverRun', |
||
67 | 'afterRun', |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * sms data |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $smsData = [ |
||
76 | 'to' => null, |
||
77 | 'templates' => [], |
||
78 | 'content' => '', |
||
79 | 'templateData' => [], |
||
80 | 'voiceCode' => null, |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * first agent for send sms/voice verify |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $firstAgent = null; |
||
89 | |||
90 | /** |
||
91 | * a instance of class 'SuperClosure\Serializer' |
||
92 | * @var null |
||
93 | */ |
||
94 | 9 | protected static $serializer = null; |
|
95 | |||
96 | 9 | /** |
|
97 | 9 | * store the static properties of Sms class when serialize a instance |
|
98 | 9 | * @var array |
|
99 | 9 | */ |
|
100 | protected $_status_before_enqueue_ = []; |
||
101 | |||
102 | /** |
||
103 | * construct |
||
104 | * |
||
105 | * @param bool $autoBoot |
||
106 | */ |
||
107 | public function __construct($autoBoot = true) |
||
113 | |||
114 | 3 | /** |
|
115 | * create sms instance and set templates |
||
116 | * |
||
117 | * @param mixed $agentName |
||
118 | * @param mixed $tempId |
||
119 | * |
||
120 | * @return Sms |
||
121 | */ |
||
122 | 3 | public static function make($agentName = null, $tempId = null) |
|
137 | 3 | ||
138 | /** |
||
139 | * send voice verify |
||
140 | * |
||
141 | * @param $code |
||
142 | * |
||
143 | * @return Sms |
||
144 | */ |
||
145 | public static function voice($code) |
||
152 | |||
153 | 3 | /** |
|
154 | 3 | * set how to use queue. |
|
155 | 3 | * |
|
156 | 3 | * @param $enable |
|
157 | 3 | * @param $handler |
|
158 | 3 | * |
|
159 | 3 | * @return bool |
|
160 | 3 | */ |
|
161 | public static function queue($enable = null, $handler = null) |
||
177 | |||
178 | /** |
||
179 | * set the mobile number |
||
180 | * |
||
181 | * @param $mobile |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function to($mobile) |
||
191 | |||
192 | /** |
||
193 | * set content for content sms |
||
194 | * |
||
195 | * @param $content |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function content($content) |
||
205 | 3 | ||
206 | 3 | /** |
|
207 | 3 | * set template id for template sms |
|
208 | 3 | * |
|
209 | * @param $agentName |
||
210 | * @param $tempId |
||
211 | 3 | * |
|
212 | 3 | * @return $this |
|
213 | */ |
||
214 | 3 | public function template($agentName, $tempId = null) |
|
229 | |||
230 | /** |
||
231 | * set data for template sms |
||
232 | * |
||
233 | * @param array $data |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function data(array $data) |
||
243 | |||
244 | /** |
||
245 | * set the first agent |
||
246 | * |
||
247 | * @param $name |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function agent($name) |
||
257 | |||
258 | 15 | /** |
|
259 | 12 | * start send |
|
260 | 12 | * |
|
261 | * @param bool $immediately |
||
262 | * |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function send($immediately = false) |
||
297 | |||
298 | 3 | /** |
|
299 | * push sms send task to queue |
||
300 | * |
||
301 | * @throws \Exception | PhpSmsException |
||
302 | * |
||
303 | * @return mixed |
||
304 | */ |
||
305 | protected function push() |
||
320 | |||
321 | 9 | /** |
|
322 | * get sms data |
||
323 | 9 | * |
|
324 | 9 | * @return array |
|
325 | 3 | */ |
|
326 | 3 | public function getData() |
|
330 | |||
331 | /** |
||
332 | * bootstrap |
||
333 | * |
||
334 | * @param bool $force |
||
335 | 18 | */ |
|
336 | public static function bootstrap($force = false) |
||
347 | 3 | ||
348 | /** |
||
349 | 3 | * generator a sms send task |
|
350 | 3 | * |
|
351 | 3 | * @return object |
|
352 | 3 | */ |
|
353 | 3 | public static function generatorTask() |
|
361 | |||
362 | 3 | /** |
|
363 | 3 | * configuration |
|
364 | 3 | */ |
|
365 | 3 | protected static function configuration() |
|
372 | |||
373 | /** |
||
374 | 3 | * generate enabled agents name |
|
375 | * |
||
376 | 3 | * @param array $config |
|
377 | 3 | */ |
|
378 | 3 | protected static function generatorAgentsName(&$config) |
|
386 | 3 | ||
387 | /** |
||
388 | * generator agents config |
||
389 | * |
||
390 | * @param array $config |
||
391 | */ |
||
392 | protected static function generatorAgentsConfig(&$config) |
||
405 | 15 | ||
406 | /** |
||
407 | 3 | * config value validator |
|
408 | * |
||
409 | 3 | * @throws PhpSmsException |
|
410 | */ |
||
411 | 3 | protected static function configValidator() |
|
417 | 3 | ||
418 | 15 | /** |
|
419 | 15 | * create drivers for sms send task |
|
420 | 15 | * |
|
421 | 15 | * @param $task |
|
422 | 15 | */ |
|
423 | 15 | protected static function createDrivers($task) |
|
457 | |||
458 | /** |
||
459 | * 解析可用代理器的数组模式的调度配置 |
||
460 | * |
||
461 | * @param array $options |
||
462 | * |
||
463 | * @return array |
||
464 | */ |
||
465 | protected static function parseAgentArrayOptions(array $options) |
||
475 | |||
476 | /** |
||
477 | * 从调度配置中拉取指定数据 |
||
478 | * |
||
479 | * @param array $options |
||
480 | * @param string $name |
||
481 | * |
||
482 | * @return null|string |
||
483 | */ |
||
484 | 3 | protected static function pullAgentOptionByName(array &$options, $name) |
|
494 | |||
495 | /** |
||
496 | * get agent config data by name |
||
497 | * |
||
498 | * @param $name |
||
499 | * |
||
500 | * @return array |
||
501 | 18 | */ |
|
502 | protected static function getAgentConfigData($name) |
||
507 | 3 | ||
508 | /** |
||
509 | * get a sms agent instance, |
||
510 | * if null, will create a new agent instance |
||
511 | 3 | * |
|
512 | * @param $name |
||
513 | 3 | * @param array $configData |
|
514 | 3 | * |
|
515 | * @throws PhpSmsException |
||
516 | * |
||
517 | * @return mixed |
||
518 | 3 | */ |
|
519 | public static function getSmsAgent($name, array $configData) |
||
540 | |||
541 | /** |
||
542 | * validate |
||
543 | 6 | * |
|
544 | * @throws PhpSmsException |
||
545 | 6 | */ |
|
546 | 6 | protected function validator() |
|
554 | 3 | ||
555 | 3 | /** |
|
556 | 6 | * set enable agents |
|
557 | * |
||
558 | * @param $agentName |
||
559 | * @param null $options |
||
560 | */ |
||
561 | public static function enable($agentName, $options = null) |
||
575 | |||
576 | 6 | /** |
|
577 | 6 | * set config for available agents |
|
578 | 6 | * |
|
579 | * @param $agentName |
||
580 | * @param array $config |
||
581 | * |
||
582 | * @throws PhpSmsException |
||
583 | */ |
||
584 | public static function agents($agentName, array $config = []) |
||
597 | 9 | ||
598 | /** |
||
599 | * get enable agents |
||
600 | * |
||
601 | * @return array |
||
602 | */ |
||
603 | 3 | public static function getEnableAgents() |
|
607 | |||
608 | /** |
||
609 | * get agents config info |
||
610 | * |
||
611 | 3 | * @return array |
|
612 | */ |
||
613 | 3 | public static function getAgentsConfig() |
|
617 | |||
618 | /** |
||
619 | * tear down enable agents |
||
620 | */ |
||
621 | public static function cleanEnableAgents() |
||
625 | |||
626 | 6 | /** |
|
627 | 6 | * tear down agents config |
|
628 | 6 | */ |
|
629 | 6 | public static function cleanAgentsConfig() |
|
633 | 6 | ||
634 | 6 | /** |
|
635 | 6 | * overload static method |
|
636 | 6 | * |
|
637 | * @param $name |
||
638 | * @param $args |
||
639 | 6 | * |
|
640 | * @throws PhpSmsException |
||
641 | */ |
||
642 | 6 | public static function __callStatic($name, $args) |
|
661 | |||
662 | /** |
||
663 | * overload method |
||
664 | * |
||
665 | * @param $name |
||
666 | * @param $args |
||
667 | * |
||
668 | * @throws PhpSmsException |
||
669 | * @throws \Exception |
||
670 | */ |
||
671 | public function __call($name, $args) |
||
679 | |||
680 | /** |
||
681 | * serialize magic method |
||
682 | * store current sms instance status |
||
683 | * |
||
684 | * @return array |
||
685 | */ |
||
686 | public function __sleep() |
||
694 | |||
695 | /** |
||
696 | * unserialize magic method |
||
697 | * note: the force bootstrap must before reinstall handlers! |
||
698 | */ |
||
699 | public function __wakeup() |
||
707 | |||
708 | /** |
||
709 | * get a serializer |
||
710 | * |
||
711 | * @return Serializer |
||
712 | */ |
||
713 | public static function getSerializer() |
||
721 | |||
722 | /** |
||
723 | * serialize enable agents |
||
724 | * |
||
725 | * @return array |
||
726 | */ |
||
727 | protected static function serializeEnableAgents() |
||
744 | |||
745 | /** |
||
746 | * unserialize enable agents |
||
747 | * |
||
748 | * @param array $serialized |
||
749 | * |
||
750 | * @return mixed |
||
751 | */ |
||
752 | protected static function unserializeEnableAgents(array $serialized) |
||
768 | |||
769 | /** |
||
770 | * serialize these hooks` handlers: |
||
771 | * 'beforeRun','beforeDriverRun','afterDriverRun','afterRun' |
||
772 | * |
||
773 | * @return array |
||
774 | */ |
||
775 | protected static function serializeHandlers() |
||
792 | |||
793 | /** |
||
794 | * reinstall hooks` handlers by serialized handlers |
||
795 | * |
||
796 | * @param array $handlers |
||
797 | */ |
||
798 | protected static function reinstallHandlers(array $handlers) |
||
811 | } |
||
812 |
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..