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|resource $word The word to send, as a string |
||
425 | * or seekable stream. |
||
426 | * @param string|resource $word,... Additional word fragments to be sent |
||
1 ignored issue
–
show
|
|||
427 | * as a single word. |
||
428 | * |
||
429 | * @return int The number of bytes sent. |
||
430 | * |
||
431 | * @see sendWordFromStream() |
||
432 | * @see getNextWord() |
||
433 | */ |
||
434 | public function sendWord($word) |
||
496 | |||
497 | /** |
||
498 | * Verifies that the length is supported. |
||
499 | * |
||
500 | * Verifies if the specified length is supported by the API. Throws a |
||
501 | * {@link LengthException} if that's not the case. Currently, RouterOS |
||
502 | * supports words up to 0xFFFFFFFF in length, so that's the only check |
||
503 | * performed. |
||
504 | * |
||
505 | * @param int $length The length to verify. |
||
506 | * |
||
507 | * @return void |
||
508 | */ |
||
509 | public static function verifyLengthSupport($length) |
||
520 | |||
521 | /** |
||
522 | * Encodes the length as required by the RouterOS API. |
||
523 | * |
||
524 | * @param int $length The length to encode. |
||
525 | * |
||
526 | * @return string The encoded length. |
||
527 | */ |
||
528 | public static function encodeLength($length) |
||
560 | |||
561 | /** |
||
562 | * Get the length of the next word in queue. |
||
563 | * |
||
564 | * Get the length of the next word in queue without getting the word. |
||
565 | * For pesisntent connections, note that the underlying transmitter will |
||
566 | * be locked for receiving until either {@link self::getNextWord()} or |
||
567 | * {@link self::getNextWordAsStream()} is called. |
||
568 | * |
||
569 | * @return int|double |
||
570 | */ |
||
571 | public function getNextWordLength() |
||
583 | |||
584 | /** |
||
585 | * Get the next word in queue as a string. |
||
586 | * |
||
587 | * Get the next word in queue as a string, after automatically decoding its |
||
588 | * length. |
||
589 | * |
||
590 | * @return string The word. |
||
591 | * |
||
592 | * @see close() |
||
593 | */ |
||
594 | public function getNextWord() |
||
617 | |||
618 | /** |
||
619 | * Get the next word in queue as a stream. |
||
620 | * |
||
621 | * Get the next word in queue as a stream, after automatically decoding its |
||
622 | * length. |
||
623 | * |
||
624 | * @return resource The word, as a stream. |
||
625 | * |
||
626 | * @see close() |
||
627 | */ |
||
628 | public function getNextWordAsStream() |
||
652 | |||
653 | /** |
||
654 | * Decodes the length of the incoming message. |
||
655 | * |
||
656 | * Decodes the length of the incoming message, as specified by the RouterOS |
||
657 | * API. |
||
658 | * |
||
659 | * @param T\Stream $trans The transmitter from which to decode the length of |
||
660 | * the incoming message. |
||
661 | * |
||
662 | * @return int|double The decoded length. |
||
663 | * Is of type "double" only for values above "2 << 31". |
||
664 | */ |
||
665 | public static function decodeLength(T\Stream $trans) |
||
675 | |||
676 | /** |
||
677 | * Decodes the length of the incoming message. |
||
678 | * |
||
679 | * Decodes the length of the incoming message, as specified by the RouterOS |
||
680 | * API. |
||
681 | * |
||
682 | * Difference with the non private function is that this one doesn't perform |
||
683 | * locking if the connection is a persistent one. |
||
684 | * |
||
685 | * @param T\Stream $trans The transmitter from which to decode the length of |
||
686 | * the incoming message. |
||
687 | * |
||
688 | * @return int|double The decoded length. |
||
689 | * Is of type "double" only for values above "2 << 31". |
||
690 | */ |
||
691 | private static function _decodeLength(T\Stream $trans) |
||
718 | |||
719 | /** |
||
720 | * Closes the opened connection, even if it is a persistent one. |
||
721 | * |
||
722 | * @return bool TRUE on success, FALSE on failure. |
||
723 | */ |
||
724 | public function close() |
||
728 | } |
||
729 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.