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, ShortsTrait; |
||
| 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 | 17 | 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 | 16 | private function config(string $parameter) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Send write query to RouterOS |
||
| 78 | * |
||
| 79 | * @param string|array|\RouterOS\Query $query |
||
| 80 | * @return \RouterOS\Client |
||
| 81 | * @throws \RouterOS\Exceptions\QueryException |
||
| 82 | * @deprecated |
||
| 83 | */ |
||
| 84 | 15 | public function write($query): Client |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Send write query to RouterOS (modern version of write) |
||
| 103 | * |
||
| 104 | * @param string $endpoint Path of API query |
||
| 105 | * @param array|null $where List of where filters |
||
| 106 | * @param string|null $operations Some operations which need make on response |
||
| 107 | * @param string|null $tag Mark query with tag |
||
| 108 | * @return \RouterOS\Client |
||
| 109 | * @throws \RouterOS\Exceptions\QueryException |
||
| 110 | * @since 1.0.0 |
||
| 111 | */ |
||
| 112 | public function query(string $endpoint, array $where = null, string $operations = null, string $tag = null): Client |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Send write query object to RouterOS |
||
| 147 | * |
||
| 148 | * @param \RouterOS\Query $query |
||
| 149 | * @return \RouterOS\Client |
||
| 150 | * @throws \RouterOS\Exceptions\QueryException |
||
| 151 | * @since 1.0.0 |
||
| 152 | */ |
||
| 153 | 15 | private function writeRAW(Query $query): Client |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Read RAW response from RouterOS |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | * @since 1.0.0 |
||
| 171 | */ |
||
| 172 | 15 | private function readRAW(): array |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Read answer from server after query was executed |
||
| 211 | * |
||
| 212 | * A Mikrotik reply is formed of blocks |
||
| 213 | * Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal') |
||
| 214 | * Each block end with an zero byte (empty line) |
||
| 215 | * Reply ends with a complete !done or !fatal block (ended with 'empty line') |
||
| 216 | * A !fatal block precedes TCP connexion close |
||
| 217 | * |
||
| 218 | * @param bool $parse |
||
| 219 | * @return mixed |
||
| 220 | */ |
||
| 221 | 15 | public function read(bool $parse = true) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Read using Iterators to improve performance on large dataset |
||
| 232 | * |
||
| 233 | * @return \RouterOS\ResponseIterator |
||
| 234 | * @since 1.0.0 |
||
| 235 | */ |
||
| 236 | 4 | public function readAsIterator(): ResponseIterator |
|
| 240 | |||
| 241 | /** |
||
| 242 | * This method was created by memory save reasons, it convert response |
||
| 243 | * from RouterOS to readable array in safe way. |
||
| 244 | * |
||
| 245 | * @param array $raw Array RAW response from server |
||
| 246 | * @return mixed |
||
| 247 | * |
||
| 248 | * Based on RouterOSResponseArray solution by @arily |
||
| 249 | * |
||
| 250 | * @link https://github.com/arily/RouterOSResponseArray |
||
| 251 | * @since 1.0.0 |
||
| 252 | */ |
||
| 253 | 4 | private function rosario(array $raw): array |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Parse response from Router OS |
||
| 287 | * |
||
| 288 | * @param array $response Response data |
||
| 289 | * @return array Array with parsed data |
||
| 290 | */ |
||
| 291 | 5 | public function parseResponse(array $response): array |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Parse result from RouterOS by regular expression |
||
| 330 | * |
||
| 331 | * @param string $value |
||
| 332 | * @param array $matches |
||
| 333 | */ |
||
| 334 | 4 | private function pregResponse(string $value, &$matches) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Authorization logic |
||
| 341 | * |
||
| 342 | * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS |
||
| 343 | * @return bool |
||
| 344 | * @throws \RouterOS\Exceptions\ClientException |
||
| 345 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 346 | * @throws \RouterOS\Exceptions\QueryException |
||
| 347 | */ |
||
| 348 | 15 | private function login(bool $legacyRetry = false): bool |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Detect by login request if firmware is legacy |
||
| 391 | * |
||
| 392 | * @param array $response |
||
| 393 | * @return bool |
||
| 394 | * @throws ConfigException |
||
| 395 | */ |
||
| 396 | 14 | private function isLegacy(array &$response): bool |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Connect to socket server |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | * @throws \RouterOS\Exceptions\ClientException |
||
| 406 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 407 | * @throws \RouterOS\Exceptions\QueryException |
||
| 408 | */ |
||
| 409 | 16 | private function connect(): bool |
|
| 440 | } |
||
| 441 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.