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 \Closure |
||
| 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 | * @param bool $autoBoot |
||
| 93 | */ |
||
| 94 | 9 | public function __construct($autoBoot = true) |
|
| 95 | { |
||
| 96 | 9 | if ($autoBoot) { |
|
| 97 | 9 | self::bootstrap(); |
|
| 98 | 9 | } |
|
| 99 | 9 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * create sms instance and set templates |
||
| 103 | * |
||
| 104 | * @param null $agentName |
||
| 105 | * @param null $tempId |
||
| 106 | * |
||
| 107 | * @return Sms |
||
| 108 | */ |
||
| 109 | 3 | public static function make($agentName = null, $tempId = null) |
|
| 110 | { |
||
| 111 | 3 | $sms = new self(); |
|
| 112 | 3 | if (is_array($agentName)) { |
|
| 113 | $sms->template($agentName); |
||
| 114 | 3 | } elseif ($agentName && is_string($agentName)) { |
|
| 115 | if ($tempId === null) { |
||
| 116 | $sms->content($agentName); |
||
| 117 | } elseif (is_string("$tempId")) { |
||
| 118 | $sms->template($agentName, $tempId); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | 3 | return $sms; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * send voice verify |
||
| 127 | * |
||
| 128 | * @param $code |
||
| 129 | * |
||
| 130 | * @return Sms |
||
| 131 | */ |
||
| 132 | 3 | public static function voice($code) |
|
| 133 | { |
||
| 134 | 3 | $sms = new self(); |
|
| 135 | 3 | $sms->smsData['voiceCode'] = $code; |
|
| 136 | |||
| 137 | 3 | return $sms; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * set how to use queue. |
||
| 142 | * |
||
| 143 | * @param $enable |
||
| 144 | * @param $handler |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 3 | public static function queue($enable = null, $handler = null) |
|
| 149 | { |
||
| 150 | 3 | if ($enable === null && $handler === null) { |
|
| 151 | 3 | return self::$enableQueue; |
|
| 152 | } |
||
| 153 | 3 | if (is_callable($enable)) { |
|
| 154 | 3 | $handler = $enable; |
|
| 155 | 3 | $enable = true; |
|
| 156 | 3 | } |
|
| 157 | 3 | self::$enableQueue = (bool) $enable; |
|
| 158 | 3 | if (is_callable($handler)) { |
|
| 159 | 3 | self::$howToUseQueue = $handler; |
|
|
|
|||
| 160 | 3 | } |
|
| 161 | |||
| 162 | 3 | return self::$enableQueue; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * set the mobile number |
||
| 167 | * |
||
| 168 | * @param $mobile |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 6 | public function to($mobile) |
|
| 173 | { |
||
| 174 | 6 | $this->smsData['to'] = $mobile; |
|
| 175 | |||
| 176 | 6 | return $this; |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * set content for content sms |
||
| 181 | * |
||
| 182 | * @param $content |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | 3 | public function content($content) |
|
| 187 | { |
||
| 188 | 3 | $this->smsData['content'] = trim((String) $content); |
|
| 189 | |||
| 190 | 3 | return $this; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * set template id for template sms |
||
| 195 | * |
||
| 196 | * @param $agentName |
||
| 197 | * @param $tempId |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | 3 | public function template($agentName, $tempId = null) |
|
| 202 | { |
||
| 203 | 3 | if (is_array($agentName)) { |
|
| 204 | 3 | foreach ($agentName as $k => $v) { |
|
| 205 | 3 | $this->template($k, $v); |
|
| 206 | 3 | } |
|
| 207 | 3 | } elseif ($agentName && $tempId) { |
|
| 208 | 3 | if (!isset($this->smsData['templates']) || !is_array($this->smsData['templates'])) { |
|
| 209 | $this->smsData['templates'] = []; |
||
| 210 | } |
||
| 211 | 3 | $this->smsData['templates']["$agentName"] = $tempId; |
|
| 212 | 3 | } |
|
| 213 | |||
| 214 | 3 | return $this; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * set data for template sms |
||
| 219 | * |
||
| 220 | * @param array $data |
||
| 221 | * |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | 15 | public function data(array $data) |
|
| 225 | 15 | { |
|
| 226 | 3 | $this->smsData['templateData'] = $data; |
|
| 227 | |||
| 228 | 3 | return $this; |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * set the first agent |
||
| 233 | * |
||
| 234 | * @param $name |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | 3 | public function agent($name) |
|
| 239 | { |
||
| 240 | 3 | $this->firstAgent = (String) $name; |
|
| 241 | |||
| 242 | 3 | return $this; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * start send |
||
| 247 | * |
||
| 248 | * @param bool $immediately |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | 15 | public function send($immediately = false) |
|
| 253 | { |
||
| 254 | 15 | $this->validator(); |
|
| 255 | |||
| 256 | // if disable push to queue, |
||
| 257 | // send the sms immediately. |
||
| 258 | 15 | if (!self::$enableQueue) { |
|
| 259 | 12 | $immediately = true; |
|
| 260 | 12 | } |
|
| 261 | |||
| 262 | // whatever 'PhpSms' whether to enable or disable push to queue, |
||
| 263 | // if you are already pushed sms instance to queue, |
||
| 264 | // you can recall the method `send()` in queue job without `true` parameter. |
||
| 265 | // |
||
| 266 | // So this mechanism in order to make you convenient use the method `send()` in queue system. |
||
| 267 | 15 | if ($this->pushedToQueue) { |
|
| 268 | 3 | $immediately = true; |
|
| 269 | 3 | } |
|
| 270 | |||
| 271 | // whether to send sms immediately, |
||
| 272 | // or push it to queue. |
||
| 273 | 15 | if ($immediately) { |
|
| 274 | 15 | $result = Balancer::run(self::TASK, $this->getData(), $this->firstAgent); |
|
| 275 | 15 | } else { |
|
| 276 | 3 | $result = $this->push(); |
|
| 277 | } |
||
| 278 | |||
| 279 | 15 | return $result; |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * push sms send task to queue |
||
| 284 | * |
||
| 285 | * @throws \Exception | PhpSmsException |
||
| 286 | * |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | 3 | protected function push() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * get sms data |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | 33 | public function getData() |
|
| 311 | { |
||
| 312 | 33 | return $this->smsData; |
|
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * bootstrap |
||
| 317 | */ |
||
| 318 | 9 | public static function bootstrap() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * generator a sms send task |
||
| 329 | * |
||
| 330 | * @return object |
||
| 331 | */ |
||
| 332 | 18 | public static function generatorTask() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * configuration |
||
| 343 | */ |
||
| 344 | 3 | protected static function configuration() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * generate enabled agents name |
||
| 354 | * |
||
| 355 | * @param array $config |
||
| 356 | */ |
||
| 357 | 3 | protected static function generatorAgentsName(&$config) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * generator agents config |
||
| 368 | * |
||
| 369 | * @param array $config |
||
| 370 | */ |
||
| 371 | 3 | protected static function generatorAgentsConfig(&$config) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * config value validator |
||
| 387 | * |
||
| 388 | * @throws PhpSmsException |
||
| 389 | */ |
||
| 390 | 3 | protected static function configValidator() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * create drivers for sms send task |
||
| 399 | * |
||
| 400 | * @param $task |
||
| 401 | */ |
||
| 402 | 15 | 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 | * get agent config data by name |
||
| 476 | * |
||
| 477 | * @param $name |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | 3 | 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 | * @param array $configData |
||
| 493 | * |
||
| 494 | * @throws PhpSmsException |
||
| 495 | * |
||
| 496 | * @return mixed |
||
| 497 | */ |
||
| 498 | 18 | public static function getSmsAgent($name, array $configData) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * validate |
||
| 521 | * |
||
| 522 | * @throws PhpSmsException |
||
| 523 | */ |
||
| 524 | 18 | protected function validator() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * set enable agents |
||
| 535 | * |
||
| 536 | * @param $agentName |
||
| 537 | * @param null $options |
||
| 538 | */ |
||
| 539 | 6 | public static function enable($agentName, $options = null) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * set config for available agents |
||
| 556 | * |
||
| 557 | * @param $agentName |
||
| 558 | * @param array $config |
||
| 559 | * |
||
| 560 | * @throws PhpSmsException |
||
| 561 | */ |
||
| 562 | 6 | public static function agents($agentName, array $config = []) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * get enable agents |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | 9 | public static function getEnableAgents() |
|
| 585 | |||
| 586 | /** |
||
| 587 | * get agents config info |
||
| 588 | * |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | 9 | public static function getAgentsConfig() |
|
| 595 | |||
| 596 | /** |
||
| 597 | * tear down enable agents |
||
| 598 | */ |
||
| 599 | 3 | public static function cleanEnableAgents() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * tear down agents config |
||
| 606 | */ |
||
| 607 | 3 | public static function cleanAgentsConfig() |
|
| 611 | |||
| 612 | /** |
||
| 613 | * overload static method |
||
| 614 | * |
||
| 615 | * @param $name |
||
| 616 | * @param $args |
||
| 617 | * |
||
| 618 | * @throws PhpSmsException |
||
| 619 | */ |
||
| 620 | 6 | public static function __callStatic($name, $args) |
|
| 639 | |||
| 640 | /** |
||
| 641 | * overload method |
||
| 642 | * |
||
| 643 | * @param $name |
||
| 644 | * @param $args |
||
| 645 | * |
||
| 646 | * @throws PhpSmsException |
||
| 647 | * @throws \Exception |
||
| 648 | */ |
||
| 649 | 3 | 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..