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 Channel 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 Channel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Channel extends EventEmitter implements ChannelInterface |
||
| 30 | { |
||
| 31 | use LoopAwareTrait; |
||
| 32 | use RequestRecordStorage; |
||
| 33 | use ResponseRecordStorage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const TYPE_SND = 'SND'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | const TYPE_REQ = 'REQ'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | const BINDER = 1; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | const CONNECTOR = 2; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | const MODE_DEFAULT = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | const MODE_STANDARD = 0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | const MODE_BUFFER_ONLINE = 1; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | const MODE_BUFFER_OFFLINE = 2; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | const MODE_BUFFER = 3; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $name; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var ModelInterface |
||
| 87 | */ |
||
| 88 | protected $model; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var RouterCompositeInterface |
||
| 92 | */ |
||
| 93 | protected $router; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var EncoderInterface |
||
| 97 | */ |
||
| 98 | protected $encoder; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var EventListener[] |
||
| 102 | */ |
||
| 103 | protected $handlers; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $seed; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | protected $counter; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var TimerInterface |
||
| 117 | */ |
||
| 118 | protected $reqsHelperTimer; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var TimerInterface |
||
| 122 | */ |
||
| 123 | protected $repsHelperTimer; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $name |
||
| 127 | * @param ModelInterface $model |
||
| 128 | * @param RouterCompositeInterface $router |
||
| 129 | * @param EncoderInterface $encoder |
||
| 130 | * @param LoopInterface $loop |
||
| 131 | * @throws InstantiationException |
||
| 132 | */ |
||
| 133 | 84 | public function __construct($name, ModelInterface $model, RouterCompositeInterface $router, EncoderInterface $encoder, LoopInterface $loop) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * |
||
| 165 | */ |
||
| 166 | 6 | public function __destruct() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @override |
||
| 189 | * @inheritDoc |
||
| 190 | */ |
||
| 191 | 1 | public function getName() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @override |
||
| 198 | * @inheritDoc |
||
| 199 | */ |
||
| 200 | 1 | public function getModel() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @override |
||
| 207 | * @inheritDoc |
||
| 208 | */ |
||
| 209 | 1 | public function getRouter() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @override |
||
| 216 | * @inheritDoc |
||
| 217 | */ |
||
| 218 | 1 | public function getInput() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @override |
||
| 225 | * @inheritDoc |
||
| 226 | */ |
||
| 227 | 1 | public function getOutput() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @override |
||
| 234 | * @inheritDoc |
||
| 235 | */ |
||
| 236 | 4 | View Code Duplication | public function createProtocol($message = null) |
| 249 | |||
| 250 | /** |
||
| 251 | * @override |
||
| 252 | * @inheritDoc |
||
| 253 | */ |
||
| 254 | 1 | public function onStart(callable $handler) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @override |
||
| 261 | * @inheritDoc |
||
| 262 | */ |
||
| 263 | 1 | public function onStop(callable $handler) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @override |
||
| 270 | * @inheritDoc |
||
| 271 | */ |
||
| 272 | 1 | public function onConnect(callable $handler) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @override |
||
| 279 | * @inheritDoc |
||
| 280 | */ |
||
| 281 | 1 | public function onDisconnect(callable $handler) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @override |
||
| 288 | * @inheritDoc |
||
| 289 | */ |
||
| 290 | 1 | public function onInput(callable $handler) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @override |
||
| 297 | * @inheritDoc |
||
| 298 | */ |
||
| 299 | 1 | public function onOutput(callable $handler) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @override |
||
| 306 | * @inheritDoc |
||
| 307 | */ |
||
| 308 | 1 | public function start() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @override |
||
| 315 | * @inheritDoc |
||
| 316 | */ |
||
| 317 | 1 | public function stop() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @override |
||
| 324 | * @inheritDoc |
||
| 325 | */ |
||
| 326 | 2 | View Code Duplication | public function send($name, $message, $flags = Channel::MODE_DEFAULT, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
| 335 | |||
| 336 | /** |
||
| 337 | * @override |
||
| 338 | * @inheritDoc |
||
| 339 | */ |
||
| 340 | 2 | View Code Duplication | public function push($name, $message, $flags = Channel::MODE_DEFAULT, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
| 349 | |||
| 350 | /** |
||
| 351 | * @override |
||
| 352 | * @inheritDoc |
||
| 353 | */ |
||
| 354 | 5 | View Code Duplication | public function sendAsync($name, $message, $flags = Channel::MODE_DEFAULT) |
| 367 | |||
| 368 | /** |
||
| 369 | * @override |
||
| 370 | * @inheritDoc |
||
| 371 | */ |
||
| 372 | 5 | View Code Duplication | public function pushAsync($name, $message, $flags = Channel::MODE_DEFAULT) |
| 385 | |||
| 386 | /** |
||
| 387 | * @override |
||
| 388 | * @inheritDoc |
||
| 389 | */ |
||
| 390 | 5 | View Code Duplication | public function sendRequest($name, $message, $flags = Channel::MODE_DEFAULT, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
| 403 | |||
| 404 | /** |
||
| 405 | * @override |
||
| 406 | * @inheritDoc |
||
| 407 | */ |
||
| 408 | 5 | View Code Duplication | public function pushRequest($name, $message, $flags = Channel::MODE_DEFAULT, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
| 421 | |||
| 422 | /** |
||
| 423 | * @override |
||
| 424 | * @inheritDoc |
||
| 425 | */ |
||
| 426 | 5 | public function receive($sender, ProtocolInterface $protocol) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @override |
||
| 441 | * @inheritDoc |
||
| 442 | */ |
||
| 443 | 1 | public function pull($sender, ProtocolInterface $protocol) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @override |
||
| 450 | * @inheritDoc |
||
| 451 | */ |
||
| 452 | 1 | public function isStarted() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @override |
||
| 459 | * @inheritDoc |
||
| 460 | */ |
||
| 461 | 1 | public function isStopped() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @override |
||
| 468 | * @inheritDoc |
||
| 469 | */ |
||
| 470 | 2 | public function isConnected($name) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @override |
||
| 489 | * @inheritDoc |
||
| 490 | */ |
||
| 491 | 2 | public function getConnected() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @override |
||
| 498 | * @inheritDoc |
||
| 499 | */ |
||
| 500 | 1 | public function filterConnected($pattern) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @override |
||
| 507 | * @inheritDoc |
||
| 508 | */ |
||
| 509 | 1 | View Code Duplication | protected function handleSendAsync($name, $message, $flags = Channel::MODE_DEFAULT) |
| 522 | |||
| 523 | /** |
||
| 524 | * @override |
||
| 525 | * @inheritDoc |
||
| 526 | */ |
||
| 527 | protected function handlePushAsync($name, $message, $flags = Channel::MODE_DEFAULT) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @override |
||
| 555 | * @inheritDoc |
||
| 556 | */ |
||
| 557 | 1 | protected function handleSendRequest($name, $message, $flags = Channel::MODE_DEFAULT, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * @override |
||
| 573 | * @inheritDoc |
||
| 574 | */ |
||
| 575 | protected function handlePushRequest($name, $message, $flags = Channel::MODE_DEFAULT, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @override |
||
| 613 | * @inheritDoc |
||
| 614 | */ |
||
| 615 | public function handleReceive($sender, $multipartMessage) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param ProtocolInterface |
||
| 634 | * @return bool |
||
| 635 | */ |
||
| 636 | protected function handleReceiveRequest(ProtocolInterface $protocol) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param ProtocolInterface $protocol |
||
| 658 | * @return bool |
||
| 659 | */ |
||
| 660 | protected function handleReceiveResponse(ProtocolInterface $protocol) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @return string |
||
| 690 | */ |
||
| 691 | 7 | protected function genID() |
|
| 695 | |||
| 696 | /** |
||
| 697 | * @return float |
||
| 698 | */ |
||
| 699 | 7 | protected function getTime() |
|
| 703 | |||
| 704 | /** |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | 9 | View Code Duplication | protected function getNextSuffix() |
| 717 | |||
| 718 | /** |
||
| 719 | * @param string|string[] $message |
||
| 720 | * @return ProtocolInterface |
||
| 721 | */ |
||
| 722 | 5 | protected function createMessageProtocol($message) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * |
||
| 749 | */ |
||
| 750 | 84 | private function registerPeriodicTimers() |
|
| 767 | |||
| 768 | /** |
||
| 769 | * |
||
| 770 | */ |
||
| 771 | 6 | private function unregisterPeriodicTimers() |
|
| 783 | |||
| 784 | /** |
||
| 785 | * |
||
| 786 | */ |
||
| 787 | 84 | private function registerEvents() |
|
| 792 | |||
| 793 | /** |
||
| 794 | * |
||
| 795 | */ |
||
| 796 | 6 | private function unregisterEvents() |
|
| 803 | } |
||
| 804 |
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.