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 |
||
| 14 | class Sms |
||
| 15 | { |
||
| 16 | const TASK_NAME = 'PhpSms'; |
||
| 17 | const TYPE_SMS = 1; |
||
| 18 | const TYPE_VOICE = 2; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The instances of Agent. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected static $agents = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The dispatch scheme of agent, |
||
| 29 | * and these agents are available. |
||
| 30 | * example: |
||
| 31 | * [ |
||
| 32 | * 'Agent1' => '10 backup', |
||
| 33 | * 'Agent2' => '20 backup', |
||
| 34 | * ] |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected static $scheme = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The configuration information of agents. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected static $agentsConfig = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Whether to use the queue. |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected static $enableQueue = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * How to use the queue. |
||
| 56 | * |
||
| 57 | * @var \Closure |
||
| 58 | */ |
||
| 59 | protected static $howToUseQueue = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The available hooks for balancing task. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected static $availableHooks = [ |
||
| 67 | 'beforeRun', |
||
| 68 | 'beforeDriverRun', |
||
| 69 | 'afterDriverRun', |
||
| 70 | 'afterRun', |
||
| 71 | ]; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * An instance of class [SuperClosure\Serializer] use for serialization closures. |
||
| 75 | * |
||
| 76 | * @var Serializer |
||
| 77 | */ |
||
| 78 | protected static $serializer = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The data container of SMS/voice verify. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $smsData = [ |
||
| 86 | 'type' => self::TYPE_SMS, |
||
| 87 | 'to' => null, |
||
| 88 | 'templates' => [], |
||
| 89 | 'templateData' => [], |
||
| 90 | 'content' => null, |
||
| 91 | 'voiceCode' => null, |
||
| 92 | ]; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The name of first agent. |
||
| 96 | * |
||
| 97 | * @var string|null |
||
| 98 | */ |
||
| 99 | protected $firstAgent = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Whether the current instance has already pushed to the queue system. |
||
| 103 | * |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $pushedToQueue = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Status container, |
||
| 110 | * store some configuration information before serialize current instance(before enqueue). |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected $_status_before_enqueue_ = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Constructor |
||
| 118 | * |
||
| 119 | * @param bool $autoBoot |
||
| 120 | */ |
||
| 121 | 6 | public function __construct($autoBoot = true) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Boot balancing task for send SMS/voice verify. |
||
| 130 | * |
||
| 131 | * Note: 判断drivers是否为空不能用'empty',因为在TaskBalance库的中Task类的drivers属性是受保护的(不可访问), |
||
| 132 | * 虽然通过魔术方法可以获取到其值,但在其目前版本(v0.4.2)其内部却并没有使用'__isset'魔术方法对'empty'或'isset'函数进行逻辑补救. |
||
| 133 | */ |
||
| 134 | 6 | public static function bootstrap() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get or generate a balancing task instance for send SMS/voice verify. |
||
| 145 | * |
||
| 146 | * @return Task |
||
| 147 | */ |
||
| 148 | 15 | public static function getTask() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Configuration. |
||
| 159 | */ |
||
| 160 | 6 | protected static function configuration() |
|
| 161 | { |
||
| 162 | 6 | $config = []; |
|
| 163 | 6 | if (!count(self::scheme())) { |
|
| 164 | 3 | self::initScheme($config); |
|
| 165 | 2 | } |
|
| 166 | 6 | $diff = array_diff_key(self::scheme(), self::$agentsConfig); |
|
| 167 | 6 | self::initAgentsConfig(array_keys($diff), $config); |
|
| 168 | 6 | self::validateConfig(); |
|
| 169 | 6 | } |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Try to read the dispatch scheme of agent from config file. |
||
| 173 | * |
||
| 174 | * @param array $config |
||
| 175 | */ |
||
| 176 | 3 | protected static function initScheme(array &$config) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Try to initialize the specified agents` configuration information. |
||
| 185 | * |
||
| 186 | * @param array $agents |
||
| 187 | * @param array $config |
||
| 188 | */ |
||
| 189 | 6 | protected static function initAgentsConfig(array $agents, array &$config) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * validate configuration. |
||
| 204 | * |
||
| 205 | * @throws PhpSmsException |
||
| 206 | */ |
||
| 207 | 6 | protected static function validateConfig() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Create drivers for the balancing task. |
||
| 216 | * |
||
| 217 | * @param Task $task |
||
| 218 | */ |
||
| 219 | 18 | protected static function createDrivers(Task $task) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Parsing the dispatch scheme. |
||
| 251 | * 解析代理器的数组模式的调度配置 |
||
| 252 | * |
||
| 253 | * @param array $options |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 9 | protected static function parseScheme(array $options) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Get a sms agent instance by agent name, |
||
| 270 | * if null, will try to create a new agent instance. |
||
| 271 | * |
||
| 272 | * @param string $name |
||
| 273 | * |
||
| 274 | * @throws PhpSmsException |
||
| 275 | * |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | 27 | public static function getAgent($name) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Whether to has specified agent. |
||
| 299 | * |
||
| 300 | * @param string $name |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | 36 | public static function hasAgent($name) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Set or get the dispatch scheme of agent by name. |
||
| 311 | * |
||
| 312 | * @param mixed $name |
||
| 313 | * @param mixed $scheme |
||
| 314 | * |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | 18 | public static function scheme($name = null, $scheme = null) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Modify the dispatch scheme of agent by name. |
||
| 330 | * |
||
| 331 | * @param $key |
||
| 332 | * @param $value |
||
| 333 | * |
||
| 334 | * @throws PhpSmsException |
||
| 335 | */ |
||
| 336 | 6 | protected static function modifyScheme($key, $value) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Set or get configuration information by agent name. |
||
| 344 | * |
||
| 345 | * @param mixed $name |
||
| 346 | * @param mixed $config |
||
| 347 | * @param bool $override |
||
| 348 | * |
||
| 349 | * @throws PhpSmsException |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | 18 | public static function config($name = null, $config = null, $override = false) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Modify the configuration information of agent by name. |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * @param array $value |
||
| 378 | * |
||
| 379 | * @throws PhpSmsException |
||
| 380 | */ |
||
| 381 | 9 | protected static function modifyConfig($key, array $value) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Validate the agent name. |
||
| 392 | * Agent name must be a string, but not be a number string |
||
| 393 | * |
||
| 394 | * @param string $name |
||
| 395 | * |
||
| 396 | * @throws PhpSmsException |
||
| 397 | */ |
||
| 398 | 12 | protected static function validateAgentName($name) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Tear down agent use scheme and prepare to create and start a new balancing task, |
||
| 407 | * so before do it must destroy old task instance. |
||
| 408 | */ |
||
| 409 | 6 | public static function cleanScheme() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Tear down all the configuration information of agent. |
||
| 417 | */ |
||
| 418 | 6 | public static function cleanConfig() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Create a sms instance send SMS, |
||
| 425 | * your can also set SMS templates or content at the same time. |
||
| 426 | * |
||
| 427 | * @param mixed $agentName |
||
| 428 | * @param mixed $tempId |
||
| 429 | * |
||
| 430 | * @return Sms |
||
| 431 | */ |
||
| 432 | public static function make($agentName = null, $tempId = null) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Create a sms instance send voice verify, |
||
| 451 | * your can also set verify code at the same time. |
||
| 452 | * |
||
| 453 | * @param int|string|null $code |
||
| 454 | * |
||
| 455 | * @return Sms |
||
| 456 | */ |
||
| 457 | 3 | public static function voice($code = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Set whether to use the queue system, and define how to use it. |
||
| 468 | * |
||
| 469 | * @param mixed $enable |
||
| 470 | * @param mixed $handler |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | 3 | public static function queue($enable = null, $handler = null) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Set the recipient`s mobile number. |
||
| 493 | * |
||
| 494 | * @param string $mobile |
||
| 495 | * |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | 6 | public function to($mobile) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Set the content for content SMS. |
||
| 507 | * |
||
| 508 | * @param string $content |
||
| 509 | * |
||
| 510 | * @return $this |
||
| 511 | */ |
||
| 512 | 3 | public function content($content) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Set the template id for template SMS. |
||
| 521 | * |
||
| 522 | * @param mixed $agentName |
||
| 523 | * @param mixed $tempId |
||
| 524 | * |
||
| 525 | * @return $this |
||
| 526 | */ |
||
| 527 | 3 | public function template($agentName, $tempId = null) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Set the template data for template SMS. |
||
| 536 | * |
||
| 537 | * @param array $data |
||
| 538 | * |
||
| 539 | * @return $this |
||
| 540 | */ |
||
| 541 | 3 | public function data(array $data) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Set the first agent by name. |
||
| 550 | * |
||
| 551 | * @param string $name |
||
| 552 | * |
||
| 553 | * @return $this |
||
| 554 | */ |
||
| 555 | 3 | public function agent($name) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Start send SMS/voice verify. |
||
| 564 | * |
||
| 565 | * If give a true parameter, this system will immediately start request to send SMS/voice verify whatever whether to use the queue. |
||
| 566 | * if you are already pushed sms instance to the queue, you can recall the method `send()` in queue system without `true` parameter, |
||
| 567 | * so this mechanism in order to make you convenient use the method `send()` in queue system. |
||
| 568 | * |
||
| 569 | * @param bool $immediately |
||
| 570 | * |
||
| 571 | * @return mixed |
||
| 572 | */ |
||
| 573 | 18 | public function send($immediately = false) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Push to the queue by a custom method. |
||
| 592 | * |
||
| 593 | * @throws \Exception | PhpSmsException |
||
| 594 | * |
||
| 595 | * @return mixed |
||
| 596 | */ |
||
| 597 | 3 | public function push() |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Get all the data of SMS/voice verify. |
||
| 615 | * |
||
| 616 | * @param null|string $name |
||
| 617 | * |
||
| 618 | * @return mixed |
||
| 619 | */ |
||
| 620 | 36 | public function getData($name = null) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Overload static method. |
||
| 631 | * |
||
| 632 | * @param string $name |
||
| 633 | * @param array $args |
||
| 634 | * |
||
| 635 | * @throws PhpSmsException |
||
| 636 | */ |
||
| 637 | 9 | public static function __callStatic($name, $args) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Overload method. |
||
| 659 | * |
||
| 660 | * @param string $name |
||
| 661 | * @param array $args |
||
| 662 | * |
||
| 663 | * @throws PhpSmsException |
||
| 664 | * @throws \Exception |
||
| 665 | */ |
||
| 666 | 3 | public function __call($name, $args) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Serialize magic method. |
||
| 677 | * |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | 3 | public function __sleep() |
|
| 692 | |||
| 693 | /** |
||
| 694 | * Deserialize magic method. |
||
| 695 | */ |
||
| 696 | 3 | public function __wakeup() |
|
| 708 | |||
| 709 | /** |
||
| 710 | * Get a closure serializer. |
||
| 711 | * |
||
| 712 | * @return Serializer |
||
| 713 | */ |
||
| 714 | 3 | protected static function getSerializer() |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Serialize or deserialize the agent use scheme. |
||
| 725 | * |
||
| 726 | * @param array $scheme |
||
| 727 | * |
||
| 728 | * @return array |
||
| 729 | */ |
||
| 730 | 3 | protected static function serializeOrDeserializeScheme(array $scheme) |
|
| 741 | |||
| 742 | /** |
||
| 743 | * Serialize the hooks` handlers of balancing task |
||
| 744 | * |
||
| 745 | * @return array |
||
| 746 | */ |
||
| 747 | 3 | protected static function serializeHandlers() |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Reinstall hooks` handlers for balancing task. |
||
| 762 | * |
||
| 763 | * @param array $handlers |
||
| 764 | */ |
||
| 765 | 3 | protected static function reinstallHandlers(array $handlers) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Serialize/deserialize the specified closure and replace the origin value. |
||
| 780 | * |
||
| 781 | * @param array $options |
||
| 782 | * @param int|string $key |
||
| 783 | */ |
||
| 784 | 3 | protected static function serializeOrDeserializeClosureAndReplace(array &$options, $key) |
|
| 796 | } |
||
| 797 |
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..