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 | * |
||
| 93 | * @var Serializer |
||
| 94 | 9 | */ |
|
| 95 | protected static $serializer = null; |
||
| 96 | 9 | ||
| 97 | 9 | /** |
|
| 98 | 9 | * store the static properties of Sms class when serialize a instance |
|
| 99 | 9 | * |
|
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $_status_before_enqueue_ = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * construct |
||
| 106 | * |
||
| 107 | * @param bool $autoBoot |
||
| 108 | */ |
||
| 109 | 3 | public function __construct($autoBoot = true) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * create sms instance and set templates |
||
| 118 | * |
||
| 119 | * @param mixed $agentName |
||
| 120 | * @param mixed $tempId |
||
| 121 | * |
||
| 122 | 3 | * @return Sms |
|
| 123 | */ |
||
| 124 | public static function make($agentName = null, $tempId = null) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * send voice verify |
||
| 142 | * |
||
| 143 | * @param $code |
||
| 144 | * |
||
| 145 | * @return Sms |
||
| 146 | */ |
||
| 147 | public static function voice($code) |
||
| 154 | 3 | ||
| 155 | 3 | /** |
|
| 156 | 3 | * set how to use queue. |
|
| 157 | 3 | * |
|
| 158 | 3 | * @param $enable |
|
| 159 | 3 | * @param $handler |
|
| 160 | 3 | * |
|
| 161 | * @return bool |
||
| 162 | 3 | */ |
|
| 163 | public static function queue($enable = null, $handler = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * set the mobile number |
||
| 182 | * |
||
| 183 | * @param $mobile |
||
| 184 | * |
||
| 185 | * @return $this |
||
| 186 | 3 | */ |
|
| 187 | 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 | 3 | ||
| 208 | 3 | /** |
|
| 209 | * set template id for template sms |
||
| 210 | * |
||
| 211 | 3 | * @param $agentName |
|
| 212 | 3 | * @param $tempId |
|
| 213 | * |
||
| 214 | 3 | * @return $this |
|
| 215 | */ |
||
| 216 | public function template($agentName, $tempId = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * set data for template sms |
||
| 234 | * |
||
| 235 | * @param array $data |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | 3 | */ |
|
| 239 | public function data(array $data) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * set the first agent |
||
| 248 | * |
||
| 249 | * @param $name |
||
| 250 | * |
||
| 251 | * @return $this |
||
| 252 | 15 | */ |
|
| 253 | public function agent($name) |
||
| 259 | 12 | ||
| 260 | 12 | /** |
|
| 261 | * start send |
||
| 262 | * |
||
| 263 | * @param bool $immediately |
||
| 264 | * |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | 15 | public function send($immediately = false) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * push sms send task to queue |
||
| 302 | * |
||
| 303 | * @throws \Exception | PhpSmsException |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | protected function push() |
||
| 322 | |||
| 323 | 9 | /** |
|
| 324 | 9 | * get sms data |
|
| 325 | 3 | * |
|
| 326 | 3 | * @return array |
|
| 327 | 3 | */ |
|
| 328 | 9 | public function getData() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * bootstrap |
||
| 335 | 18 | * |
|
| 336 | * @param bool $force |
||
| 337 | 18 | */ |
|
| 338 | 3 | public static function bootstrap($force = false) |
|
| 349 | 3 | ||
| 350 | 3 | /** |
|
| 351 | 3 | * generator a sms send task |
|
| 352 | 3 | * |
|
| 353 | 3 | * @return object |
|
| 354 | */ |
||
| 355 | public static function generatorTask() |
||
| 363 | 3 | ||
| 364 | 3 | /** |
|
| 365 | 3 | * configuration |
|
| 366 | 3 | */ |
|
| 367 | 3 | protected static function configuration() |
|
| 374 | 3 | ||
| 375 | /** |
||
| 376 | 3 | * generate enabled agents name |
|
| 377 | 3 | * |
|
| 378 | 3 | * @param array $config |
|
| 379 | 3 | */ |
|
| 380 | 3 | protected static function generatorAgentsName(&$config) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * generator agents config |
||
| 391 | * |
||
| 392 | * @param array $config |
||
| 393 | 3 | */ |
|
| 394 | protected static function generatorAgentsConfig(&$config) |
||
| 407 | 3 | ||
| 408 | /** |
||
| 409 | 3 | * config value validator |
|
| 410 | * |
||
| 411 | 3 | * @throws PhpSmsException |
|
| 412 | */ |
||
| 413 | protected static function configValidator() |
||
| 419 | 15 | ||
| 420 | 15 | /** |
|
| 421 | 15 | * create drivers for sms send task |
|
| 422 | 15 | * |
|
| 423 | 15 | * @param $task |
|
| 424 | */ |
||
| 425 | protected static function createDrivers($task) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * 解析可用代理器的数组模式的调度配置 |
||
| 462 | * |
||
| 463 | * @param array $options |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | protected static function parseAgentArrayOptions(array $options) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * 从调度配置中拉取指定数据 |
||
| 480 | * |
||
| 481 | * @param array $options |
||
| 482 | * @param string $name |
||
| 483 | * |
||
| 484 | 3 | * @return null|string |
|
| 485 | */ |
||
| 486 | 3 | protected static function pullAgentOptionByName(array &$options, $name) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * get agent config data by name |
||
| 499 | * |
||
| 500 | * @param $name |
||
| 501 | 18 | * |
|
| 502 | * @return array |
||
| 503 | 18 | */ |
|
| 504 | 3 | protected static function getAgentConfigData($name) |
|
| 509 | |||
| 510 | /** |
||
| 511 | 3 | * get a sms agent instance, |
|
| 512 | * if null, will create a new agent instance |
||
| 513 | 3 | * |
|
| 514 | 3 | * @param $name |
|
| 515 | * @param array $configData |
||
| 516 | * |
||
| 517 | * @throws PhpSmsException |
||
| 518 | 3 | * |
|
| 519 | * @return mixed |
||
| 520 | 18 | */ |
|
| 521 | public static function getSmsAgent($name, array $configData) |
||
| 542 | |||
| 543 | 6 | /** |
|
| 544 | * validate |
||
| 545 | 6 | * |
|
| 546 | 6 | * @throws PhpSmsException |
|
| 547 | 6 | */ |
|
| 548 | 6 | protected function validator() |
|
| 556 | 6 | ||
| 557 | /** |
||
| 558 | * set enable agents |
||
| 559 | * |
||
| 560 | * @param $agentName |
||
| 561 | * @param null $options |
||
| 562 | */ |
||
| 563 | public static function enable($agentName, $options = null) |
||
| 577 | 6 | ||
| 578 | 6 | /** |
|
| 579 | * set config for available agents |
||
| 580 | * |
||
| 581 | * @param $agentName |
||
| 582 | * @param array $config |
||
| 583 | * |
||
| 584 | * @throws PhpSmsException |
||
| 585 | 9 | */ |
|
| 586 | public static function agents($agentName, array $config = []) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * get enable agents |
||
| 602 | * |
||
| 603 | 3 | * @return array |
|
| 604 | */ |
||
| 605 | 3 | public static function getEnableAgents() |
|
| 609 | |||
| 610 | /** |
||
| 611 | 3 | * get agents config info |
|
| 612 | * |
||
| 613 | 3 | * @return array |
|
| 614 | 3 | */ |
|
| 615 | public static function getAgentsConfig() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * tear down enable agents |
||
| 622 | */ |
||
| 623 | public static function cleanEnableAgents() |
||
| 627 | 6 | ||
| 628 | 6 | /** |
|
| 629 | 6 | * tear down agents config |
|
| 630 | 6 | */ |
|
| 631 | 6 | public static function cleanAgentsConfig() |
|
| 635 | 6 | ||
| 636 | 6 | /** |
|
| 637 | * overload static method |
||
| 638 | * |
||
| 639 | 6 | * @param $name |
|
| 640 | * @param $args |
||
| 641 | * |
||
| 642 | 6 | * @throws PhpSmsException |
|
| 643 | */ |
||
| 644 | public static function __callStatic($name, $args) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * overload method |
||
| 666 | * |
||
| 667 | * @param $name |
||
| 668 | * @param $args |
||
| 669 | * |
||
| 670 | * @throws PhpSmsException |
||
| 671 | * @throws \Exception |
||
| 672 | */ |
||
| 673 | public function __call($name, $args) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * serialize magic method |
||
| 684 | * store current sms instance status |
||
| 685 | * |
||
| 686 | * @return array |
||
| 687 | */ |
||
| 688 | public function __sleep() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * unserialize magic method |
||
| 699 | * note: the force bootstrap must before reinstall handlers! |
||
| 700 | */ |
||
| 701 | public function __wakeup() |
||
| 709 | |||
| 710 | /** |
||
| 711 | * get a serializer |
||
| 712 | * |
||
| 713 | * @return Serializer |
||
| 714 | */ |
||
| 715 | public static function getSerializer() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * serialize enable agents |
||
| 726 | * |
||
| 727 | * @return array |
||
| 728 | */ |
||
| 729 | protected static function serializeEnableAgents() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * unserialize enable agents |
||
| 744 | * |
||
| 745 | * @param array $serialized |
||
| 746 | * |
||
| 747 | * @return mixed |
||
| 748 | */ |
||
| 749 | protected static function unserializeEnableAgents(array $serialized) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * serialize character closure value of a array and replace origin value |
||
| 763 | * |
||
| 764 | * @param array $options |
||
| 765 | * @param $key |
||
| 766 | */ |
||
| 767 | protected static function serializeClosureAndReplace(array &$options, $key) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * unserialize character string of a array to closure and replace origin value |
||
| 777 | * |
||
| 778 | * @param array $options |
||
| 779 | * @param $key |
||
| 780 | */ |
||
| 781 | protected static function unserializeToClosureAndReplace(array &$options, $key) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * serialize these hooks` handlers: |
||
| 791 | * 'beforeRun','beforeDriverRun','afterDriverRun','afterRun' |
||
| 792 | * |
||
| 793 | * @return array |
||
| 794 | */ |
||
| 795 | protected static function serializeHandlers() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * reinstall hooks` handlers by serialized handlers |
||
| 815 | * |
||
| 816 | * @param array $handlers |
||
| 817 | */ |
||
| 818 | protected static function reinstallHandlers(array $handlers) |
||
| 831 | } |
||
| 832 |
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..