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 |
||
| 11 | class Client extends AbstractClient |
||
| 12 | { |
||
| 13 | use UrlHelpersTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $apiList; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $urlApi; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $urlClient; |
||
|
|
|||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $urlConsole; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $apiKey; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $secretKey; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $ssoKey; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $responseError = 'errortext'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $responseCode = 'errorcode'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | private $ssoEnabled = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructs a new Cloudstack client instance. |
||
| 67 | * |
||
| 68 | * @param array $options |
||
| 69 | * An array of options to set on this client. Options include |
||
| 70 | * 'apiList', 'urlApi', 'urlClient', 'urlConsole', 'apiKey', |
||
| 71 | * 'secretKey', 'responseError' and 'responseCode'. |
||
| 72 | * @param array $collaborators |
||
| 73 | * An array of collaborators that may be used to override |
||
| 74 | * this provider's default behavior. Collaborators include |
||
| 75 | * `requestFactory` and `httpClient`. |
||
| 76 | */ |
||
| 77 | public function __construct(array $options = [], array $collaborators = []) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns all options that can be configured. |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | protected function getConfigurableOptions() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns all options that are required. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | protected function getRequiredOptions() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Verifies that all required options have been passed. |
||
| 127 | * |
||
| 128 | * @param array $options |
||
| 129 | * @return void |
||
| 130 | * @throws InvalidArgumentException |
||
| 131 | */ |
||
| 132 | private function assertRequiredOptions(array $options) |
||
| 142 | |||
| 143 | public function command($command, array $options = []) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Verifies that all required options have been passed. |
||
| 156 | * |
||
| 157 | * @param array $options |
||
| 158 | * @return void |
||
| 159 | * @throws RuntimeException |
||
| 160 | * @throws InvalidArgumentException |
||
| 161 | */ |
||
| 162 | private function assertRequiredCommandOptions($command, array $options = []) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns command method based on the command. |
||
| 183 | * |
||
| 184 | * @param string $command |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getCommandMethod($command) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Builds the command URL's query string. |
||
| 198 | * |
||
| 199 | * @param array $params |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getCommandQuery(array $params) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Builds the authorization URL. |
||
| 209 | * |
||
| 210 | * @param string $command |
||
| 211 | * @param array $options |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getCommandUrl($command, array $options = []) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns command parameters based on provided options. |
||
| 225 | * |
||
| 226 | * @param string $command |
||
| 227 | * @param array $options |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | protected function getCommandParameters($command, array $options) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Signs the command parameters. |
||
| 241 | * |
||
| 242 | * @param array $params |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | protected function signCommandParameters(array $params = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get Cloudstack Client API list. |
||
| 275 | * |
||
| 276 | * Tries to load the API list from the cache directory when |
||
| 277 | * the 'apiList' on the class is empty. |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | * @throws RuntimeException |
||
| 281 | */ |
||
| 282 | public function getApiList() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set Cloudstack Client API list. |
||
| 301 | * |
||
| 302 | * @param array $apiList |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function setApiList(array $apiList) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Appends a query string to a URL. |
||
| 312 | * |
||
| 313 | * @param string $url |
||
| 314 | * @param string $query |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | protected function appendQuery($url, $query) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Build a query string from an array. |
||
| 330 | * |
||
| 331 | * @param array $params |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | protected function buildQueryString(array $params) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Flatten query params. |
||
| 360 | * |
||
| 361 | * @param array $params |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | protected static function flattenParams(array $params) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Checks a provider response for errors. |
||
| 381 | * |
||
| 382 | * @param ResponseInterface $response |
||
| 383 | * @param array|string $data |
||
| 384 | * @return void |
||
| 385 | * @throws \PCextreme\Cloudstack\Exceptions\ClientException |
||
| 386 | */ |
||
| 387 | protected function checkResponse(ResponseInterface $response, $data) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Enable SSO key signing for the next request. |
||
| 405 | * |
||
| 406 | * @param bool $enable |
||
| 407 | * @return self |
||
| 408 | */ |
||
| 409 | public function enableSso($enable = true) |
||
| 415 | /** |
||
| 416 | * Determine if SSO signing is enabled. |
||
| 417 | * |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | public function isSsoEnabled() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Handle dynamic method calls into the method. |
||
| 427 | * |
||
| 428 | * @param string $method |
||
| 429 | * @param array $parameters |
||
| 430 | * @return mixed |
||
| 431 | */ |
||
| 432 | public function __call($method, $parameters) |
||
| 438 | } |
||
| 439 |
This check marks private properties in classes that are never used. Those properties can be removed.