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 |
||
128 | class Client extends AbstractHttpClient implements LoggerAwareInterface |
||
129 | { |
||
130 | const DEPRECATION_HEADER = 'X-DEPRECATION-NOTICE'; |
||
131 | |||
132 | /** |
||
133 | * @sideeffect Test123 |
||
134 | * @var LoggerInterface |
||
135 | */ |
||
136 | protected $logger; |
||
137 | |||
138 | /** |
||
139 | * @var Manager |
||
140 | */ |
||
141 | protected $oauthManager; |
||
142 | |||
143 | /** |
||
144 | * @var ClientRequestInterface[] |
||
145 | */ |
||
146 | protected $batchRequests = []; |
||
147 | |||
148 | protected $tokenRefreshed = false; |
||
149 | |||
150 | /** |
||
151 | * @param array|Config $config |
||
152 | * @param $cache |
||
153 | * @param LoggerInterface $logger |
||
154 | */ |
||
155 | 59 | public function __construct($config, $cache = null, LoggerInterface $logger = null) |
|
163 | |||
164 | /** |
||
165 | * @return Manager |
||
166 | */ |
||
167 | 460 | public function getOauthManager() |
|
171 | |||
172 | /** |
||
173 | * @param Manager $oauthManager |
||
174 | * @return $this |
||
175 | */ |
||
176 | 59 | protected function setOauthManager(Manager $oauthManager) |
|
181 | |||
182 | /** |
||
183 | * @param LoggerInterface $logger |
||
184 | * @return $this |
||
185 | */ |
||
186 | 59 | public function setLogger(LoggerInterface $logger = null) |
|
193 | |||
194 | /** |
||
195 | * @param array $options |
||
196 | * @return AdapterInterface |
||
197 | */ |
||
198 | 486 | public function getHttpClient($options = []) |
|
213 | |||
214 | |||
215 | /** |
||
216 | * @return string |
||
217 | */ |
||
218 | 58 | protected function getBaseUrl() |
|
222 | |||
223 | /** |
||
224 | * Executes an API request synchronously |
||
225 | * |
||
226 | * @param ClientRequestInterface $request |
||
227 | * @param array $headers |
||
228 | * @return ApiResponseInterface |
||
229 | * @throws ApiException |
||
230 | * @throws InvalidTokenException |
||
231 | */ |
||
232 | 478 | public function execute(ClientRequestInterface $request, array $headers = null) |
|
260 | |||
261 | /** |
||
262 | * Executes an API request asynchronously |
||
263 | * @param ClientRequestInterface $request |
||
264 | * @return ApiResponseInterface |
||
265 | */ |
||
266 | 2 | public function executeAsync(ClientRequestInterface $request, array $headers = null) |
|
283 | |||
284 | /** |
||
285 | * @param ClientRequestInterface $request |
||
286 | * @param array $headers |
||
287 | * @return RequestInterface |
||
288 | */ |
||
289 | 485 | protected function createHttpRequest(ClientRequestInterface $request, array $headers = null) |
|
306 | |||
307 | /** |
||
308 | * Executes API requests in batch |
||
309 | * @param array $headers |
||
310 | * @return ApiResponseInterface[] |
||
311 | * @throws ApiException |
||
312 | */ |
||
313 | 394 | public function executeBatch(array $headers = null) |
|
346 | |||
347 | /** |
||
348 | * @param $exception |
||
349 | * @return $this |
||
350 | */ |
||
351 | 80 | protected function logException(ApiException $exception) |
|
370 | |||
371 | /** |
||
372 | * @param ResponseInterface $response |
||
373 | * @param RequestInterface $request |
||
374 | * @return $this |
||
375 | */ |
||
376 | 474 | protected function logDeprecatedRequest(ResponseInterface $response, RequestInterface $request) |
|
393 | |||
394 | /** |
||
395 | * @param RequestInterface $request |
||
396 | * @param ResponseInterface $response |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function format(RequestInterface $request, ResponseInterface $response) |
||
408 | |||
409 | /** |
||
410 | * @param array $headers |
||
411 | * @return array |
||
412 | */ |
||
413 | 394 | protected function getBatchHttpRequests(array $headers = null) |
|
424 | |||
425 | /** |
||
426 | * Adds a request to the batch execution queue |
||
427 | * @param ClientRequestInterface $request |
||
428 | * @return $this |
||
429 | */ |
||
430 | 394 | public function addBatchRequest(ClientRequestInterface $request) |
|
438 | |||
439 | /** |
||
440 | * Instantiates a client with the given config |
||
441 | * @param Config $config |
||
442 | * @return static |
||
443 | */ |
||
444 | public static function ofConfig(Config $config) |
||
448 | |||
449 | /** |
||
450 | * Instantiates a client with the given config and cache adapter |
||
451 | * @param Config $config |
||
452 | * @param $cache |
||
453 | * @return static |
||
454 | */ |
||
455 | public static function ofConfigAndCache(Config $config, $cache) |
||
459 | |||
460 | /** |
||
461 | * Instantiates a client with the given config and a PSR-3 compliant logger |
||
462 | * @param Config $config |
||
463 | * @param LoggerInterface $logger |
||
464 | * @return static |
||
465 | */ |
||
466 | 1 | public static function ofConfigAndLogger(Config $config, LoggerInterface $logger) |
|
470 | |||
471 | /** |
||
472 | * Instantiates a client with the given config, a cache adapter and a PSR-3 compliant logger |
||
473 | * @param Config $config |
||
474 | * @param $cache |
||
475 | * @param LoggerInterface $logger |
||
476 | * @return static |
||
477 | */ |
||
478 | 30 | public static function ofConfigCacheAndLogger(Config $config, $cache, LoggerInterface $logger) |
|
482 | } |
||
483 |