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 | * enable agents` name |
||
| 25 | * |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | protected static $agentsName = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * agents` 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 | * |
||
| 93 | * @var Serializer |
||
| 94 | */ |
||
| 95 | protected static $serializer = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * store the static properties of Sms class when serialize a instance |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $_status_before_enqueue_ = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * construct |
||
| 106 | * |
||
| 107 | * @param bool $autoBoot |
||
| 108 | */ |
||
| 109 | 6 | public function __construct($autoBoot = true) |
|
| 110 | { |
||
| 111 | 6 | if ($autoBoot) { |
|
| 112 | 6 | self::bootstrap(); |
|
| 113 | 6 | } |
|
| 114 | 6 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * create sms instance and set templates |
||
| 118 | * |
||
| 119 | * @param mixed $agentName |
||
| 120 | * @param mixed $tempId |
||
| 121 | * |
||
| 122 | * @return Sms |
||
| 123 | */ |
||
| 124 | public static function make($agentName = null, $tempId = null) |
||
| 125 | { |
||
| 126 | $sms = new self(); |
||
| 127 | if (is_array($agentName)) { |
||
| 128 | $sms->template($agentName); |
||
| 129 | } elseif ($agentName && is_string($agentName)) { |
||
| 130 | if ($tempId === null) { |
||
| 131 | $sms->content($agentName); |
||
| 132 | } elseif (is_string("$tempId")) { |
||
| 133 | $sms->template($agentName, $tempId); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return $sms; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * send voice verify |
||
| 142 | * |
||
| 143 | * @param $code |
||
| 144 | * |
||
| 145 | * @return Sms |
||
| 146 | */ |
||
| 147 | 3 | public static function voice($code) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * set how to use queue. |
||
| 157 | * |
||
| 158 | * @param $enable |
||
| 159 | * @param $handler |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | 3 | public static function queue($enable = null, $handler = null) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * set the mobile number |
||
| 182 | * |
||
| 183 | * @param $mobile |
||
| 184 | * |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | 3 | public function to($mobile) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * set content for content sms |
||
| 196 | * |
||
| 197 | * @param $content |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | 3 | public function content($content) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * set template id for template sms |
||
| 210 | * |
||
| 211 | * @param $agentName |
||
| 212 | * @param $tempId |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 3 | public function template($agentName, $tempId = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * set data for template sms |
||
| 234 | * |
||
| 235 | * @param array $data |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | 3 | public function data(array $data) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * set the first agent |
||
| 248 | * |
||
| 249 | * @param $name |
||
| 250 | * |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 3 | public function agent($name) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * start send |
||
| 262 | * |
||
| 263 | * @param bool $immediately |
||
| 264 | * |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | 18 | public function send($immediately = false) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * push sms send task to queue |
||
| 300 | * |
||
| 301 | * @throws \Exception | PhpSmsException |
||
| 302 | * |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | 3 | public function push() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * get sms data |
||
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | 36 | public function getData() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * bootstrap |
||
| 333 | */ |
||
| 334 | 9 | public static function bootstrap() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * generator a sms send task |
||
| 345 | * |
||
| 346 | * @return object |
||
| 347 | */ |
||
| 348 | 18 | public static function generatorTask() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * configuration |
||
| 359 | */ |
||
| 360 | 6 | protected static function configuration() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * generate enabled agents name |
||
| 370 | * |
||
| 371 | * @param array $config |
||
| 372 | */ |
||
| 373 | 6 | protected static function generatorAgentsName(&$config) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * generator agents config |
||
| 384 | * |
||
| 385 | * @param array $config |
||
| 386 | */ |
||
| 387 | 6 | protected static function generatorAgentsConfig(&$config) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * config value validator |
||
| 403 | * |
||
| 404 | * @throws PhpSmsException |
||
| 405 | */ |
||
| 406 | 6 | protected static function configValidator() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * create drivers for sms send task |
||
| 415 | * |
||
| 416 | * @param $task |
||
| 417 | */ |
||
| 418 | 18 | protected static function createDrivers($task) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * 解析可用代理器的数组模式的调度配置 |
||
| 455 | * |
||
| 456 | * @param array $options |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 3 | protected static function parseAgentArrayOptions(array $options) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * 从调度配置中拉取指定数据 |
||
| 473 | * |
||
| 474 | * @param array $options |
||
| 475 | * @param string $name |
||
| 476 | * |
||
| 477 | * @return null|string |
||
| 478 | */ |
||
| 479 | 3 | protected static function pullAgentOptionByName(array &$options, $name) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * get agent config data by name |
||
| 492 | * |
||
| 493 | * @param $name |
||
| 494 | * |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | 6 | protected static function getAgentConfigData($name) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * get a sms agent instance, |
||
| 505 | * if null, will create a new agent instance |
||
| 506 | * |
||
| 507 | * @param $name |
||
| 508 | * @param array $configData |
||
| 509 | * |
||
| 510 | * @throws PhpSmsException |
||
| 511 | * |
||
| 512 | * @return mixed |
||
| 513 | */ |
||
| 514 | 21 | public static function getSmsAgent($name, array $configData) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * set enable agents |
||
| 538 | * |
||
| 539 | * @param $agentName |
||
| 540 | * @param null $options |
||
| 541 | */ |
||
| 542 | 6 | public static function enable($agentName, $options = null) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * set config for available agents |
||
| 559 | * |
||
| 560 | * @param $agentName |
||
| 561 | * @param array $config |
||
| 562 | * |
||
| 563 | * @throws PhpSmsException |
||
| 564 | */ |
||
| 565 | 9 | public static function agents($agentName, array $config = []) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * get enable agents |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | 12 | public static function getEnableAgents() |
|
| 588 | |||
| 589 | /** |
||
| 590 | * get agents config info |
||
| 591 | * |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | 12 | public static function getAgentsConfig() |
|
| 598 | |||
| 599 | /** |
||
| 600 | * tear down enable agents |
||
| 601 | */ |
||
| 602 | 6 | public static function cleanEnableAgents() |
|
| 606 | |||
| 607 | /** |
||
| 608 | * tear down agents config |
||
| 609 | */ |
||
| 610 | 3 | public static function cleanAgentsConfig() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * overload static method |
||
| 617 | * |
||
| 618 | * @param $name |
||
| 619 | * @param $args |
||
| 620 | * |
||
| 621 | * @throws PhpSmsException |
||
| 622 | */ |
||
| 623 | 9 | public static function __callStatic($name, $args) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * overload method |
||
| 645 | * |
||
| 646 | * @param $name |
||
| 647 | * @param $args |
||
| 648 | * |
||
| 649 | * @throws PhpSmsException |
||
| 650 | * @throws \Exception |
||
| 651 | */ |
||
| 652 | 3 | public function __call($name, $args) |
|
| 660 | |||
| 661 | /** |
||
| 662 | * serialize magic method |
||
| 663 | * store current sms instance status |
||
| 664 | * |
||
| 665 | * @return array |
||
| 666 | */ |
||
| 667 | 3 | public function __sleep() |
|
| 679 | |||
| 680 | /** |
||
| 681 | * unserialize magic method |
||
| 682 | * note: the force bootstrap must before reinstall handlers! |
||
| 683 | */ |
||
| 684 | 3 | public function __wakeup() |
|
| 696 | |||
| 697 | /** |
||
| 698 | * get a serializer |
||
| 699 | * |
||
| 700 | * @return Serializer |
||
| 701 | */ |
||
| 702 | 3 | public static function getSerializer() |
|
| 710 | |||
| 711 | /** |
||
| 712 | * serialize enable agents |
||
| 713 | * |
||
| 714 | * @return array |
||
| 715 | */ |
||
| 716 | 3 | protected static function serializeEnableAgents() |
|
| 728 | |||
| 729 | /** |
||
| 730 | * unserialize enable agents |
||
| 731 | * |
||
| 732 | * @param array $serialized |
||
| 733 | * |
||
| 734 | * @return mixed |
||
| 735 | */ |
||
| 736 | 3 | protected static function unserializeEnableAgents(array $serialized) |
|
| 747 | |||
| 748 | /** |
||
| 749 | * serialize character closure value of a array and replace origin value |
||
| 750 | * |
||
| 751 | * @param array $options |
||
| 752 | * @param $key |
||
| 753 | */ |
||
| 754 | 3 | protected static function serializeClosureAndReplace(array &$options, $key) |
|
| 761 | |||
| 762 | /** |
||
| 763 | * unserialize character string of a array to closure and replace origin value |
||
| 764 | * |
||
| 765 | * @param array $options |
||
| 766 | * @param $key |
||
| 767 | */ |
||
| 768 | 3 | protected static function unserializeToClosureAndReplace(array &$options, $key) |
|
| 775 | |||
| 776 | /** |
||
| 777 | * serialize these hooks` handlers: |
||
| 778 | * 'beforeRun','beforeDriverRun','afterDriverRun','afterRun' |
||
| 779 | * |
||
| 780 | * @return array |
||
| 781 | */ |
||
| 782 | 3 | protected static function serializeHandlers() |
|
| 799 | |||
| 800 | /** |
||
| 801 | * reinstall hooks` handlers by serialized handlers |
||
| 802 | * |
||
| 803 | * @param array $handlers |
||
| 804 | */ |
||
| 805 | 3 | protected static function reinstallHandlers(array $handlers) |
|
| 818 | } |
||
| 819 |
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..