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] for serialize closure objects. |
||
| 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 | 6 | public static function bootstrap() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Whether task initialized. |
||
| 141 | * |
||
| 142 | * Note: 判断drivers是否为空不能用'empty',因为在TaskBalance库的中Task类的drivers属性是受保护的(不可访问), |
||
| 143 | * 虽然通过魔术方法可以获取到其值,但在其目前版本(v0.4.2)其内部却并没有使用'__isset'魔术方法对'empty'或'isset'函数进行逻辑补救. |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 12 | protected static function taskInitialized() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get or generate a balancing task instance for send SMS/voice verify. |
||
| 156 | * |
||
| 157 | * @return Task |
||
| 158 | */ |
||
| 159 | 21 | public static function getTask() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Configuration. |
||
| 170 | */ |
||
| 171 | 6 | protected static function configuration() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Try to read the dispatch scheme of agent from config file. |
||
| 184 | * |
||
| 185 | * @param array $config |
||
| 186 | */ |
||
| 187 | 3 | protected static function initScheme(array &$config) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Try to initialize the specified agents` configuration information. |
||
| 196 | * |
||
| 197 | * @param array $agents |
||
| 198 | * @param array $config |
||
| 199 | */ |
||
| 200 | 6 | protected static function initAgentsConfig(array $agents, array &$config) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * validate configuration. |
||
| 215 | * |
||
| 216 | * @throws PhpSmsException |
||
| 217 | */ |
||
| 218 | 6 | protected static function validateConfig() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Initialize the task. |
||
| 227 | */ |
||
| 228 | 18 | protected static function initTask() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Parsing the dispatch scheme. |
||
| 260 | * 解析代理器的数组模式的调度配置 |
||
| 261 | * |
||
| 262 | * @param array $options |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | 9 | protected static function parseScheme(array $options) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Get a sms agent instance by agent name, |
||
| 279 | * if null, will try to create a new agent instance. |
||
| 280 | * |
||
| 281 | * @param string $name |
||
| 282 | * |
||
| 283 | * @throws PhpSmsException |
||
| 284 | * |
||
| 285 | * @return mixed |
||
| 286 | */ |
||
| 287 | 27 | public static function getAgent($name) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Whether to has specified agent. |
||
| 308 | * |
||
| 309 | * @param string $name |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | 36 | public static function hasAgent($name) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Set or get the dispatch scheme of agent by name. |
||
| 320 | * |
||
| 321 | * @param mixed $name |
||
| 322 | * @param mixed $scheme |
||
| 323 | * |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | 18 | public static function scheme($name = null, $scheme = null) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Modify the dispatch scheme of agent by name. |
||
| 339 | * |
||
| 340 | * @param $key |
||
| 341 | * @param $value |
||
| 342 | * |
||
| 343 | * @throws PhpSmsException |
||
| 344 | */ |
||
| 345 | 6 | protected static function modifyScheme($key, $value) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Set or get configuration information by agent name. |
||
| 356 | * |
||
| 357 | * @param mixed $name |
||
| 358 | * @param mixed $config |
||
| 359 | * @param bool $override |
||
| 360 | * |
||
| 361 | * @throws PhpSmsException |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | 18 | public static function config($name = null, $config = null, $override = false) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Modify the configuration information of agent by name. |
||
| 387 | * |
||
| 388 | * @param string $key |
||
| 389 | * @param array $value |
||
| 390 | * |
||
| 391 | * @throws PhpSmsException |
||
| 392 | */ |
||
| 393 | 9 | protected static function modifyConfig($key, array $value) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Validate the agent name. |
||
| 404 | * Agent name must be a string, but not be a number string |
||
| 405 | * |
||
| 406 | * @param string $name |
||
| 407 | * |
||
| 408 | * @throws PhpSmsException |
||
| 409 | */ |
||
| 410 | 12 | protected static function validateAgentName($name) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Tear down agent use scheme and prepare to create and start a new balancing task, |
||
| 419 | * so before do it must destroy old task instance. |
||
| 420 | */ |
||
| 421 | 6 | public static function cleanScheme() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Tear down all the configuration information of agent. |
||
| 429 | */ |
||
| 430 | 6 | public static function cleanConfig() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Create a sms instance send SMS, |
||
| 437 | * your can also set SMS templates or content at the same time. |
||
| 438 | * |
||
| 439 | * @param mixed $agentName |
||
| 440 | * @param mixed $tempId |
||
| 441 | * |
||
| 442 | * @return Sms |
||
| 443 | */ |
||
| 444 | public static function make($agentName = null, $tempId = null) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Create a sms instance send voice verify, |
||
| 463 | * your can also set verify code at the same time. |
||
| 464 | * |
||
| 465 | * @param int|string|null $code |
||
| 466 | * |
||
| 467 | * @return Sms |
||
| 468 | */ |
||
| 469 | 3 | public static function voice($code = null) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Set whether to use the queue system, and define how to use it. |
||
| 480 | * |
||
| 481 | * @param mixed $enable |
||
| 482 | * @param mixed $handler |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | 3 | public static function queue($enable = null, $handler = null) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Set the recipient`s mobile number. |
||
| 505 | * |
||
| 506 | * @param string $mobile |
||
| 507 | * |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | 6 | public function to($mobile) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Set the content for content SMS. |
||
| 519 | * |
||
| 520 | * @param string $content |
||
| 521 | * |
||
| 522 | * @return $this |
||
| 523 | */ |
||
| 524 | 3 | public function content($content) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Set the template id for template SMS. |
||
| 533 | * |
||
| 534 | * @param mixed $name |
||
| 535 | * @param mixed $tempId |
||
| 536 | * |
||
| 537 | * @return $this |
||
| 538 | */ |
||
| 539 | 3 | public function template($name, $tempId = null) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Set the template data for template SMS. |
||
| 548 | * |
||
| 549 | * @param array $data |
||
| 550 | * |
||
| 551 | * @return $this |
||
| 552 | */ |
||
| 553 | 3 | public function data(array $data) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Set the first agent by name. |
||
| 562 | * |
||
| 563 | * @param string $name |
||
| 564 | * |
||
| 565 | * @return $this |
||
| 566 | */ |
||
| 567 | 3 | public function agent($name) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Start send SMS/voice verify. |
||
| 576 | * |
||
| 577 | * If give a true parameter, this system will immediately start request to send SMS/voice verify whatever whether to use the queue. |
||
| 578 | * if you are already pushed sms instance to the queue, you can recall the method `send()` in queue system without `true` parameter, |
||
| 579 | * so this mechanism in order to make you convenient use the method `send()` in queue system. |
||
| 580 | * |
||
| 581 | * @param bool $immediately |
||
| 582 | * |
||
| 583 | * @return mixed |
||
| 584 | */ |
||
| 585 | 18 | public function send($immediately = false) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Push to the queue by a custom method. |
||
| 604 | * |
||
| 605 | * @throws \Exception | PhpSmsException |
||
| 606 | * |
||
| 607 | * @return mixed |
||
| 608 | */ |
||
| 609 | 3 | public function push() |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Get all the data of SMS/voice verify. |
||
| 627 | * |
||
| 628 | * @param null|string $name |
||
| 629 | * |
||
| 630 | * @return mixed |
||
| 631 | */ |
||
| 632 | 36 | public function getData($name = null) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Overload static method. |
||
| 643 | * |
||
| 644 | * @param string $name |
||
| 645 | * @param array $args |
||
| 646 | * |
||
| 647 | * @throws PhpSmsException |
||
| 648 | */ |
||
| 649 | 9 | public static function __callStatic($name, $args) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Overload method. |
||
| 671 | * |
||
| 672 | * @param string $name |
||
| 673 | * @param array $args |
||
| 674 | * |
||
| 675 | * @throws PhpSmsException |
||
| 676 | * @throws \Exception |
||
| 677 | */ |
||
| 678 | 3 | public function __call($name, $args) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Serialize magic method. |
||
| 689 | * |
||
| 690 | * @return array |
||
| 691 | */ |
||
| 692 | 3 | public function __sleep() |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Deserialize magic method. |
||
| 707 | */ |
||
| 708 | 3 | public function __wakeup() |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Get a closure serializer. |
||
| 723 | * |
||
| 724 | * @return Serializer |
||
| 725 | */ |
||
| 726 | 3 | protected static function getSerializer() |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Serialize or deserialize the agent use scheme. |
||
| 737 | * |
||
| 738 | * @param array $scheme |
||
| 739 | * |
||
| 740 | * @return array |
||
| 741 | */ |
||
| 742 | 3 | protected static function serializeOrDeserializeScheme(array $scheme) |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Serialize the hooks` handlers of balancing task |
||
| 756 | * |
||
| 757 | * @return array |
||
| 758 | */ |
||
| 759 | 3 | protected static function serializeHandlers() |
|
| 771 | |||
| 772 | /** |
||
| 773 | * Reinstall hooks` handlers for balancing task. |
||
| 774 | * |
||
| 775 | * @param array $handlers |
||
| 776 | */ |
||
| 777 | 3 | protected static function reinstallHandlers(array $handlers) |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Serialize/deserialize the specified closure and replace the origin value. |
||
| 792 | * |
||
| 793 | * @param array $options |
||
| 794 | * @param int|string $key |
||
| 795 | */ |
||
| 796 | 3 | protected static function serializeOrDeserializeClosureAndReplace(array &$options, $key) |
|
| 808 | } |
||
| 809 |
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..