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 | * log agent`s name |
||
| 19 | */ |
||
| 20 | const LOG_AGENT = 'Log'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * agents instance |
||
| 24 | */ |
||
| 25 | protected static $agents; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * agents`s name |
||
| 29 | * |
||
| 30 | * @var |
||
| 31 | */ |
||
| 32 | protected static $agentsName = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * agents`s config |
||
| 36 | * |
||
| 37 | * @var |
||
| 38 | */ |
||
| 39 | protected static $agentsConfig = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * whether to enable queue |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected static $enableQueue = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * queue work |
||
| 50 | * |
||
| 51 | * @var null |
||
| 52 | */ |
||
| 53 | protected static $howToUseQueue = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * sms already pushed to queue |
||
| 57 | * |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $pushedToQueue = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * hook handlers |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected static $enableHooks = [ |
||
| 68 | 'beforeRun', |
||
| 69 | 'afterRun', |
||
| 70 | ]; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * sms data |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $smsData = [ |
||
| 78 | 'to' => null, |
||
| 79 | 'templates' => [], |
||
| 80 | 'content' => '', |
||
| 81 | 'templateData' => [], |
||
| 82 | 'voiceCode' => null, |
||
| 83 | ]; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * first agent for send sms/voice verify |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $firstAgent = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * construct |
||
| 94 | */ |
||
| 95 | 9 | public function __construct() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * create sms instance and set templates |
||
| 102 | * |
||
| 103 | * @param null $agentName |
||
| 104 | * @param null $tempId |
||
| 105 | * |
||
| 106 | * @return Sms |
||
| 107 | */ |
||
| 108 | 3 | public static function make($agentName = null, $tempId = null) |
|
| 109 | { |
||
| 110 | 3 | $sms = new self(); |
|
| 111 | 3 | if (is_array($agentName)) { |
|
| 112 | $sms->template($agentName); |
||
| 113 | 3 | } elseif ($agentName && is_string($agentName)) { |
|
| 114 | if ($tempId === null) { |
||
| 115 | $sms->content($agentName); |
||
| 116 | } elseif (is_string("$tempId")) { |
||
| 117 | $sms->template($agentName, $tempId); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | 3 | return $sms; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * send voice verify |
||
| 126 | * |
||
| 127 | * @param $code |
||
| 128 | * |
||
| 129 | * @return Sms |
||
| 130 | */ |
||
| 131 | 3 | public static function voice($code) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * set how to use queue. |
||
| 141 | * |
||
| 142 | * @param $enable |
||
| 143 | * @param $handler |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 3 | public static function queue($enable = null, $handler = null) |
|
| 148 | { |
||
| 149 | 3 | if ($enable === null && $handler === null) { |
|
| 150 | 3 | return self::$enableQueue; |
|
| 151 | } |
||
| 152 | 3 | if (is_callable($enable)) { |
|
| 153 | 3 | $handler = $enable; |
|
| 154 | 3 | $enable = true; |
|
| 155 | 3 | } |
|
| 156 | 3 | self::$enableQueue = (bool) $enable; |
|
| 157 | 3 | if (is_callable($handler)) { |
|
| 158 | 3 | self::$howToUseQueue = $handler; |
|
|
|
|||
| 159 | 3 | } |
|
| 160 | |||
| 161 | 3 | return self::$enableQueue; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * set the mobile number |
||
| 166 | * |
||
| 167 | * @param $mobile |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | 6 | public function to($mobile) |
|
| 172 | { |
||
| 173 | 6 | $this->smsData['to'] = $mobile; |
|
| 174 | |||
| 175 | 6 | return $this; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * set content for content sms |
||
| 180 | * |
||
| 181 | * @param $content |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 3 | public function content($content) |
|
| 186 | { |
||
| 187 | 3 | $this->smsData['content'] = trim((String) $content); |
|
| 188 | |||
| 189 | 3 | return $this; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * set template id for template sms |
||
| 194 | * |
||
| 195 | * @param $agentName |
||
| 196 | * @param $tempId |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | 3 | public function template($agentName, $tempId = null) |
|
| 201 | { |
||
| 202 | 3 | if (is_array($agentName)) { |
|
| 203 | 3 | foreach ($agentName as $k => $v) { |
|
| 204 | 3 | $this->template($k, $v); |
|
| 205 | 3 | } |
|
| 206 | 3 | } elseif ($agentName && $tempId) { |
|
| 207 | 3 | if (!isset($this->smsData['templates']) || !is_array($this->smsData['templates'])) { |
|
| 208 | $this->smsData['templates'] = []; |
||
| 209 | } |
||
| 210 | 3 | $this->smsData['templates']["$agentName"] = $tempId; |
|
| 211 | 3 | } |
|
| 212 | |||
| 213 | 3 | return $this; |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * set data for template sms |
||
| 218 | * |
||
| 219 | * @param array $data |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | 18 | public function data(array $data) |
|
| 224 | { |
||
| 225 | 18 | $this->smsData['templateData'] = $data; |
|
| 226 | |||
| 227 | 3 | return $this; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * set the first agent |
||
| 232 | * |
||
| 233 | * @param $name |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | 3 | public function agent($name) |
|
| 238 | { |
||
| 239 | 3 | $this->firstAgent = (String) $name; |
|
| 240 | |||
| 241 | 3 | return $this; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * start send |
||
| 246 | * |
||
| 247 | * @param bool $immediately |
||
| 248 | * |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | 15 | public function send($immediately = false) |
|
| 252 | { |
||
| 253 | 15 | $this->validator(); |
|
| 254 | |||
| 255 | // if disable push to queue, |
||
| 256 | // send the sms immediately. |
||
| 257 | 15 | if (!self::$enableQueue) { |
|
| 258 | 12 | $immediately = true; |
|
| 259 | 12 | } |
|
| 260 | |||
| 261 | // whatever 'PhpSms' whether to enable or disable push to queue, |
||
| 262 | // if you are already pushed sms instance to queue, |
||
| 263 | // you can recall the method `send()` in queue job without `true` parameter. |
||
| 264 | // |
||
| 265 | // So this mechanism in order to make you convenient use the method `send()` in queue system. |
||
| 266 | 15 | if ($this->pushedToQueue) { |
|
| 267 | 3 | $immediately = true; |
|
| 268 | 3 | } |
|
| 269 | |||
| 270 | // whether to send sms immediately, |
||
| 271 | // or push it to queue. |
||
| 272 | 15 | if ($immediately) { |
|
| 273 | 15 | $result = Balancer::run(self::TASK, $this->getData(), $this->firstAgent); |
|
| 274 | 15 | } else { |
|
| 275 | 3 | $result = $this->push(); |
|
| 276 | } |
||
| 277 | |||
| 278 | 15 | return $result; |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * push sms send task to queue |
||
| 283 | * |
||
| 284 | * @throws \Exception | PhpSmsException |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | 3 | protected function push() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * get sms data |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | 33 | public function getData() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * init |
||
| 316 | * |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | 15 | protected static function init() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * generator a sms send task |
||
| 328 | * |
||
| 329 | * @return null |
||
| 330 | */ |
||
| 331 | 18 | public static function generatorTask() |
|
| 332 | { |
||
| 333 | 18 | if (!Balancer::getTask(self::TASK)) { |
|
| 334 | Balancer::task(self::TASK, function ($task) { |
||
| 335 | // create drivers |
||
| 336 | 3 | self::createAgents($task); |
|
| 337 | 3 | }); |
|
| 338 | 3 | } |
|
| 339 | |||
| 340 | 18 | return Balancer::getTask(self::TASK); |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * configuration |
||
| 345 | */ |
||
| 346 | 15 | protected static function configuration() |
|
| 347 | { |
||
| 348 | 15 | $config = []; |
|
| 349 | 15 | if (empty(self::$agentsName)) { |
|
| 350 | 3 | $config = include __DIR__ . '/../config/phpsms.php'; |
|
| 351 | 3 | self::generatorAgentsName($config); |
|
| 352 | 3 | } |
|
| 353 | 15 | if (empty(self::$agentsConfig)) { |
|
| 354 | 3 | $config = $config ?: include __DIR__ . '/../config/phpsms.php'; |
|
| 355 | 3 | self::generatorAgentsConfig($config); |
|
| 356 | 3 | } |
|
| 357 | 15 | self::configValidator(); |
|
| 358 | 15 | } |
|
| 359 | |||
| 360 | /** |
||
| 361 | * generate enabled agents name |
||
| 362 | * |
||
| 363 | * @param array $config |
||
| 364 | */ |
||
| 365 | 3 | protected static function generatorAgentsName($config) |
|
| 366 | { |
||
| 367 | 3 | $config = isset($config['enable']) ? $config['enable'] : null; |
|
| 368 | 3 | if ($config) { |
|
| 369 | 3 | self::enable($config); |
|
| 370 | 3 | } |
|
| 371 | 3 | } |
|
| 372 | |||
| 373 | /** |
||
| 374 | * generator agents config |
||
| 375 | * |
||
| 376 | * @param array $config |
||
| 377 | */ |
||
| 378 | 3 | protected static function generatorAgentsConfig($config) |
|
| 379 | { |
||
| 380 | 3 | $config = isset($config['agents']) ? $config['agents'] : []; |
|
| 381 | 3 | self::agents($config); |
|
| 382 | 3 | } |
|
| 383 | |||
| 384 | /** |
||
| 385 | * config value validator |
||
| 386 | * |
||
| 387 | * @throws PhpSmsException |
||
| 388 | */ |
||
| 389 | 15 | protected static function configValidator() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * create drivers for sms send task |
||
| 406 | * |
||
| 407 | * @param $task |
||
| 408 | */ |
||
| 409 | 15 | protected static function createAgents($task) |
|
| 410 | { |
||
| 411 | 3 | foreach (self::$agentsName as $name => $options) { |
|
| 412 | 3 | $configData = self::getAgentConfigData($name); |
|
| 413 | 3 | $task->driver("$name $options") |
|
| 414 | 3 | ->data($configData) |
|
| 415 | 15 | ->work(function ($driver) { |
|
| 416 | 15 | $configData = $driver->getDriverData(); |
|
| 417 | 15 | $agent = self::getSmsAgent($driver->name, $configData); |
|
| 418 | 15 | $smsData = $driver->getTaskData(); |
|
| 419 | 15 | extract($smsData); |
|
| 420 | 15 | if (isset($smsData['voiceCode']) && $smsData['voiceCode']) { |
|
| 421 | $agent->voiceVerify($to, $voiceCode); |
||
| 422 | } else { |
||
| 423 | 15 | $template = isset($templates[$driver->name]) ? $templates[$driver->name] : 0; |
|
| 424 | 15 | $agent->sendSms($template, $to, $templateData, $content); |
|
| 425 | } |
||
| 426 | 15 | $result = $agent->getResult(); |
|
| 427 | 15 | if ($result['success']) { |
|
| 428 | 15 | $driver->success(); |
|
| 429 | 15 | } |
|
| 430 | 15 | unset($result['success']); |
|
| 431 | |||
| 432 | 15 | return $result; |
|
| 433 | 3 | }); |
|
| 434 | 3 | } |
|
| 435 | 3 | } |
|
| 436 | |||
| 437 | /** |
||
| 438 | * get agent config data by name |
||
| 439 | * |
||
| 440 | * @param $name |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | 3 | protected static function getAgentConfigData($name) |
|
| 445 | { |
||
| 446 | 3 | return isset(self::$agentsConfig[$name]) ? |
|
| 447 | 3 | (Array) self::$agentsConfig[$name] : []; |
|
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * get a sms agent instance, |
||
| 452 | * if null, will create a new agent instance |
||
| 453 | * |
||
| 454 | * @param $name |
||
| 455 | * @param array $configData |
||
| 456 | * |
||
| 457 | * @throws PhpSmsException |
||
| 458 | * |
||
| 459 | * @return mixed |
||
| 460 | */ |
||
| 461 | 18 | public static function getSmsAgent($name, array $configData) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * validate |
||
| 477 | * |
||
| 478 | * @throws PhpSmsException |
||
| 479 | */ |
||
| 480 | 18 | protected function validator() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * set enable agents |
||
| 491 | * |
||
| 492 | * @param $agentName |
||
| 493 | * @param null $options |
||
| 494 | */ |
||
| 495 | 6 | public static function enable($agentName, $options = null) |
|
| 496 | { |
||
| 497 | 6 | if (is_array($agentName)) { |
|
| 498 | 6 | foreach ($agentName as $name => $opt) { |
|
| 499 | 6 | self::enable($name, $opt); |
|
| 500 | 6 | } |
|
| 501 | 6 | } elseif ($agentName && is_string($agentName) && !is_array($options) && is_string("$options")) { |
|
| 502 | 3 | self::$agentsName["$agentName"] = "$options"; |
|
| 503 | 6 | } elseif (is_int($agentName) && !is_array($options) && "$options") { |
|
| 504 | 3 | self::$agentsName["$options"] = '1'; |
|
| 505 | 3 | } elseif ($agentName && $options === null) { |
|
| 506 | self::$agentsName["$agentName"] = '1'; |
||
| 507 | } |
||
| 508 | 6 | } |
|
| 509 | |||
| 510 | /** |
||
| 511 | * set config for available agents |
||
| 512 | * |
||
| 513 | * @param $agentName |
||
| 514 | * @param array $config |
||
| 515 | * |
||
| 516 | * @throws PhpSmsException |
||
| 517 | */ |
||
| 518 | 6 | public static function agents($agentName, array $config = []) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * get enable agents |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | 9 | public static function getEnableAgents() |
|
| 541 | |||
| 542 | /** |
||
| 543 | * get agents config info |
||
| 544 | * |
||
| 545 | * @return array |
||
| 546 | */ |
||
| 547 | 9 | public static function getAgentsConfig() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * tear down enable agents |
||
| 554 | */ |
||
| 555 | 3 | public static function cleanEnableAgents() |
|
| 556 | { |
||
| 557 | 3 | self::$agentsName = []; |
|
| 558 | 3 | } |
|
| 559 | |||
| 560 | /** |
||
| 561 | * tear down agents config |
||
| 562 | */ |
||
| 563 | 3 | public static function cleanAgentsConfig() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * overload static method |
||
| 570 | * |
||
| 571 | * @param $name |
||
| 572 | * @param $args |
||
| 573 | * |
||
| 574 | * @throws PhpSmsException |
||
| 575 | */ |
||
| 576 | 6 | public static function __callStatic($name, $args) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * overload method |
||
| 596 | * |
||
| 597 | * @param $name |
||
| 598 | * @param $args |
||
| 599 | * |
||
| 600 | * @throws PhpSmsException |
||
| 601 | * @throws \Exception |
||
| 602 | */ |
||
| 603 | 3 | public function __call($name, $args) |
|
| 611 | } |
||
| 612 |
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..