Complex classes like Communicator 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 Communicator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Communicator |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Used when getting/setting all (default) charsets. |
||
| 48 | */ |
||
| 49 | const CHARSET_ALL = -1; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Used when getting/setting the (default) remote charset. |
||
| 53 | * |
||
| 54 | * The remote charset is the charset in which RouterOS stores its data. |
||
| 55 | * If you want to keep compatibility with your Winbox, this charset should |
||
| 56 | * match the default charset from your Windows' regional settings. |
||
| 57 | */ |
||
| 58 | const CHARSET_REMOTE = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Used when getting/setting the (default) local charset. |
||
| 62 | * |
||
| 63 | * The local charset is the charset in which the data from RouterOS will be |
||
| 64 | * returned as. This charset should match the charset of the place the data |
||
| 65 | * will eventually be written to. |
||
| 66 | */ |
||
| 67 | const CHARSET_LOCAL = 1; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * An array with the default charset. |
||
| 71 | * |
||
| 72 | * Charset types as keys, and the default charsets as values. |
||
| 73 | * |
||
| 74 | * @var array<string,string|null> |
||
| 75 | */ |
||
| 76 | protected static $defaultCharsets = array( |
||
| 77 | self::CHARSET_REMOTE => null, |
||
| 78 | self::CHARSET_LOCAL => null |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * An array with the current charset. |
||
| 83 | * |
||
| 84 | * Charset types as keys, and the current charsets as values. |
||
| 85 | * |
||
| 86 | * @var array<string,string|null> |
||
| 87 | */ |
||
| 88 | protected $charsets = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Length of next word. |
||
| 92 | * |
||
| 93 | * Length of next word. NULL if no peeking was done. |
||
| 94 | * |
||
| 95 | * @var int|double|null |
||
| 96 | */ |
||
| 97 | protected $nextWordLength = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Last state of the word lock. |
||
| 101 | * |
||
| 102 | * Last state of the word lock when using persisntent connections. |
||
| 103 | * Unused by non-persistent connections. |
||
| 104 | * |
||
| 105 | * @var int|false |
||
| 106 | */ |
||
| 107 | protected $nextWordLock = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The transmitter for the connection. |
||
| 111 | * |
||
| 112 | * @var T\TcpClient |
||
| 113 | */ |
||
| 114 | protected $trans; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Creates a new connection with the specified options. |
||
| 118 | * |
||
| 119 | * @param string $host Hostname (IP or domain) of RouterOS. |
||
| 120 | * @param int|null $port The port on which the RouterOS host |
||
| 121 | * provides the API service. You can also specify NULL, in which case |
||
| 122 | * the port will automatically be chosen between 8728 and 8729, |
||
| 123 | * depending on the value of $crypto. |
||
| 124 | * @param bool $persist Whether or not the connection should be a |
||
| 125 | * persistent one. |
||
| 126 | * @param double|null $timeout The timeout for the connection. |
||
| 127 | * @param string $key A string that uniquely identifies the |
||
| 128 | * connection. |
||
| 129 | * @param string $crypto The encryption for this connection. |
||
| 130 | * Must be one of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* |
||
| 131 | * constants. Off by default. RouterOS currently supports only TLS, but |
||
| 132 | * the setting is provided in this fashion for forward compatibility's |
||
| 133 | * sake. And for the sake of simplicity, if you specify an encryption, |
||
| 134 | * don't specify a context and your default context uses the value |
||
| 135 | * "DEFAULT" for ciphers, "ADH" will be automatically added to the list |
||
| 136 | * of ciphers. |
||
| 137 | * @param resource|null $context A context for the socket. |
||
| 138 | * |
||
| 139 | * @see sendWord() |
||
| 140 | */ |
||
| 141 | public function __construct( |
||
| 198 | |||
| 199 | /** |
||
| 200 | * A shorthand gateway. |
||
| 201 | * |
||
| 202 | * This is a magic PHP method that allows you to call the object as a |
||
| 203 | * function. Depending on the argument given, one of the other functions in |
||
| 204 | * the class is invoked and its returned value is returned by this function. |
||
| 205 | * |
||
| 206 | * @param string|null $string A string of the word to send, or NULL to get |
||
| 207 | * the next word as a string. |
||
| 208 | * |
||
| 209 | * @return int|string If a string is provided, returns the number of bytes |
||
| 210 | * sent, otherwise returns the next word as a string. |
||
| 211 | */ |
||
| 212 | public function __invoke($string = null) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Checks whether a variable is a seekable stream resource. |
||
| 220 | * |
||
| 221 | * @param mixed $var The value to check. |
||
| 222 | * |
||
| 223 | * @return bool TRUE if $var is a seekable stream, FALSE otherwise. |
||
| 224 | */ |
||
| 225 | public static function isSeekableStream($var) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Uses iconv to convert a stream from one charset to another. |
||
| 236 | * |
||
| 237 | * @param string $inCharset The charset of the stream. |
||
| 238 | * @param string $outCharset The desired resulting charset. |
||
| 239 | * @param resource $stream The stream to convert. The stream is assumed |
||
| 240 | * to be seekable, and is read from its current position to its end, |
||
| 241 | * after which, it is seeked back to its starting position. |
||
| 242 | * |
||
| 243 | * @return resource A new stream that uses the $out_charset. The stream is a |
||
| 244 | * subset from the original stream, from its current position to its |
||
| 245 | * end, seeked at its start. |
||
| 246 | */ |
||
| 247 | public static function iconvStream($inCharset, $outCharset, $stream) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Sets the default charset(s) for new connections. |
||
| 274 | * |
||
| 275 | * @param mixed $charset The charset to set. If $charsetType is |
||
| 276 | * {@link self::CHARSET_ALL}, you can supply either a string to use for |
||
| 277 | * all charsets, or an array with the charset types as keys, and the |
||
| 278 | * charsets as values. |
||
| 279 | * @param int $charsetType Which charset to set. Valid values are the |
||
| 280 | * CHARSET_* constants. Any other value is treated as |
||
| 281 | * {@link self::CHARSET_ALL}. |
||
| 282 | * |
||
| 283 | * @return string|array The old charset. If $charsetType is |
||
| 284 | * {@link self::CHARSET_ALL}, the old values will be returned as an |
||
| 285 | * array with the types as keys, and charsets as values. |
||
| 286 | * |
||
| 287 | * @see setCharset() |
||
| 288 | */ |
||
| 289 | public static function setDefaultCharset( |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Gets the default charset(s). |
||
| 310 | * |
||
| 311 | * @param int $charsetType Which charset to get. Valid values are the |
||
| 312 | * CHARSET_* constants. Any other value is treated as |
||
| 313 | * {@link self::CHARSET_ALL}. |
||
| 314 | * |
||
| 315 | * @return string|array The current charset. If $charsetType is |
||
| 316 | * {@link self::CHARSET_ALL}, the current values will be returned as an |
||
| 317 | * array with the types as keys, and charsets as values. |
||
| 318 | * |
||
| 319 | * @see setDefaultCharset() |
||
| 320 | */ |
||
| 321 | public static function getDefaultCharset($charsetType) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Gets the length of a seekable stream. |
||
| 329 | * |
||
| 330 | * Gets the length of a seekable stream. |
||
| 331 | * |
||
| 332 | * @param resource $stream The stream to check. The stream is assumed to be |
||
| 333 | * seekable. |
||
| 334 | * |
||
| 335 | * @return double The number of bytes in the stream between its current |
||
| 336 | * position and its end. |
||
| 337 | */ |
||
| 338 | public static function seekableStreamLength($stream) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Sets the charset(s) for this connection. |
||
| 350 | * |
||
| 351 | * Sets the charset(s) for this connection. The specified charset(s) will be |
||
| 352 | * used for all future words. When sending, {@link self::CHARSET_LOCAL} is |
||
| 353 | * converted to {@link self::CHARSET_REMOTE}, and when receiving, |
||
| 354 | * {@link self::CHARSET_REMOTE} is converted to {@link self::CHARSET_LOCAL}. |
||
| 355 | * Setting NULL to either charset will disable charset conversion, and data |
||
| 356 | * will be both sent and received "as is". |
||
| 357 | * |
||
| 358 | * @param mixed $charset The charset to set. If $charsetType is |
||
| 359 | * {@link self::CHARSET_ALL}, you can supply either a string to use for |
||
| 360 | * all charsets, or an array with the charset types as keys, and the |
||
| 361 | * charsets as values. |
||
| 362 | * @param int $charsetType Which charset to set. Valid values are the |
||
| 363 | * CHARSET_* constants. Any other value is treated as |
||
| 364 | * {@link self::CHARSET_ALL}. |
||
| 365 | * |
||
| 366 | * @return string|array The old charset. If $charsetType is |
||
| 367 | * {@link self::CHARSET_ALL}, the old values will be returned as an |
||
| 368 | * array with the types as keys, and charsets as values. |
||
| 369 | * |
||
| 370 | * @see setDefaultCharset() |
||
| 371 | */ |
||
| 372 | public function setCharset($charset, $charsetType = self::CHARSET_ALL) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Gets the charset(s) for this connection. |
||
| 391 | * |
||
| 392 | * @param int $charsetType Which charset to get. Valid values are the |
||
| 393 | * CHARSET_* constants. Any other value is treated as |
||
| 394 | * {@link self::CHARSET_ALL}. |
||
| 395 | * |
||
| 396 | * @return string|array The current charset. If $charsetType is |
||
| 397 | * {@link self::CHARSET_ALL}, the current values will be returned as an |
||
| 398 | * array with the types as keys, and charsets as values. |
||
| 399 | * |
||
| 400 | * @see getDefaultCharset() |
||
| 401 | * @see setCharset() |
||
| 402 | */ |
||
| 403 | public function getCharset($charsetType) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Gets the transmitter for this connection. |
||
| 411 | * |
||
| 412 | * @return T\TcpClient The transmitter for this connection. |
||
| 413 | */ |
||
| 414 | public function getTransmitter() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Sends a word. |
||
| 421 | * |
||
| 422 | * Sends a word and automatically encodes its length when doing so. |
||
| 423 | * |
||
| 424 | * @param string $word The word to send. |
||
| 425 | * |
||
| 426 | * @return int The number of bytes sent. |
||
| 427 | * |
||
| 428 | * @see sendWordFromStream() |
||
| 429 | * @see getNextWord() |
||
| 430 | */ |
||
| 431 | public function sendWord($word) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Sends a word based on a stream. |
||
| 455 | * |
||
| 456 | * Sends a word based on a stream and automatically encodes its length when |
||
| 457 | * doing so. The stream is read from its current position to its end, and |
||
| 458 | * then returned to its current position. Because of those operations, the |
||
| 459 | * supplied stream must be seekable. |
||
| 460 | * |
||
| 461 | * @param string $prefix A string to prepend before the stream contents. |
||
| 462 | * @param resource $stream The seekable stream to send. |
||
| 463 | * |
||
| 464 | * @return int The number of bytes sent. |
||
| 465 | * |
||
| 466 | * @see sendWord() |
||
| 467 | */ |
||
| 468 | public function sendWordFromStream($prefix, $stream) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Verifies that the length is supported. |
||
| 504 | * |
||
| 505 | * Verifies if the specified length is supported by the API. Throws a |
||
| 506 | * {@link LengthException} if that's not the case. Currently, RouterOS |
||
| 507 | * supports words up to 0xFFFFFFFF in length, so that's the only check |
||
| 508 | * performed. |
||
| 509 | * |
||
| 510 | * @param int $length The length to verify. |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | */ |
||
| 514 | public static function verifyLengthSupport($length) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Encodes the length as required by the RouterOS API. |
||
| 528 | * |
||
| 529 | * @param int $length The length to encode. |
||
| 530 | * |
||
| 531 | * @return string The encoded length. |
||
| 532 | */ |
||
| 533 | public static function encodeLength($length) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the length of the next word in queue. |
||
| 568 | * |
||
| 569 | * Get the length of the next word in queue without getting the word. |
||
| 570 | * For pesisntent connections, note that the underlying transmitter will |
||
| 571 | * be locked for receiving until either {@link self::getNextWord()} or |
||
| 572 | * {@link self::getNextWordAsStream()} is called. |
||
| 573 | * |
||
| 574 | * @return int|double |
||
| 575 | */ |
||
| 576 | public function getNextWordLength() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get the next word in queue as a string. |
||
| 591 | * |
||
| 592 | * Get the next word in queue as a string, after automatically decoding its |
||
| 593 | * length. |
||
| 594 | * |
||
| 595 | * @return string The word. |
||
| 596 | * |
||
| 597 | * @see close() |
||
| 598 | */ |
||
| 599 | public function getNextWord() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the next word in queue as a stream. |
||
| 625 | * |
||
| 626 | * Get the next word in queue as a stream, after automatically decoding its |
||
| 627 | * length. |
||
| 628 | * |
||
| 629 | * @return resource The word, as a stream. |
||
| 630 | * |
||
| 631 | * @see close() |
||
| 632 | */ |
||
| 633 | public function getNextWordAsStream() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Decodes the length of the incoming message. |
||
| 660 | * |
||
| 661 | * Decodes the length of the incoming message, as specified by the RouterOS |
||
| 662 | * API. |
||
| 663 | * |
||
| 664 | * @param T\Stream $trans The transmitter from which to decode the length of |
||
| 665 | * the incoming message. |
||
| 666 | * |
||
| 667 | * @return int|double The decoded length. |
||
| 668 | * Is of type "double" only for values above "2 << 31". |
||
| 669 | */ |
||
| 670 | public static function decodeLength(T\Stream $trans) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Decodes the length of the incoming message. |
||
| 683 | * |
||
| 684 | * Decodes the length of the incoming message, as specified by the RouterOS |
||
| 685 | * API. |
||
| 686 | * |
||
| 687 | * Difference with the non private function is that this one doesn't perform |
||
| 688 | * locking if the connection is a persistent one. |
||
| 689 | * |
||
| 690 | * @param T\Stream $trans The transmitter from which to decode the length of |
||
| 691 | * the incoming message. |
||
| 692 | * |
||
| 693 | * @return int|double The decoded length. |
||
| 694 | * Is of type "double" only for values above "2 << 31". |
||
| 695 | */ |
||
| 696 | private static function _decodeLength(T\Stream $trans) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Closes the opened connection, even if it is a persistent one. |
||
| 726 | * |
||
| 727 | * @return bool TRUE on success, FALSE on failure. |
||
| 728 | */ |
||
| 729 | public function close() |
||
| 733 | } |
||
| 734 |