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 Socket 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 Socket, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Socket extends BaseEventEmitter implements ModelInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | const COMMAND_HEARTBEAT = 1; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | const COMMAND_MESSAGE = 2; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | const SEND_STATUS_DROPPED = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | const SEND_STATUS_SUCCEEDED = 1; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | const SEND_STATUS_BUFFERED = 2; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var LoopInterface |
||
| 48 | */ |
||
| 49 | protected $loop; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $id; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $endpoint; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $type; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string[] |
||
| 68 | */ |
||
| 69 | protected $hosts; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string[] |
||
| 73 | */ |
||
| 74 | protected $flags; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var mixed[] |
||
| 78 | */ |
||
| 79 | protected $options; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | protected $isConnected; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var SocketInterface|SocketListenerInterface|null |
||
| 88 | */ |
||
| 89 | protected $socket; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var ConnectionPool |
||
| 93 | */ |
||
| 94 | protected $connectionPool; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Buffer |
||
| 98 | */ |
||
| 99 | protected $offlineBuffer; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var Buffer |
||
| 103 | */ |
||
| 104 | protected $onlineBuffer; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var string[] |
||
| 108 | */ |
||
| 109 | protected $frameBuffer; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var TimerInterface |
||
| 113 | */ |
||
| 114 | private $hTimer; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var TimerInterface |
||
| 118 | */ |
||
| 119 | private $rTimer; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param LoopInterface $loop |
||
| 123 | * @param string[] $params |
||
| 124 | */ |
||
| 125 | 40 | public function __construct(LoopInterface $loop, $params) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * |
||
| 166 | */ |
||
| 167 | public function __destruct() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @override |
||
| 191 | * @inheritDoc |
||
| 192 | */ |
||
| 193 | 32 | public function start($blockEvent = false) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @override |
||
| 229 | * @inheritDoc |
||
| 230 | */ |
||
| 231 | 32 | public function stop($blockEvent = false) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @override |
||
| 258 | * @inheritDoc |
||
| 259 | */ |
||
| 260 | 24 | public function unicast($id, $message, $flags = Channel::MODE_STANDARD) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @override |
||
| 274 | * @inheritDoc |
||
| 275 | */ |
||
| 276 | 2 | public function broadcast($message) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @override |
||
| 296 | * @inheritDoc |
||
| 297 | */ |
||
| 298 | 32 | public function isStarted() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @override |
||
| 305 | * @inheritDoc |
||
| 306 | */ |
||
| 307 | 1 | public function isStopped() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @override |
||
| 314 | * @inheritDoc |
||
| 315 | */ |
||
| 316 | 1 | public function isConnected($id) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @override |
||
| 323 | * @inheritDoc |
||
| 324 | */ |
||
| 325 | 3 | public function getConnected() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set connection statically to be marked as online until specific timestamp. |
||
| 332 | * |
||
| 333 | * @param string $id |
||
| 334 | * @param float $until |
||
| 335 | */ |
||
| 336 | public function markConnectionOnline($id, $until) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set connection statically to be marked always as online. |
||
| 343 | * |
||
| 344 | * @param string $id |
||
| 345 | */ |
||
| 346 | public function markConnectionPersistent($id) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $message |
||
| 353 | * @return string[] |
||
| 354 | */ |
||
| 355 | 30 | View Code Duplication | protected function parseBinderMessage($message) |
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $message |
||
| 368 | * @return string[] |
||
| 369 | */ |
||
| 370 | 29 | View Code Duplication | protected function parseConnectorMessage($message) |
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $id |
||
| 383 | * @param string $type |
||
| 384 | * @return string[] |
||
| 385 | */ |
||
| 386 | 30 | protected function prepareBinderMessage($id, $type) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $id |
||
| 393 | * @param string $type |
||
| 394 | * @return string[] |
||
| 395 | */ |
||
| 396 | 30 | protected function prepareConnectorMessage($id, $type) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @return SocketListenerInterface |
||
| 403 | */ |
||
| 404 | 34 | protected function createBinder() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @return SocketInterface |
||
| 424 | */ |
||
| 425 | 29 | protected function createConnector() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * |
||
| 446 | */ |
||
| 447 | 32 | protected function destroyBinder() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * |
||
| 455 | */ |
||
| 456 | 29 | protected function destroyConnector() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $event |
||
| 464 | * @param callable $callback |
||
| 465 | */ |
||
| 466 | protected function setEventListener($event, callable $callback) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $event |
||
| 476 | * @param callable $callback |
||
| 477 | */ |
||
| 478 | protected function removeEventListener($event, callable $callback) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return Buffer |
||
| 488 | */ |
||
| 489 | 40 | protected function getBuffer() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @return ConnectionPool |
||
| 496 | */ |
||
| 497 | 40 | protected function getConnectionPool() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * @param SocketInterface $client |
||
| 504 | * @param string $data |
||
| 505 | */ |
||
| 506 | 29 | public function onData(SocketInterface $client, $data) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * @param SocketInterface $client |
||
| 544 | * @param string $message |
||
| 545 | */ |
||
| 546 | 29 | private function onMessage(SocketInterface $client, $message) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * @param Connection $conn |
||
| 574 | * @param string[] $message |
||
| 575 | */ |
||
| 576 | 25 | private function onRecvMessage(Connection $conn, $message) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * @param Connection $conn |
||
| 585 | */ |
||
| 586 | 29 | private function onRecvHeartbeat(Connection $conn) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * @param Connection $conn |
||
| 593 | * @param $message[] |
||
| 594 | * @return mixed |
||
| 595 | */ |
||
| 596 | 25 | private function recvMessage(Connection $conn, $message) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * @param Connection $conn |
||
| 603 | */ |
||
| 604 | 29 | private function recvHeartbeat(Connection $conn) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | 32 | private function startConnection() |
|
| 670 | |||
| 671 | /** |
||
| 672 | * @return bool |
||
| 673 | */ |
||
| 674 | 32 | private function stopConnection() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * @param string $id |
||
| 712 | * @param string $type |
||
| 713 | * @param string $message |
||
| 714 | * @return null|string |
||
| 715 | */ |
||
| 716 | 29 | private function getFrame($id, $type, $message) |
|
| 751 | |||
| 752 | /** |
||
| 753 | * @param string $id |
||
| 754 | * @param string $type |
||
| 755 | * @param string|string[] $message |
||
| 756 | * @param int $flags |
||
| 757 | * @return bool |
||
| 758 | */ |
||
| 759 | 29 | private function sendMessage($id, $type, $message = null, $flags = Channel::MODE_STANDARD) |
|
| 797 | |||
| 798 | /** |
||
| 799 | * @param string $id |
||
| 800 | * @param string $data |
||
| 801 | * @return bool |
||
| 802 | */ |
||
| 803 | 29 | private function writeData($id, $data) |
|
| 833 | |||
| 834 | /** |
||
| 835 | * @param string $id |
||
| 836 | * @return bool |
||
| 837 | */ |
||
| 838 | 29 | private function heartbeat($id) |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Start heartbeat. |
||
| 850 | * |
||
| 851 | * Heartbeat mechanisms is used to identify online and offline sockets. |
||
| 852 | */ |
||
| 853 | 32 | private function startHeartbeat() |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Stop hearbeat. |
||
| 877 | */ |
||
| 878 | 32 | private function stopHeartbeat() |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Clear connection pool. |
||
| 889 | */ |
||
| 890 | 32 | private function clearConnectionPool() |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Start time register. |
||
| 902 | * |
||
| 903 | * Time register purpose is to cyclically increase timestamp representing last time of tick of event loop. This |
||
| 904 | * method allows model to not mark external sockets wrongly as offline because of its own heavy load. |
||
| 905 | */ |
||
| 906 | 32 | private function startTimeRegister() |
|
| 919 | |||
| 920 | /** |
||
| 921 | * Stop time register. |
||
| 922 | */ |
||
| 923 | 32 | private function stopTimeRegister() |
|
| 932 | } |
||
| 933 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.