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 | /** |
||
| 17 | * Send task`s name. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | const TASK = 'PhpSms'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * SMS agents(service providers), |
||
| 25 | * they are instances of class [Toplan\PhpSms\Agent]. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected static $agents = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The enabled agents` name. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected static $agentsName = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The enabled agents` config info. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected static $agentsConfig = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Whether to use queue. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected static $enableQueue = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * How to push to queue. |
||
| 54 | * |
||
| 55 | * @var \Closure |
||
| 56 | */ |
||
| 57 | protected static $howToUseQueue = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The enable hooks for balance task. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected static $enableHooks = [ |
||
| 65 | 'beforeRun', |
||
| 66 | 'beforeDriverRun', |
||
| 67 | 'afterDriverRun', |
||
| 68 | 'afterRun', |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * An instance of class [SuperClosure\Serializer] |
||
| 73 | * |
||
| 74 | * @var Serializer |
||
| 75 | */ |
||
| 76 | protected static $serializer = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * SMS(or voice verify) data container. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $smsData = [ |
||
| 84 | 'to' => null, |
||
| 85 | 'templates' => [], |
||
| 86 | 'content' => null, |
||
| 87 | 'templateData' => [], |
||
| 88 | 'voiceCode' => null, |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The name of first agent. |
||
| 93 | * |
||
| 94 | * @var string|null |
||
| 95 | */ |
||
| 96 | protected $firstAgent = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Whether the current instance has already pushed to queue. |
||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | protected $pushedToQueue = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Status container, |
||
| 107 | * store config data before serialize a instance(before enqueue). |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $_status_before_enqueue_ = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor |
||
| 115 | * |
||
| 116 | * @param bool $autoBoot |
||
| 117 | */ |
||
| 118 | 6 | public function __construct($autoBoot = true) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Boot balanced send task. |
||
| 127 | */ |
||
| 128 | 9 | public static function bootstrap() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get or generate a balanced task instance for send SMS/voice verify. |
||
| 139 | * |
||
| 140 | * @return object |
||
| 141 | */ |
||
| 142 | 18 | public static function getTask() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Configuration. |
||
| 153 | */ |
||
| 154 | 6 | protected static function configuration() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Try to read and set enable agents` name from config file. |
||
| 166 | * |
||
| 167 | * @param array $config |
||
| 168 | */ |
||
| 169 | 3 | protected static function ReadEnableAgentsFromConfig(&$config) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Try to read and set enabled agents` config from config file. |
||
| 178 | * |
||
| 179 | * @param array $config |
||
| 180 | */ |
||
| 181 | 6 | protected static function ReadAgentsConfigFromConfig(&$config) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Config values validator. |
||
| 197 | * |
||
| 198 | * @throws PhpSmsException |
||
| 199 | */ |
||
| 200 | 6 | protected static function configValidator() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Create drivers of the balanced task. |
||
| 209 | * |
||
| 210 | * @param $task |
||
| 211 | */ |
||
| 212 | 18 | protected static function createDrivers($task) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Parse the enabled agents` scheduling config data. |
||
| 249 | * 解析可用代理器的数组模式的调度配置 |
||
| 250 | * |
||
| 251 | * @param array $options |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | 3 | protected static function parseAgentArrayOptions(array $options) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Pull the character option data from the scheduling config. |
||
| 268 | * |
||
| 269 | * @param array $options |
||
| 270 | * @param string $name |
||
| 271 | * |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | 3 | protected static function pullAgentOptionByName(array &$options, $name) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Get agent config data by name. |
||
| 287 | * |
||
| 288 | * @param string $name |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | 6 | protected static function getAgentConfigData($name) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get a sms agent instance by agent name, |
||
| 299 | * if null, will try to create a new agent instance. |
||
| 300 | * |
||
| 301 | * @param string $name |
||
| 302 | * @param array $configData |
||
| 303 | * |
||
| 304 | * @throws PhpSmsException |
||
| 305 | * |
||
| 306 | * @return mixed |
||
| 307 | */ |
||
| 308 | 21 | public static function getSmsAgent($name, array $configData) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set enable agents. |
||
| 332 | * |
||
| 333 | * @param mixed $agentName |
||
| 334 | * @param mixed $options |
||
| 335 | */ |
||
| 336 | 6 | public static function enable($agentName, $options = null) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Set config info by agent name. |
||
| 353 | * |
||
| 354 | * @param array|string $agentName |
||
| 355 | * @param array $config |
||
| 356 | * |
||
| 357 | * @throws PhpSmsException |
||
| 358 | */ |
||
| 359 | 6 | public static function agents($agentName, array $config = []) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get the enabled agents` name. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | 12 | public static function getEnableAgents() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get the enabled agents` config info. |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | 12 | public static function getAgentsConfig() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Tear down enable agents and prepare to create and start a new balance task, |
||
| 395 | * so before do it must destroy old task instance. |
||
| 396 | */ |
||
| 397 | 6 | public static function cleanEnableAgents() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Tear down agents config and prepare to create and start a new balance task, |
||
| 405 | * so before do it must destroy old task instance. |
||
| 406 | */ |
||
| 407 | 3 | public static function cleanAgentsConfig() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Create a sms instance which send SMS, |
||
| 415 | * and set SMS templates or content. |
||
| 416 | * |
||
| 417 | * @param mixed $agentName |
||
| 418 | * @param mixed $tempId |
||
| 419 | * |
||
| 420 | * @return Sms |
||
| 421 | */ |
||
| 422 | public static function make($agentName = null, $tempId = null) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Create a sms instance which send voice verify, |
||
| 440 | * and set verify code. |
||
| 441 | * |
||
| 442 | * @param string|int $code |
||
| 443 | * |
||
| 444 | * @return Sms |
||
| 445 | */ |
||
| 446 | 3 | public static function voice($code) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Set whether to use queue, and define how to use queue. |
||
| 456 | * |
||
| 457 | * @param mixed $enable |
||
| 458 | * @param mixed $handler |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | 3 | public static function queue($enable = null, $handler = null) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Set the receiver`s mobile number. |
||
| 481 | * |
||
| 482 | * @param string $mobile |
||
| 483 | * |
||
| 484 | * @return $this |
||
| 485 | */ |
||
| 486 | 6 | public function to($mobile) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Set content for content SMS. |
||
| 495 | * |
||
| 496 | * @param string $content |
||
| 497 | * |
||
| 498 | * @return $this |
||
| 499 | */ |
||
| 500 | 3 | public function content($content) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Set template id for template SMS. |
||
| 509 | * |
||
| 510 | * @param mixed $agentName |
||
| 511 | * @param mixed $tempId |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | 3 | public function template($agentName, $tempId = null) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Set template data for template SMS. |
||
| 533 | * |
||
| 534 | * @param array $data |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | 3 | public function data(array $data) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Set the first agent by agent`s name. |
||
| 547 | * |
||
| 548 | * @param string $name |
||
| 549 | * |
||
| 550 | * @return $this |
||
| 551 | */ |
||
| 552 | 3 | public function agent($name) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Start send SMS/voice verify. |
||
| 561 | * |
||
| 562 | * If give a true parameter, will immediately start whatever whether to use queue. |
||
| 563 | * if you are already pushed sms instance to queue, you can recall the method `send()` in queue job without `true` parameter, |
||
| 564 | * so this mechanism in order to make you convenient use the method `send()` in queue system. |
||
| 565 | * |
||
| 566 | * @param bool $immediately |
||
| 567 | * |
||
| 568 | * @return mixed |
||
| 569 | */ |
||
| 570 | 18 | public function send($immediately = false) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Push to queue by custom method. |
||
| 589 | * |
||
| 590 | * @throws \Exception | PhpSmsException |
||
| 591 | * |
||
| 592 | * @return mixed |
||
| 593 | */ |
||
| 594 | 3 | public function push() |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Get all the data of SMS/voice verify. |
||
| 612 | * |
||
| 613 | * @param null|string $name |
||
| 614 | * |
||
| 615 | * @return mixed |
||
| 616 | */ |
||
| 617 | 36 | public function getData($name = null) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Overload static method. |
||
| 628 | * |
||
| 629 | * @param string $name |
||
| 630 | * @param array $args |
||
| 631 | * |
||
| 632 | * @throws PhpSmsException |
||
| 633 | */ |
||
| 634 | 9 | public static function __callStatic($name, $args) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Overload method. |
||
| 656 | * |
||
| 657 | * @param string $name |
||
| 658 | * @param array $args |
||
| 659 | * |
||
| 660 | * @throws PhpSmsException |
||
| 661 | * @throws \Exception |
||
| 662 | */ |
||
| 663 | 3 | public function __call($name, $args) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Serialize magic method, |
||
| 674 | * store current sms instance status. |
||
| 675 | * |
||
| 676 | * @return array |
||
| 677 | */ |
||
| 678 | 3 | public function __sleep() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Unserialize magic method, |
||
| 693 | * note: the force bootstrap must before reinstall handlers! |
||
| 694 | */ |
||
| 695 | 3 | public function __wakeup() |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Get a closure serializer. |
||
| 710 | * |
||
| 711 | * @return Serializer |
||
| 712 | */ |
||
| 713 | 3 | public static function getSerializer() |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Serialize enabled agents. |
||
| 724 | * |
||
| 725 | * @return array |
||
| 726 | */ |
||
| 727 | 3 | protected static function serializeEnableAgents() |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Unserialize enabled agents. |
||
| 742 | * |
||
| 743 | * @param array $serialized |
||
| 744 | * |
||
| 745 | * @return mixed |
||
| 746 | */ |
||
| 747 | 3 | protected static function unserializeEnableAgents(array $serialized) |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Serialize character closure value of a array and replace origin value. |
||
| 761 | * |
||
| 762 | * @param array $options |
||
| 763 | * @param string $key |
||
| 764 | */ |
||
| 765 | 3 | protected static function serializeClosureAndReplace(array &$options, $key) |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Unserialize character string of a array to closure and replace origin value. |
||
| 775 | * |
||
| 776 | * @param array $options |
||
| 777 | * @param string $key |
||
| 778 | */ |
||
| 779 | 3 | protected static function unserializeToClosureAndReplace(array &$options, $key) |
|
| 786 | |||
| 787 | /** |
||
| 788 | * Serialize these hooks` handlers: |
||
| 789 | * 'beforeRun','beforeDriverRun','afterDriverRun','afterRun'. |
||
| 790 | * |
||
| 791 | * @return array |
||
| 792 | */ |
||
| 793 | 3 | protected static function serializeHandlers() |
|
| 810 | |||
| 811 | /** |
||
| 812 | * Reinstall balance task hooks` handlers by serialized handlers. |
||
| 813 | * |
||
| 814 | * @param array $handlers |
||
| 815 | */ |
||
| 816 | 3 | protected static function reinstallHandlers(array $handlers) |
|
| 829 | } |
||
| 830 |
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..