Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * The default name of balancing task. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | const TASK = 'PhpSms'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The 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` configuration information. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected static $agentsConfig = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Whether to use the queue. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected static $enableQueue = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * How to use the queue. |
||
| 54 | * |
||
| 55 | * @var \Closure |
||
| 56 | */ |
||
| 57 | protected static $howToUseQueue = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The enable hooks for balancing 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] use for serialization closures. |
||
| 73 | * |
||
| 74 | * @var Serializer |
||
| 75 | */ |
||
| 76 | protected static $serializer = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * SMS/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 the queue system. |
||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | protected $pushedToQueue = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Status container, |
||
| 107 | * store some configuration information before serialize current instance(before enqueue). |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $_status_before_enqueue_ = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor |
||
| 115 | * |
||
| 116 | * @param bool $autoBoot |
||
| 117 | */ |
||
| 118 | public function __construct($autoBoot = true) |
||
| 119 | 6 | { |
|
| 120 | if ($autoBoot) { |
||
| 121 | 6 | self::bootstrap(); |
|
| 122 | 3 | } |
|
| 123 | 2 | } |
|
| 124 | 6 | ||
| 125 | /** |
||
| 126 | * Boot balancing task for send SMS/voice verify. |
||
| 127 | */ |
||
| 128 | public static function bootstrap() |
||
| 139 | 6 | ||
| 140 | /** |
||
| 141 | * Get or generate a balancing task instance for send SMS/voice verify. |
||
| 142 | * |
||
| 143 | * @return Task |
||
| 144 | */ |
||
| 145 | public static function getTask() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Configuration. |
||
| 156 | */ |
||
| 157 | protected static function configuration() |
||
| 167 | 6 | ||
| 168 | /** |
||
| 169 | * Try to read enable agents` name from config file. |
||
| 170 | * |
||
| 171 | * @param array $config |
||
| 172 | */ |
||
| 173 | protected static function initEnableAgents(array &$config) |
||
| 179 | 3 | ||
| 180 | /** |
||
| 181 | * Try to initialize the specified agents` configuration information. |
||
| 182 | * |
||
| 183 | * @param array $agents |
||
| 184 | * @param array $config |
||
| 185 | */ |
||
| 186 | protected static function initAgentsConfig(array $agents, array &$config) |
||
| 198 | 3 | ||
| 199 | /** |
||
| 200 | * validate configuration. |
||
| 201 | * |
||
| 202 | * @throws PhpSmsException |
||
| 203 | */ |
||
| 204 | protected static function validateConfig() |
||
| 210 | 6 | ||
| 211 | /** |
||
| 212 | * Create drivers for the balancing task. |
||
| 213 | * |
||
| 214 | * @param Task $task |
||
| 215 | */ |
||
| 216 | protected static function createDrivers(Task $task) |
||
| 250 | 3 | ||
| 251 | /** |
||
| 252 | * Parsing scheduling configuration. |
||
| 253 | * 解析代理器的数组模式的调度配置 |
||
| 254 | * |
||
| 255 | * @param array $options |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | protected static function parseAgentArrayOptions(array $options) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Pull the value of the specified option out of the scheduling configuration. |
||
| 272 | * |
||
| 273 | * @param array $options |
||
| 274 | * @param string $name |
||
| 275 | * |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | protected static function pullAgentOptionByName(array &$options, $name) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get agent configuration information by name. |
||
| 291 | * |
||
| 292 | * @param string $name |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | protected static function getAgentConfigData($name) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get a sms agent instance by agent name, |
||
| 303 | * if null, will try to create a new agent instance. |
||
| 304 | * |
||
| 305 | * @param string $name |
||
| 306 | * @param array $configData |
||
| 307 | * |
||
| 308 | * @throws PhpSmsException |
||
| 309 | * |
||
| 310 | * @return mixed |
||
| 311 | */ |
||
| 312 | public static function getSmsAgent($name, array $configData) |
||
| 313 | 21 | { |
|
| 314 | if (!isset(self::$agents[$name])) { |
||
| 315 | 21 | $configData['name'] = $name; |
|
| 316 | 6 | $className = isset($configData['agentClass']) ? $configData['agentClass'] : ('Toplan\\PhpSms\\' . $name . 'Agent'); |
|
| 317 | 6 | if ((isset($configData['sendSms']) && is_callable($configData['sendSms'])) || |
|
| 318 | 6 | (isset($configData['voiceVerify']) && is_callable($configData['voiceVerify']))) { |
|
| 319 | 6 | //创建寄生代理器 |
|
| 320 | $configData['agentClass'] = ''; |
||
| 321 | 3 | self::$agents[$name] = new ParasiticAgent($configData); |
|
| 322 | 3 | } elseif (class_exists($className)) { |
|
| 323 | 5 | //创建新代理器 |
|
| 324 | self::$agents[$name] = new $className($configData); |
||
| 325 | 3 | } else { |
|
| 326 | 2 | //无代理器可用 |
|
| 327 | throw new PhpSmsException("Do not support [$name] agent."); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | 4 | ||
| 331 | return self::$agents[$name]; |
||
| 332 | 21 | } |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set enable agents. |
||
| 336 | * |
||
| 337 | * @param mixed $agentName |
||
| 338 | * @param mixed $options |
||
| 339 | */ |
||
| 340 | public static function enable($agentName, $options = null) |
||
| 354 | 6 | ||
| 355 | /** |
||
| 356 | * Set configuration information by agent name. |
||
| 357 | * |
||
| 358 | * @param array|string $agentName |
||
| 359 | * @param array $config |
||
| 360 | * |
||
| 361 | * @throws PhpSmsException |
||
| 362 | */ |
||
| 363 | public static function agents($agentName, array $config = []) |
||
| 376 | 6 | ||
| 377 | /** |
||
| 378 | * Get the enabled agents` name. |
||
| 379 | * |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public static function getEnableAgents() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the enabled agents` configuration information. |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | public static function getAgentsConfig() |
||
| 393 | 12 | { |
|
| 394 | return self::$agentsConfig; |
||
| 395 | 12 | } |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Tear down enable agent and prepare to create and start a new balancing task, |
||
| 399 | * so before do it must destroy old task instance. |
||
| 400 | */ |
||
| 401 | public static function cleanEnableAgents() |
||
| 406 | 6 | ||
| 407 | /** |
||
| 408 | * Tear down agent config and prepare to create and start a new balancing task, |
||
| 409 | * so before do it must destroy old task instance. |
||
| 410 | */ |
||
| 411 | public static function cleanAgentsConfig() |
||
| 416 | 3 | ||
| 417 | /** |
||
| 418 | * Create a sms instance send SMS, |
||
| 419 | * your can also set SMS templates or content at the same time. |
||
| 420 | * |
||
| 421 | * @param mixed $agentName |
||
| 422 | * @param mixed $tempId |
||
| 423 | * |
||
| 424 | * @return Sms |
||
| 425 | */ |
||
| 426 | public static function make($agentName = null, $tempId = null) |
||
| 427 | { |
||
| 428 | $sms = new self(); |
||
| 429 | if (is_array($agentName)) { |
||
| 430 | $sms->template($agentName); |
||
| 431 | } elseif ($agentName && is_string($agentName)) { |
||
| 432 | if ($tempId === null) { |
||
| 433 | $sms->content($agentName); |
||
| 434 | } elseif (is_string("$tempId")) { |
||
| 435 | $sms->template($agentName, $tempId); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | return $sms; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Create a sms instance send voice verify, |
||
| 444 | * your can also set verify code at the same time. |
||
| 445 | * |
||
| 446 | * @param string|int $code |
||
| 447 | * |
||
| 448 | * @return Sms |
||
| 449 | */ |
||
| 450 | public static function voice($code) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set whether to use the queue system, and define how to use it. |
||
| 460 | * |
||
| 461 | * @param mixed $enable |
||
| 462 | * @param mixed $handler |
||
| 463 | * |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public static function queue($enable = null, $handler = null) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Set the recipient`s mobile number. |
||
| 485 | * |
||
| 486 | * @param string $mobile |
||
| 487 | * |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | public function to($mobile) |
||
| 491 | 6 | { |
|
| 492 | $this->smsData['to'] = $mobile; |
||
| 493 | 6 | ||
| 494 | return $this; |
||
| 495 | 6 | } |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Set the content for content SMS. |
||
| 499 | * |
||
| 500 | * @param string $content |
||
| 501 | * |
||
| 502 | * @return $this |
||
| 503 | */ |
||
| 504 | public function content($content) |
||
| 505 | 3 | { |
|
| 506 | $this->smsData['content'] = trim((string) $content); |
||
| 507 | 3 | ||
| 508 | return $this; |
||
| 509 | 3 | } |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Set the template id for template SMS. |
||
| 513 | * |
||
| 514 | * @param mixed $agentName |
||
| 515 | * @param mixed $tempId |
||
| 516 | * |
||
| 517 | * @return $this |
||
| 518 | */ |
||
| 519 | public function template($agentName, $tempId = null) |
||
| 520 | 3 | { |
|
| 521 | if (is_array($agentName)) { |
||
| 522 | 3 | foreach ($agentName as $k => $v) { |
|
| 523 | 3 | $this->template($k, $v); |
|
| 524 | 3 | } |
|
| 525 | 2 | } elseif ($agentName && $tempId) { |
|
| 526 | 3 | if (!isset($this->smsData['templates']) || !is_array($this->smsData['templates'])) { |
|
| 527 | 3 | $this->smsData['templates'] = []; |
|
| 528 | } |
||
| 529 | $this->smsData['templates']["$agentName"] = $tempId; |
||
| 530 | 3 | } |
|
| 531 | 2 | ||
| 532 | return $this; |
||
| 533 | 3 | } |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Set the template data for template SMS. |
||
| 537 | * |
||
| 538 | * @param array $data |
||
| 539 | * |
||
| 540 | * @return $this |
||
| 541 | */ |
||
| 542 | public function data(array $data) |
||
| 543 | 3 | { |
|
| 544 | $this->smsData['templateData'] = $data; |
||
| 545 | 3 | ||
| 546 | return $this; |
||
| 547 | 3 | } |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Set the first agent by name. |
||
| 551 | * |
||
| 552 | * @param string $name |
||
| 553 | * |
||
| 554 | * @return $this |
||
| 555 | */ |
||
| 556 | public function agent($name) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Start send SMS/voice verify. |
||
| 565 | * |
||
| 566 | * If give a true parameter, this system will immediately start request to send SMS/voice verify whatever whether to use the queue. |
||
| 567 | * if you are already pushed sms instance to the queue, you can recall the method `send()` in queue system without `true` parameter, |
||
| 568 | * so this mechanism in order to make you convenient use the method `send()` in queue system. |
||
| 569 | * |
||
| 570 | * @param bool $immediately |
||
| 571 | * |
||
| 572 | * @return mixed |
||
| 573 | */ |
||
| 574 | public function send($immediately = false) |
||
| 575 | 18 | { |
|
| 576 | if (!self::$enableQueue || $this->pushedToQueue) { |
||
| 577 | 18 | $immediately = true; |
|
| 578 | 18 | } |
|
| 579 | 12 | if ($immediately) { |
|
| 580 | 18 | $result = Balancer::run(self::TASK, [ |
|
| 581 | 18 | 'data' => $this->getData(), |
|
| 582 | 18 | 'driver' => $this->firstAgent, |
|
| 583 | 18 | ]); |
|
| 584 | 12 | } else { |
|
| 585 | 12 | $result = $this->push(); |
|
| 586 | 3 | } |
|
| 587 | |||
| 588 | return $result; |
||
| 589 | 18 | } |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Push to the queue by a custom method. |
||
| 593 | * |
||
| 594 | * @throws \Exception | PhpSmsException |
||
| 595 | * |
||
| 596 | * @return mixed |
||
| 597 | */ |
||
| 598 | public function push() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get all the data of SMS/voice verify. |
||
| 616 | * |
||
| 617 | * @param null|string $name |
||
| 618 | * |
||
| 619 | * @return mixed |
||
| 620 | */ |
||
| 621 | public function getData($name = null) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Overload static method. |
||
| 632 | * |
||
| 633 | * @param string $name |
||
| 634 | * @param array $args |
||
| 635 | * |
||
| 636 | * @throws PhpSmsException |
||
| 637 | */ |
||
| 638 | public static function __callStatic($name, $args) |
||
| 639 | 9 | { |
|
| 640 | $name = $name === 'beforeSend' ? 'beforeRun' : $name; |
||
| 641 | 9 | $name = $name === 'afterSend' ? 'afterRun' : $name; |
|
| 642 | 9 | $name = $name === 'beforeAgentSend' ? 'beforeDriverRun' : $name; |
|
| 643 | 9 | $name = $name === 'afterAgentSend' ? 'afterDriverRun' : $name; |
|
| 644 | 9 | if (in_array($name, self::$enableHooks)) { |
|
| 645 | 9 | $handler = $args[0]; |
|
| 646 | 9 | $override = isset($args[1]) ? (bool) $args[1] : false; |
|
| 647 | 9 | if (is_callable($handler)) { |
|
| 648 | 9 | $task = self::getTask(); |
|
| 649 | 9 | $task->hook($name, $handler, $override); |
|
| 650 | 9 | } else { |
|
| 651 | 6 | throw new PhpSmsException("Please give method $name() a callable parameter"); |
|
| 652 | 3 | } |
|
| 653 | } else { |
||
| 654 | 6 | throw new PhpSmsException("Dont find method $name()"); |
|
| 655 | } |
||
| 656 | } |
||
| 657 | 9 | ||
| 658 | /** |
||
| 659 | * Overload method. |
||
| 660 | * |
||
| 661 | * @param string $name |
||
| 662 | * @param array $args |
||
| 663 | * |
||
| 664 | * @throws PhpSmsException |
||
| 665 | * @throws \Exception |
||
| 666 | */ |
||
| 667 | public function __call($name, $args) |
||
| 675 | 3 | ||
| 676 | /** |
||
| 677 | * Serialize magic method. |
||
| 678 | * |
||
| 679 | * @return array |
||
| 680 | */ |
||
| 681 | public function __sleep() |
||
| 682 | { |
||
| 683 | 3 | try { |
|
| 684 | $this->_status_before_enqueue_['enableAgents'] = self::serializeEnableAgents(); |
||
| 685 | $this->_status_before_enqueue_['agentsConfig'] = self::getAgentsConfig(); |
||
| 693 | 3 | ||
| 694 | /** |
||
| 695 | * Deserialize magic method. |
||
| 696 | */ |
||
| 697 | public function __wakeup() |
||
| 709 | 3 | ||
| 710 | 3 | /** |
|
| 711 | 3 | * Get a closure serializer. |
|
| 712 | * |
||
| 713 | * @return Serializer |
||
| 714 | */ |
||
| 715 | public static function getSerializer() |
||
| 723 | |||
| 724 | 3 | /** |
|
| 725 | * Serialize the configuration information of enabled agents. |
||
| 726 | * |
||
| 727 | * @return array |
||
| 728 | */ |
||
| 729 | protected static function serializeEnableAgents() |
||
| 741 | |||
| 742 | 3 | /** |
|
| 743 | * Deserialize the configuration information of enabled agents. |
||
| 744 | * |
||
| 745 | * @param array $serialized |
||
| 746 | * |
||
| 747 | * @return mixed |
||
| 748 | */ |
||
| 749 | protected static function deserializeEnableAgents(array $serialized) |
||
| 760 | |||
| 761 | 3 | /** |
|
| 762 | * Serialize the hooks` handlers of balancing task |
||
| 763 | * |
||
| 764 | * @return array |
||
| 765 | */ |
||
| 766 | protected static function serializeHandlers() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Reinstall hooks` handlers for balancing task. |
||
| 781 | * |
||
| 782 | * @param array $handlers |
||
| 783 | */ |
||
| 784 | 3 | protected static function reinstallHandlers(array $handlers) |
|
| 796 | |||
| 797 | /** |
||
| 798 | 3 | * Serialize the specified closure and replace the origin value. |
|
| 799 | * |
||
| 800 | 3 | * @param array $options |
|
| 801 | 3 | * @param int|string $key |
|
| 802 | 3 | */ |
|
| 803 | 3 | View Code Duplication | protected static function serializeClosureAndReplace(array &$options, $key) |
| 810 | 2 | ||
| 811 | 2 | /** |
|
| 812 | * Deserialize the specified closure and replace the origin value. |
||
| 813 | 3 | * |
|
| 814 | * @param array $options |
||
| 815 | * @param int|string $key |
||
| 816 | */ |
||
| 817 | View Code Duplication | protected static function deserializeClosureAndReplace(array &$options, $key) |
|
| 824 | } |
||
| 825 |
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..