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