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 |
||
16 | class Client implements Interfaces\ClientInterface |
||
17 | { |
||
18 | use SocketTrait; |
||
19 | |||
20 | /** |
||
21 | * Configuration of connection |
||
22 | * |
||
23 | * @var \RouterOS\Config |
||
24 | */ |
||
25 | private $_config; |
||
26 | |||
27 | /** |
||
28 | * API communication object |
||
29 | * |
||
30 | * @var \RouterOS\APIConnector |
||
31 | */ |
||
32 | |||
33 | private $_connector; |
||
34 | |||
35 | /** |
||
36 | * Client constructor. |
||
37 | * |
||
38 | * @param array|\RouterOS\Config $config |
||
39 | * @throws \RouterOS\Exceptions\ClientException |
||
40 | * @throws \RouterOS\Exceptions\ConfigException |
||
41 | * @throws \RouterOS\Exceptions\QueryException |
||
42 | */ |
||
43 | 14 | public function __construct($config) |
|
63 | |||
64 | /** |
||
65 | * Get some parameter from config |
||
66 | * |
||
67 | * @param string $parameter Name of required parameter |
||
68 | * @return mixed |
||
69 | * @throws \RouterOS\Exceptions\ConfigException |
||
70 | */ |
||
71 | 13 | private function config(string $parameter) |
|
75 | |||
76 | /** |
||
77 | * Return socket resource if is exist |
||
78 | * |
||
79 | * @return \RouterOS\Config |
||
80 | * @since 0.6 |
||
81 | */ |
||
82 | 1 | public function getConfig(): Config |
|
86 | |||
87 | /** |
||
88 | * Set configuration of client |
||
89 | * |
||
90 | * @param \RouterOS\Config $config |
||
91 | * @since 0.7 |
||
92 | */ |
||
93 | 13 | public function setConfig(Config $config) |
|
97 | |||
98 | /** |
||
99 | * Send write query to RouterOS (with or without tag) |
||
100 | * |
||
101 | * @param string|array|\RouterOS\Query $query |
||
102 | * @return \RouterOS\Client |
||
103 | * @throws \RouterOS\Exceptions\QueryException |
||
104 | */ |
||
105 | 12 | public function write($query): Client |
|
128 | |||
129 | /** |
||
130 | * Read answer from server after query was executed |
||
131 | * |
||
132 | * A Mikrotik reply is formed of blocks |
||
133 | * Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal') |
||
134 | * Each block end with an zero byte (empty line) |
||
135 | * Reply ends with a complete !done or !fatal block (ended with 'empty line') |
||
136 | * A !fatal block precedes TCP connexion close |
||
137 | * |
||
138 | * @param bool $parse |
||
139 | * @return array |
||
140 | */ |
||
141 | 12 | public function read(bool $parse = true): array |
|
177 | |||
178 | /** |
||
179 | * Alias for ->write() method |
||
180 | * |
||
181 | * @param string|array|\RouterOS\Query $query |
||
182 | * @return \RouterOS\Client |
||
183 | * @throws \RouterOS\Exceptions\QueryException |
||
184 | */ |
||
185 | 1 | public function w($query): Client |
|
189 | |||
190 | /** |
||
191 | * Alias for ->read() method |
||
192 | * |
||
193 | * @param bool $parse |
||
194 | * @return array |
||
195 | * @since 0.7 |
||
196 | */ |
||
197 | 1 | public function r(bool $parse = true): array |
|
201 | |||
202 | /** |
||
203 | * Alias for ->write()->read() combination of methods |
||
204 | * |
||
205 | * @param string|array|\RouterOS\Query $query |
||
206 | * @param bool $parse |
||
207 | * @return array |
||
208 | * @throws \RouterOS\Exceptions\ClientException |
||
209 | * @throws \RouterOS\Exceptions\QueryException |
||
210 | * @since 0.6 |
||
211 | */ |
||
212 | 4 | public function wr($query, bool $parse = true): array |
|
216 | |||
217 | /** |
||
218 | * Parse response from Router OS |
||
219 | * |
||
220 | * @param array $response Response data |
||
221 | * @return array Array with parsed data |
||
222 | */ |
||
223 | 4 | private function parseResponse(array $response): array |
|
224 | { |
||
225 | 4 | $result = []; |
|
226 | 4 | $i = -1; |
|
227 | 4 | $lines = \count($response); |
|
228 | 4 | foreach ($response as $key => $value) { |
|
229 | switch ($value) { |
||
230 | 4 | case '!re': |
|
231 | 1 | $i++; |
|
232 | 1 | break; |
|
233 | 4 | case '!fatal': |
|
234 | 1 | $result = $response; |
|
235 | 1 | break 2; |
|
236 | 3 | case '!trap': |
|
237 | 3 | case '!done': |
|
238 | // Check for =ret=, .tag and any other following messages |
||
239 | 3 | for ($j = $key + 1; $j <= $lines; $j++) { |
|
240 | // If we have lines after current one |
||
241 | 3 | if (isset($response[$j])) { |
|
242 | 2 | $this->pregResponse($response[$j], $matches); |
|
243 | 2 | if (!empty($matches)) { |
|
244 | 2 | $result['after'][$matches[1][0]] = $matches[2][0]; |
|
245 | } |
||
246 | } |
||
247 | } |
||
248 | 3 | break 2; |
|
249 | default: |
||
250 | 1 | $this->pregResponse($value, $matches); |
|
251 | 1 | if (!empty($matches)) { |
|
252 | 1 | $result[$i][$matches[1][0]] = $matches[2][0]; |
|
253 | } |
||
254 | 1 | break; |
|
255 | } |
||
256 | } |
||
257 | 4 | return $result; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Parse result from RouterOS by regular expression |
||
262 | * |
||
263 | * @param string $value |
||
264 | * @param array $matches |
||
265 | */ |
||
266 | 3 | private function pregResponse(string $value, &$matches) |
|
270 | |||
271 | /** |
||
272 | * Authorization logic |
||
273 | * |
||
274 | * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS |
||
275 | * @return bool |
||
276 | * @throws \RouterOS\Exceptions\ClientException |
||
277 | * @throws \RouterOS\Exceptions\ConfigException |
||
278 | * @throws \RouterOS\Exceptions\QueryException |
||
279 | */ |
||
280 | 12 | private function login(bool $legacyRetry = false): bool |
|
319 | |||
320 | /** |
||
321 | * Detect by login request if firmware is legacy |
||
322 | * |
||
323 | * @param array $response |
||
324 | * @return bool |
||
325 | * @throws ConfigException |
||
326 | */ |
||
327 | 11 | private function isLegacy(array &$response): bool |
|
331 | |||
332 | /** |
||
333 | * Connect to socket server |
||
334 | * |
||
335 | * @return bool |
||
336 | * @throws \RouterOS\Exceptions\ClientException |
||
337 | * @throws \RouterOS\Exceptions\ConfigException |
||
338 | * @throws \RouterOS\Exceptions\QueryException |
||
339 | */ |
||
340 | 13 | private function connect(): bool |
|
371 | |||
372 | } |
||
373 |