Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Client |
||
50 | { |
||
51 | /** |
||
52 | * Used in {@link static::isRequestActive()} to limit search only to |
||
53 | * requests that have a callback. |
||
54 | */ |
||
55 | const FILTER_CALLBACK = 1; |
||
56 | /** |
||
57 | * Used in {@link static::isRequestActive()} to limit search only to |
||
58 | * requests that use the buffer. |
||
59 | */ |
||
60 | const FILTER_BUFFER = 2; |
||
61 | /** |
||
62 | * Used in {@link static::isRequestActive()} to indicate no limit in search. |
||
63 | */ |
||
64 | const FILTER_ALL = 3; |
||
65 | |||
66 | /** |
||
67 | * The communicator for this client. |
||
68 | * |
||
69 | * @var Communicator |
||
70 | */ |
||
71 | protected $com; |
||
72 | |||
73 | /** |
||
74 | * The number of currently pending requests. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $pendingRequestsCount = 0; |
||
79 | |||
80 | /** |
||
81 | * An array of responses that have not yet been extracted |
||
82 | * or passed to a callback. |
||
83 | * |
||
84 | * Key is the tag of the request, and the value is an array of |
||
85 | * associated responses. |
||
86 | * |
||
87 | * @var array<string,Response[]> |
||
88 | */ |
||
89 | protected $responseBuffer = array(); |
||
90 | |||
91 | /** |
||
92 | * An array of callbacks to be executed as responses come. |
||
93 | * |
||
94 | * Key is the tag of the request, and the value is the callback for it. |
||
95 | * |
||
96 | * @var array<string,callback> |
||
97 | */ |
||
98 | protected $callbacks = array(); |
||
99 | |||
100 | /** |
||
101 | * A registry for the operations. |
||
102 | * |
||
103 | * Particularly helpful at persistent connections. |
||
104 | * |
||
105 | * @var Registry |
||
106 | */ |
||
107 | protected $registry = null; |
||
108 | |||
109 | /** |
||
110 | * Stream response words that are above this many bytes. |
||
111 | * NULL to disable streaming completely. |
||
112 | * |
||
113 | * @var int|null |
||
114 | */ |
||
115 | private $_streamingResponses = null; |
||
116 | |||
117 | /** |
||
118 | * Creates a new instance of a RouterOS API client. |
||
119 | * |
||
120 | * Creates a new instance of a RouterOS API client with the specified |
||
121 | * settings. |
||
122 | * |
||
123 | * @param string $host Hostname (IP or domain) of RouterOS. |
||
124 | * @param string $username The RouterOS username. |
||
125 | * @param string $password The RouterOS password. |
||
126 | * @param int|null $port The port on which the RouterOS host |
||
127 | * provides the API service. You can also specify NULL, in which case |
||
128 | * the port will automatically be chosen between 8728 and 8729, |
||
129 | * depending on the value of $crypto. |
||
130 | * @param bool $persist Whether or not the connection should be a |
||
131 | * persistent one. |
||
132 | * @param double|null $timeout The timeout for the connection. |
||
133 | * @param string $crypto The encryption for this connection. |
||
134 | * Must be one of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* |
||
135 | * constants. Off by default. RouterOS currently supports only TLS, but |
||
136 | * the setting is provided in this fashion for forward compatibility's |
||
137 | * sake. And for the sake of simplicity, if you specify an encryption, |
||
138 | * don't specify a context and your default context uses the value |
||
139 | * "DEFAULT" for ciphers, "ADH" will be automatically added to the list |
||
140 | * of ciphers. |
||
141 | * @param resource|null $context A context for the socket. |
||
142 | * |
||
143 | * @see sendSync() |
||
144 | * @see sendAsync() |
||
145 | */ |
||
146 | public function __construct( |
||
190 | |||
191 | /** |
||
192 | * A shorthand gateway. |
||
193 | * |
||
194 | * This is a magic PHP method that allows you to call the object as a |
||
195 | * function. Depending on the argument given, one of the other functions in |
||
196 | * the class is invoked and its returned value is returned by this function. |
||
197 | * |
||
198 | * @param mixed $arg Value can be either a {@link Request} to send, which |
||
199 | * would be sent asynchronously if it has a tag, and synchronously if |
||
200 | * not, a number to loop with or NULL to complete all pending requests. |
||
201 | * Any other value is converted to string and treated as the tag of a |
||
202 | * request to complete. |
||
203 | * |
||
204 | * @return mixed Whatever the long form function would have returned. |
||
205 | */ |
||
206 | public function __invoke($arg = null) |
||
218 | |||
219 | /** |
||
220 | * Login to a RouterOS connection. |
||
221 | * |
||
222 | * @param Communicator $com The communicator to attempt to login to. |
||
223 | * @param string $username The RouterOS username. |
||
224 | * @param string $password The RouterOS password. |
||
225 | * @param int|null $timeout The time to wait for each response. NULL |
||
226 | * waits indefinitely. |
||
227 | * |
||
228 | * @return bool TRUE on success, FALSE on failure. |
||
229 | */ |
||
230 | public static function login( |
||
267 | |||
268 | /** |
||
269 | * Login to a RouterOS connection. |
||
270 | * |
||
271 | * This is the actual login procedure, applied regardless of persistence and |
||
272 | * charset settings. |
||
273 | * |
||
274 | * @param Communicator $com The communicator to attempt to login to. |
||
275 | * @param string $username The RouterOS username. |
||
276 | * @param string $password The RouterOS password. Potentially parsed |
||
277 | * already by iconv. |
||
278 | * @param int|null $timeout The time to wait for each response. NULL |
||
279 | * waits indefinitely. |
||
280 | * |
||
281 | * @return bool TRUE on success, FALSE on failure. |
||
282 | */ |
||
283 | private static function _login( |
||
314 | |||
315 | /** |
||
316 | * Sets the charset(s) for this connection. |
||
317 | * |
||
318 | * Sets the charset(s) for this connection. The specified charset(s) will be |
||
319 | * used for all future requests and responses. When sending, |
||
320 | * {@link Communicator::CHARSET_LOCAL} is converted to |
||
321 | * {@link Communicator::CHARSET_REMOTE}, and when receiving, |
||
322 | * {@link Communicator::CHARSET_REMOTE} is converted to |
||
323 | * {@link Communicator::CHARSET_LOCAL}. Setting NULL to either charset will |
||
324 | * disable charset convertion, and data will be both sent and received "as |
||
325 | * is". |
||
326 | * |
||
327 | * @param mixed $charset The charset to set. If $charsetType is |
||
328 | * {@link Communicator::CHARSET_ALL}, you can supply either a string to |
||
329 | * use for all charsets, or an array with the charset types as keys, and |
||
330 | * the charsets as values. |
||
331 | * @param int $charsetType Which charset to set. Valid values are the |
||
332 | * Communicator::CHARSET_* constants. Any other value is treated as |
||
333 | * {@link Communicator::CHARSET_ALL}. |
||
334 | * |
||
335 | * @return string|array The old charset. If $charsetType is |
||
336 | * {@link Communicator::CHARSET_ALL}, the old values will be returned as |
||
337 | * an array with the types as keys, and charsets as values. |
||
338 | * |
||
339 | * @see Communicator::setDefaultCharset() |
||
340 | */ |
||
341 | public function setCharset( |
||
347 | |||
348 | /** |
||
349 | * Gets the charset(s) for this connection. |
||
350 | * |
||
351 | * @param int $charsetType Which charset to get. Valid values are the |
||
352 | * Communicator::CHARSET_* constants. Any other value is treated as |
||
353 | * {@link Communicator::CHARSET_ALL}. |
||
354 | * |
||
355 | * @return string|array The current charset. If $charsetType is |
||
356 | * {@link Communicator::CHARSET_ALL}, the current values will be |
||
357 | * returned as an array with the types as keys, and charsets as values. |
||
358 | * |
||
359 | * @see setCharset() |
||
360 | */ |
||
361 | public function getCharset($charsetType) |
||
365 | |||
366 | /** |
||
367 | * Sends a request and waits for responses. |
||
368 | * |
||
369 | * @param Request $request The request to send. |
||
370 | * @param callback|null $callback Optional. A function that is to be |
||
371 | * executed when new responses for this request are available. |
||
372 | * The callback takes two parameters. The {@link Response} object as |
||
373 | * the first, and the {@link Client} object as the second one. If the |
||
374 | * callback returns TRUE, the request is canceled. Note that the |
||
375 | * callback may be executed at least two times after that. Once with a |
||
376 | * {@link Response::TYPE_ERROR} response that notifies about the |
||
377 | * canceling, plus the {@link Response::TYPE_FINAL} response. |
||
378 | * |
||
379 | * @return $this The client object. |
||
380 | * |
||
381 | * @see completeRequest() |
||
382 | * @see loop() |
||
383 | * @see cancelRequest() |
||
384 | */ |
||
385 | public function sendAsync(Request $request, $callback = null) |
||
419 | |||
420 | /** |
||
421 | * Checks if a request is active. |
||
422 | * |
||
423 | * Checks if a request is active. A request is considered active if it's a |
||
424 | * pending request and/or has responses that are not yet extracted. |
||
425 | * |
||
426 | * @param string $tag The tag of the request to look for. |
||
427 | * @param int $filter One of the FILTER_* constants. Limits the search |
||
428 | * to the specified places. |
||
429 | * |
||
430 | * @return bool TRUE if the request is active, FALSE otherwise. |
||
431 | * |
||
432 | * @see getPendingRequestsCount() |
||
433 | * @see completeRequest() |
||
434 | */ |
||
435 | public function isRequestActive($tag, $filter = self::FILTER_ALL) |
||
446 | |||
447 | /** |
||
448 | * Sends a request and gets the full response. |
||
449 | * |
||
450 | * @param Request $request The request to send. |
||
451 | * |
||
452 | * @return ResponseCollection The received responses as a collection. |
||
453 | * |
||
454 | * @see sendAsync() |
||
455 | * @see close() |
||
456 | */ |
||
457 | public function sendSync(Request $request) |
||
467 | |||
468 | /** |
||
469 | * Completes a specified request. |
||
470 | * |
||
471 | * Starts an event loop for the RouterOS callbacks and finishes when a |
||
472 | * specified request is completed. |
||
473 | * |
||
474 | * @param string|null $tag The tag of the request to complete. |
||
475 | * Setting NULL completes all requests. |
||
476 | * |
||
477 | * @return ResponseCollection A collection of {@link Response} objects that |
||
478 | * haven't been passed to a callback function or previously extracted |
||
479 | * with {@link static::extractNewResponses()}. Returns an empty |
||
480 | * collection when $tag is set to NULL (responses can still be |
||
481 | * extracted). |
||
482 | */ |
||
483 | public function completeRequest($tag = null) |
||
511 | |||
512 | /** |
||
513 | * Extracts responses for a request. |
||
514 | * |
||
515 | * Gets all new responses for a request that haven't been passed to a |
||
516 | * callback and clears the buffer from them. |
||
517 | * |
||
518 | * @param string|null $tag The tag of the request to extract |
||
519 | * new responses for. |
||
520 | * Specifying NULL with extract new responses for all requests. |
||
521 | * |
||
522 | * @return ResponseCollection A collection of {@link Response} objects for |
||
523 | * the specified request. |
||
524 | * |
||
525 | * @see loop() |
||
526 | */ |
||
527 | public function extractNewResponses($tag = null) |
||
557 | |||
558 | /** |
||
559 | * Starts an event loop for the RouterOS callbacks. |
||
560 | * |
||
561 | * Starts an event loop for the RouterOS callbacks and finishes when there |
||
562 | * are no more pending requests or when a specified timeout has passed |
||
563 | * (whichever comes first). |
||
564 | * |
||
565 | * @param int|null $sTimeout Timeout for the loop. |
||
566 | * If NULL, there is no time limit. |
||
567 | * @param int $usTimeout Microseconds to add to the time limit. |
||
568 | * |
||
569 | * @return bool TRUE when there are any more pending requests, FALSE |
||
570 | * otherwise. |
||
571 | * |
||
572 | * @see extractNewResponses() |
||
573 | * @see getPendingRequestsCount() |
||
574 | */ |
||
575 | public function loop($sTimeout = null, $usTimeout = 0) |
||
616 | |||
617 | /** |
||
618 | * Gets the number of pending requests. |
||
619 | * |
||
620 | * @return int The number of pending requests. |
||
621 | * |
||
622 | * @see isRequestActive() |
||
623 | */ |
||
624 | public function getPendingRequestsCount() |
||
628 | |||
629 | /** |
||
630 | * Cancels a request. |
||
631 | * |
||
632 | * Cancels an active request. Using this function in favor of a plain call |
||
633 | * to the "/cancel" command is highly recommended, as it also updates the |
||
634 | * counter of pending requests properly. Note that canceling a request also |
||
635 | * removes any responses for it that were not previously extracted with |
||
636 | * {@link static::extractNewResponses()}. |
||
637 | * |
||
638 | * @param string|null $tag Tag of the request to cancel. |
||
639 | * Setting NULL will cancel all requests. |
||
640 | * |
||
641 | * @return $this The client object. |
||
642 | * |
||
643 | * @see sendAsync() |
||
644 | * @see close() |
||
645 | */ |
||
646 | public function cancelRequest($tag = null) |
||
701 | |||
702 | /** |
||
703 | * Sets response streaming setting. |
||
704 | * |
||
705 | * Sets when future response words are streamed. If a word is streamed, |
||
706 | * the property value is returned a stream instead of a string, and |
||
707 | * unrecognized words are returned entirely as streams instead of strings. |
||
708 | * This is particularly useful if you expect a response that may contain |
||
709 | * one or more very large words. |
||
710 | * |
||
711 | * @param int|null $threshold Threshold after which to stream |
||
712 | * a word. That is, a word less than this length will not be streamed. |
||
713 | * If set to 0, effectively all words are streamed. |
||
714 | * NULL to disable streaming altogether. |
||
715 | * |
||
716 | * @return $this The client object. |
||
717 | * |
||
718 | * @see getStreamingResponses() |
||
719 | */ |
||
720 | public function setStreamingResponses($threshold) |
||
727 | |||
728 | /** |
||
729 | * Gets response streaming setting. |
||
730 | * |
||
731 | * Gets when future response words are streamed. |
||
732 | * |
||
733 | * @return int|null The value of the setting. |
||
734 | * |
||
735 | * @see setStreamingResponses() |
||
736 | */ |
||
737 | public function getStreamingResponses() |
||
741 | |||
742 | /** |
||
743 | * Closes the opened connection, even if it is a persistent one. |
||
744 | * |
||
745 | * Closes the opened connection, even if it is a persistent one. Note that |
||
746 | * {@link static::extractNewResponses()} can still be used to extract |
||
747 | * responses collected prior to the closing. |
||
748 | * |
||
749 | * @return bool TRUE on success, FALSE on failure. |
||
750 | */ |
||
751 | public function close() |
||
786 | |||
787 | /** |
||
788 | * Closes the connection, unless it's a persistent one. |
||
789 | */ |
||
790 | public function __destruct() |
||
800 | |||
801 | /** |
||
802 | * Sends a request to RouterOS. |
||
803 | * |
||
804 | * @param Request $request The request to send. |
||
805 | * |
||
806 | * @return $this The client object. |
||
807 | * |
||
808 | * @see sendSync() |
||
809 | * @see sendAsync() |
||
810 | */ |
||
811 | protected function send(Request $request) |
||
817 | |||
818 | /** |
||
819 | * Dispatches the next response in queue. |
||
820 | * |
||
821 | * Dispatches the next response in queue, i.e. it executes the associated |
||
822 | * callback if there is one, or places the response in the response buffer. |
||
823 | * |
||
824 | * @param int|null $sTimeout If a response is not immediately available, |
||
825 | * wait this many seconds. |
||
826 | * If NULL, wait indefinitely. |
||
827 | * @param int $usTimeout Microseconds to add to the waiting time. |
||
828 | * |
||
829 | * @throws SocketException When there's no response within the time limit. |
||
830 | * @return Response The dispatched response. |
||
831 | */ |
||
832 | protected function dispatchNextResponse($sTimeout = 0, $usTimeout = 0) |
||
873 | } |
||
874 |