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 null |
||
| 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 | 9 | * @param bool $autoBoot |
|
| 93 | */ |
||
| 94 | 9 | public function __construct($autoBoot = true) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * create sms instance and set templates |
||
| 103 | * |
||
| 104 | * @param null $agentName |
||
| 105 | 3 | * @param null $tempId |
|
| 106 | * |
||
| 107 | 3 | * @return Sms |
|
| 108 | 3 | */ |
|
| 109 | public static function make($agentName = null, $tempId = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * send voice verify |
||
| 127 | * |
||
| 128 | 3 | * @param $code |
|
| 129 | * |
||
| 130 | 3 | * @return Sms |
|
| 131 | 3 | */ |
|
| 132 | public static function voice($code) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * set how to use queue. |
||
| 142 | * |
||
| 143 | * @param $enable |
||
| 144 | 3 | * @param $handler |
|
| 145 | * |
||
| 146 | 3 | * @return bool |
|
| 147 | 3 | */ |
|
| 148 | public static function queue($enable = null, $handler = null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * set the mobile number |
||
| 167 | * |
||
| 168 | 6 | * @param $mobile |
|
| 169 | * |
||
| 170 | 6 | * @return $this |
|
| 171 | */ |
||
| 172 | 6 | public function to($mobile) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * set content for content sms |
||
| 181 | * |
||
| 182 | 3 | * @param $content |
|
| 183 | * |
||
| 184 | 3 | * @return $this |
|
| 185 | */ |
||
| 186 | 3 | public function content($content) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * set template id for template sms |
||
| 195 | * |
||
| 196 | * @param $agentName |
||
| 197 | 3 | * @param $tempId |
|
| 198 | * |
||
| 199 | 3 | * @return $this |
|
| 200 | 3 | */ |
|
| 201 | 3 | public function template($agentName, $tempId = null) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * set data for template sms |
||
| 219 | * |
||
| 220 | 11 | * @param array $data |
|
| 221 | * |
||
| 222 | 3 | * @return $this |
|
| 223 | */ |
||
| 224 | 3 | public function data(array $data) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * set the first agent |
||
| 233 | * |
||
| 234 | 3 | * @param $name |
|
| 235 | * |
||
| 236 | 3 | * @return $this |
|
| 237 | */ |
||
| 238 | 3 | public function agent($name) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * start send |
||
| 247 | * |
||
| 248 | 15 | * @param bool $immediately |
|
| 249 | * |
||
| 250 | 15 | * @return mixed |
|
| 251 | */ |
||
| 252 | public function send($immediately = false) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * push sms send task to queue |
||
| 284 | * |
||
| 285 | 3 | * @throws \Exception | PhpSmsException |
|
| 286 | * |
||
| 287 | 3 | * @return mixed |
|
| 288 | */ |
||
| 289 | 3 | protected function push() |
|
| 304 | |||
| 305 | /** |
||
| 306 | 33 | * get sms data |
|
| 307 | * |
||
| 308 | 33 | * @return array |
|
| 309 | */ |
||
| 310 | public function getData() |
||
| 314 | 9 | ||
| 315 | /** |
||
| 316 | 9 | * bootstrap |
|
| 317 | 9 | */ |
|
| 318 | 9 | public static function bootstrap() |
|
| 326 | 18 | ||
| 327 | /** |
||
| 328 | 18 | * generator a sms send task |
|
| 329 | 3 | * |
|
| 330 | 2 | * @return object |
|
| 331 | */ |
||
| 332 | 18 | public static function generatorTask() |
|
| 340 | 9 | ||
| 341 | 9 | /** |
|
| 342 | 9 | * configuration |
|
| 343 | 9 | */ |
|
| 344 | 9 | protected static function configuration() |
|
| 351 | 9 | ||
| 352 | /** |
||
| 353 | 9 | * generate enabled agents name |
|
| 354 | 3 | * |
|
| 355 | 3 | * @param array $config |
|
| 356 | 3 | */ |
|
| 357 | 2 | protected static function generatorAgentsName(&$config) |
|
| 365 | 9 | ||
| 366 | /** |
||
| 367 | 9 | * generator agents config |
|
| 368 | 9 | * |
|
| 369 | 9 | * @param array $config |
|
| 370 | 3 | */ |
|
| 371 | 3 | protected static function generatorAgentsConfig(&$config) |
|
| 384 | 9 | ||
| 385 | /** |
||
| 386 | 9 | * config value validator |
|
| 387 | * |
||
| 388 | * @throws PhpSmsException |
||
| 389 | 9 | */ |
|
| 390 | protected static function configValidator() |
||
| 396 | 15 | ||
| 397 | /** |
||
| 398 | 9 | * create drivers for sms send task |
|
| 399 | * |
||
| 400 | 9 | * @param $task |
|
| 401 | */ |
||
| 402 | 9 | protected static function createDrivers($task) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * 解析可用代理器的数组模式的调度配置 |
||
| 439 | * |
||
| 440 | * @param array $options |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | protected static function parseAgentArrayOptions(array $options) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * 从调度配置中拉取指定数据 |
||
| 457 | * |
||
| 458 | * @param array $options |
||
| 459 | * @param string $name |
||
| 460 | * |
||
| 461 | * @return null|string |
||
| 462 | */ |
||
| 463 | protected static function pullAgentOptionByName(array &$options, $name) |
||
| 473 | |||
| 474 | /** |
||
| 475 | 9 | * get agent config data by name |
|
| 476 | * |
||
| 477 | 9 | * @param $name |
|
| 478 | 9 | * |
|
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | protected static function getAgentConfigData($name) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * get a sms agent instance, |
||
| 489 | * if null, will create a new agent instance |
||
| 490 | * |
||
| 491 | * @param $name |
||
| 492 | 18 | * @param array $configData |
|
| 493 | * |
||
| 494 | 18 | * @throws PhpSmsException |
|
| 495 | 3 | * |
|
| 496 | 3 | * @return mixed |
|
| 497 | 3 | */ |
|
| 498 | 3 | public static function getSmsAgent($name, array $configData) |
|
| 518 | 18 | ||
| 519 | /** |
||
| 520 | 18 | * validate |
|
| 521 | * |
||
| 522 | * @throws PhpSmsException |
||
| 523 | */ |
||
| 524 | 18 | protected function validator() |
|
| 532 | |||
| 533 | 6 | /** |
|
| 534 | * set enable agents |
||
| 535 | 6 | * |
|
| 536 | 6 | * @param $agentName |
|
| 537 | 6 | * @param null $options |
|
| 538 | 4 | */ |
|
| 539 | 6 | public static function enable($agentName, $options = null) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * set config for available agents |
||
| 556 | 6 | * |
|
| 557 | * @param $agentName |
||
| 558 | 6 | * @param array $config |
|
| 559 | 3 | * |
|
| 560 | 3 | * @throws PhpSmsException |
|
| 561 | 2 | */ |
|
| 562 | 6 | public static function agents($agentName, array $config = []) |
|
| 575 | 9 | ||
| 576 | /** |
||
| 577 | 9 | * get enable agents |
|
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | public static function getEnableAgents() |
||
| 585 | 9 | ||
| 586 | /** |
||
| 587 | 9 | * get agents config info |
|
| 588 | * |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | public static function getAgentsConfig() |
||
| 595 | 3 | ||
| 596 | 3 | /** |
|
| 597 | * tear down enable agents |
||
| 598 | */ |
||
| 599 | public static function cleanEnableAgents() |
||
| 603 | 3 | ||
| 604 | 3 | /** |
|
| 605 | * tear down agents config |
||
| 606 | */ |
||
| 607 | public static function cleanAgentsConfig() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * overload static method |
||
| 614 | 6 | * |
|
| 615 | * @param $name |
||
| 616 | 6 | * @param $args |
|
| 617 | 6 | * |
|
| 618 | 6 | * @throws PhpSmsException |
|
| 619 | 6 | */ |
|
| 620 | 6 | public static function __callStatic($name, $args) |
|
| 639 | |||
| 640 | /** |
||
| 641 | * overload method |
||
| 642 | * |
||
| 643 | 3 | * @param $name |
|
| 644 | * @param $args |
||
| 645 | * |
||
| 646 | 3 | * @throws PhpSmsException |
|
| 647 | 2 | * @throws \Exception |
|
| 648 | */ |
||
| 649 | public function __call($name, $args) |
||
| 657 | } |
||
| 658 |
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..