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 | public function __construct() |
||
| 96 | |||
| 97 | 9 | /** |
|
| 98 | 9 | * create sms instance and set templates |
|
| 99 | * |
||
| 100 | * @param null $agentName |
||
| 101 | * @param null $tempId |
||
| 102 | * |
||
| 103 | * @return Sms |
||
| 104 | */ |
||
| 105 | public static function make($agentName = null, $tempId = null) |
||
| 120 | |||
| 121 | 3 | /** |
|
| 122 | * send voice verify |
||
| 123 | * |
||
| 124 | * @param $code |
||
| 125 | * |
||
| 126 | * @return Sms |
||
| 127 | */ |
||
| 128 | public static function voice($code) |
||
| 135 | |||
| 136 | 3 | /** |
|
| 137 | * set how to use queue. |
||
| 138 | * |
||
| 139 | * @param $enable |
||
| 140 | * @param $handler |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public static function queue($enable = null, $handler = null) |
||
| 160 | |||
| 161 | 3 | /** |
|
| 162 | * set the mobile number |
||
| 163 | * |
||
| 164 | * @param $mobile |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function to($mobile) |
||
| 174 | |||
| 175 | 6 | /** |
|
| 176 | * set content for content sms |
||
| 177 | * |
||
| 178 | * @param $content |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function content($content) |
||
| 188 | |||
| 189 | 3 | /** |
|
| 190 | * set template id for template sms |
||
| 191 | * |
||
| 192 | * @param $agentName |
||
| 193 | * @param $tempId |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function template($agentName, $tempId = null) |
||
| 212 | |||
| 213 | 3 | /** |
|
| 214 | * set data for template sms |
||
| 215 | * |
||
| 216 | * @param array $data |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function data(array $data) |
||
| 226 | |||
| 227 | 3 | /** |
|
| 228 | * set the first agent |
||
| 229 | * |
||
| 230 | * @param $name |
||
| 231 | * |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function agent($name) |
||
| 240 | |||
| 241 | 3 | /** |
|
| 242 | * start send |
||
| 243 | * |
||
| 244 | * @param bool $immediately |
||
| 245 | * |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | public function send($immediately = false) |
||
| 277 | |||
| 278 | 15 | /** |
|
| 279 | * push sms send task to queue |
||
| 280 | * |
||
| 281 | * @throws \Exception | PhpSmsException |
||
| 282 | * |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | protected function push() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * get sms data |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function getData() |
||
| 310 | |||
| 311 | 33 | /** |
|
| 312 | * init |
||
| 313 | */ |
||
| 314 | protected static function init() |
||
| 320 | |||
| 321 | 15 | /** |
|
| 322 | * generator a sms send task |
||
| 323 | 15 | * |
|
| 324 | * @return null |
||
| 325 | */ |
||
| 326 | public static function generatorTask() |
||
| 334 | |||
| 335 | /** |
||
| 336 | 3 | * configuration |
|
| 337 | 3 | */ |
|
| 338 | 3 | protected static function configuration() |
|
| 345 | |||
| 346 | 15 | /** |
|
| 347 | * generate enabled agents name |
||
| 348 | 15 | * |
|
| 349 | 15 | * @param array $config |
|
| 350 | 3 | */ |
|
| 351 | 3 | protected static function generatorAgentsName(&$config) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * generator agents config |
||
| 362 | * |
||
| 363 | * @param array $config |
||
| 364 | */ |
||
| 365 | 3 | protected static function generatorAgentsConfig(&$config) |
|
| 378 | 3 | ||
| 379 | /** |
||
| 380 | 3 | * config value validator |
|
| 381 | 3 | * |
|
| 382 | 3 | * @throws PhpSmsException |
|
| 383 | */ |
||
| 384 | protected static function configValidator() |
||
| 390 | |||
| 391 | 15 | /** |
|
| 392 | * create drivers for sms send task |
||
| 393 | * |
||
| 394 | 15 | * @param $task |
|
| 395 | 15 | */ |
|
| 396 | 15 | protected static function createDrivers($task) |
|
| 430 | 15 | ||
| 431 | /** |
||
| 432 | 15 | * 解析可用代理器的数组模式的调度配置 |
|
| 433 | 3 | * |
|
| 434 | 3 | * @param array $options |
|
| 435 | 3 | * |
|
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | protected static function parseAgentArrayOptions(array $options) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * 从调度配置中拉取指定数据 |
||
| 451 | * |
||
| 452 | * @param $options |
||
| 453 | * @param $name |
||
| 454 | * |
||
| 455 | * @return null|string |
||
| 456 | */ |
||
| 457 | protected static function pullAgentOptionByName(&$options, $name) |
||
| 467 | 3 | ||
| 468 | /** |
||
| 469 | * get agent config data by name |
||
| 470 | 3 | * |
|
| 471 | * @param $name |
||
| 472 | 18 | * |
|
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | protected static function getAgentConfigData($name) |
||
| 480 | 18 | ||
| 481 | /** |
||
| 482 | 18 | * get a sms agent instance, |
|
| 483 | * if null, will create a new agent instance |
||
| 484 | * |
||
| 485 | * @param $name |
||
| 486 | 18 | * @param array $configData |
|
| 487 | * |
||
| 488 | * @throws PhpSmsException |
||
| 489 | * |
||
| 490 | * @return mixed |
||
| 491 | */ |
||
| 492 | public static function getSmsAgent($name, array $configData) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * validate |
||
| 515 | * |
||
| 516 | * @throws PhpSmsException |
||
| 517 | */ |
||
| 518 | 6 | protected function validator() |
|
| 526 | |||
| 527 | /** |
||
| 528 | 6 | * set enable agents |
|
| 529 | 6 | * |
|
| 530 | 6 | * @param $agentName |
|
| 531 | * @param null $options |
||
| 532 | */ |
||
| 533 | public static function enable($agentName, $options = null) |
||
| 547 | 9 | ||
| 548 | /** |
||
| 549 | 9 | * set config for available agents |
|
| 550 | * |
||
| 551 | * @param $agentName |
||
| 552 | * @param array $config |
||
| 553 | * |
||
| 554 | * @throws PhpSmsException |
||
| 555 | 3 | */ |
|
| 556 | public static function agents($agentName, array $config = []) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * get enable agents |
||
| 572 | * |
||
| 573 | * @return array |
||
| 574 | */ |
||
| 575 | public static function getEnableAgents() |
||
| 579 | 6 | ||
| 580 | 6 | /** |
|
| 581 | 6 | * get agents config info |
|
| 582 | 6 | * |
|
| 583 | 6 | * @return array |
|
| 584 | 6 | */ |
|
| 585 | 6 | public static function getAgentsConfig() |
|
| 589 | 6 | ||
| 590 | /** |
||
| 591 | * tear down enable agents |
||
| 592 | 6 | */ |
|
| 593 | public static function cleanEnableAgents() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * tear down agents config |
||
| 600 | */ |
||
| 601 | public static function cleanAgentsConfig() |
||
| 605 | |||
| 606 | 3 | /** |
|
| 607 | 3 | * overload static method |
|
| 608 | * |
||
| 609 | * @param $name |
||
| 610 | 3 | * @param $args |
|
| 611 | * |
||
| 612 | * @throws PhpSmsException |
||
| 613 | */ |
||
| 614 | public static function __callStatic($name, $args) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * overload method |
||
| 636 | * |
||
| 637 | * @param $name |
||
| 638 | * @param $args |
||
| 639 | * |
||
| 640 | * @throws PhpSmsException |
||
| 641 | * @throws \Exception |
||
| 642 | */ |
||
| 643 | public function __call($name, $args) |
||
| 651 | } |
||
| 652 |
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..