Complex classes like Server 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 Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Server implements MessageComponentInterface { |
||
| 31 | use |
||
| 32 | Singleton; |
||
| 33 | /** |
||
| 34 | * Message will be delivered to everyone |
||
| 35 | */ |
||
| 36 | const SEND_TO_ALL = 1; |
||
| 37 | /** |
||
| 38 | * Message will be delivered to registered users only |
||
| 39 | */ |
||
| 40 | const SEND_TO_REGISTERED_USERS = 2; |
||
| 41 | /** |
||
| 42 | * Message will be delivered to users, specified in target (might be array of users) |
||
| 43 | */ |
||
| 44 | const SEND_TO_SPECIFIC_USERS = 3; |
||
| 45 | /** |
||
| 46 | * Message will be delivered to users from group, specified in target (might be array of groups) |
||
| 47 | */ |
||
| 48 | const SEND_TO_USERS_GROUP = 4; |
||
| 49 | /** |
||
| 50 | * Message will be delivered to users whose connection objects have property with certain value, target should be an array with format [property, value] |
||
| 51 | */ |
||
| 52 | const SEND_TO_FILTER = 5; |
||
| 53 | /** |
||
| 54 | * Each object additionally will have properties `user_id`, `session_id`, `session_expire` and `user_groups` with user id and ids of user groups |
||
| 55 | * correspondingly |
||
| 56 | * |
||
| 57 | * @var ConnectionInterface[]|SplObjectStorage |
||
| 58 | */ |
||
| 59 | protected $clients; |
||
| 60 | /** |
||
| 61 | * @var ConnectionInterface[]|SplObjectStorage |
||
| 62 | */ |
||
| 63 | protected $servers; |
||
| 64 | /** |
||
| 65 | * Connection to master server |
||
| 66 | * |
||
| 67 | * @var ConnectionInterface |
||
| 68 | */ |
||
| 69 | protected $connection_to_master; |
||
| 70 | /** |
||
| 71 | * @var Pool |
||
| 72 | */ |
||
| 73 | protected $pool; |
||
| 74 | /** |
||
| 75 | * Address to WebSockets server in format wss://server/WebSockets or ws://server/WebSockets, so that one WebSockets server can reach another (in case of |
||
| 76 | * several servers) |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $address; |
||
| 81 | /** |
||
| 82 | * @var IoServer |
||
| 83 | */ |
||
| 84 | protected $io_server; |
||
| 85 | /** |
||
| 86 | * @var \React\EventLoop\LoopInterface |
||
| 87 | */ |
||
| 88 | protected $loop; |
||
| 89 | /** |
||
| 90 | * @var Client_factory |
||
| 91 | */ |
||
| 92 | protected $client_connector; |
||
| 93 | /** |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | protected $listen_port; |
||
| 97 | /** |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $listen_locally; |
||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $dns_server; |
||
| 105 | /** |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $security_key; |
||
| 109 | /** |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | protected $remember_session_ip; |
||
| 113 | protected function construct () { |
||
| 129 | /** |
||
| 130 | * Run WebSockets server |
||
| 131 | * |
||
| 132 | * @param null|string $address |
||
| 133 | */ |
||
| 134 | function run ($address = null) { |
||
| 163 | /** |
||
| 164 | * @param ConnectionInterface $connection |
||
| 165 | */ |
||
| 166 | function onOpen (ConnectionInterface $connection) { |
||
| 169 | /** |
||
| 170 | * @param ConnectionInterface $connection |
||
| 171 | * @param string $message |
||
| 172 | */ |
||
| 173 | function onMessage (ConnectionInterface $connection, $message) { |
||
| 254 | /** |
||
| 255 | * @param string $message |
||
| 256 | * @param string $action |
||
| 257 | * @param mixed $details |
||
| 258 | * @param int|int[] $send_to |
||
| 259 | * @param int $target |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected function parse_message ($message, &$action, &$details, &$send_to, &$target) { |
||
| 277 | /** |
||
| 278 | * @param string $message |
||
| 279 | * @param ConnectionInterface|null $skip_server |
||
| 280 | */ |
||
| 281 | protected function broadcast_message_to_servers ($message, $skip_server = null) { |
||
| 289 | /** |
||
| 290 | * Compose error |
||
| 291 | * |
||
| 292 | * @param int $error_code HTTP status code |
||
| 293 | * @param null|string $error_message String representation of status code |
||
| 294 | * |
||
| 295 | * @return array Array to be passed as details to `::send_to_clients()` |
||
|
|
|||
| 296 | */ |
||
| 297 | function compose_error ($error_code, $error_message = null) { |
||
| 301 | /** |
||
| 302 | * Send request to client |
||
| 303 | * |
||
| 304 | * @param string $action |
||
| 305 | * @param mixed $details |
||
| 306 | * @param int $send_to Constants `self::SEND_TO*` should be used here |
||
| 307 | * @param false|int|int[] $target Id or array of ids in case of response to one or several users or groups |
||
| 308 | */ |
||
| 309 | function send_to_clients ($action, $details, $send_to, $target = false) { |
||
| 342 | /** |
||
| 343 | * Send request to client |
||
| 344 | * |
||
| 345 | * @param string $action |
||
| 346 | * @param mixed $details |
||
| 347 | * @param int $send_to Constants `self::SEND_TO_*` should be used here |
||
| 348 | * @param false|int|int[]|mixed[] $target Id or array of ids in case of response to one or several users or groups, [property, value] for filter |
||
| 349 | */ |
||
| 350 | protected function send_to_clients_internal ($action, $details, $send_to, $target = false) { |
||
| 412 | /** |
||
| 413 | * If session not expire - will send message, otherwise will disconnect |
||
| 414 | * |
||
| 415 | * @param ConnectionInterface $client |
||
| 416 | * @param string $message |
||
| 417 | */ |
||
| 418 | protected function send_to_client_if_not_expire ($client, $message) { |
||
| 426 | /** |
||
| 427 | * Close all client connections by specified session id |
||
| 428 | * |
||
| 429 | * @param string $session_id |
||
| 430 | */ |
||
| 431 | function close_by_session ($session_id) { |
||
| 434 | /** |
||
| 435 | * Close all client connections by specified user id |
||
| 436 | * |
||
| 437 | * @param string $user_id |
||
| 438 | */ |
||
| 439 | function close_by_user ($user_id) { |
||
| 442 | /** |
||
| 443 | * Connect to master server |
||
| 444 | * |
||
| 445 | * Two trials, if server do not respond twice - it will be removed from servers pool, and next server will become master |
||
| 446 | */ |
||
| 447 | protected function connect_to_master () { |
||
| 524 | /** |
||
| 525 | * Get event loop instance |
||
| 526 | * |
||
| 527 | * @return \React\EventLoop\LoopInterface |
||
| 528 | */ |
||
| 529 | function get_loop () { |
||
| 532 | /** |
||
| 533 | * @param ConnectionInterface $connection |
||
| 534 | */ |
||
| 535 | function onClose (ConnectionInterface $connection) { |
||
| 557 | /** |
||
| 558 | * @param ConnectionInterface $connection |
||
| 559 | * @param Exception $e |
||
| 560 | */ |
||
| 561 | function onError (ConnectionInterface $connection, Exception $e) { |
||
| 567 | } |
||
| 568 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.