Complex classes like AbstractCURLRequest 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 AbstractCURLRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | abstract class AbstractCURLRequest implements CURLRequestInterface, HTTPInterface { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Content-Type "application/x-www-form-urlencoded". |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const CONTENT_TYPE_X_WWW_FORM_URLENCODED = "Content-Type: application/x-www-form-urlencoded"; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Configuration. |
||
| 42 | * |
||
| 43 | * @var CURLConfiguration |
||
| 44 | */ |
||
| 45 | private $configuration; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Headers. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $headers = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Method. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $method = self::HTTP_METHOD_GET; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * POST data. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $postData = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Query data. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $queryData = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Resource path. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $resourcePath; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Constructor. |
||
| 84 | * |
||
| 85 | * @param string $method The Method. |
||
| 86 | * @param CURLConfiguration $configuration The configuration. |
||
| 87 | * @param string $resourcePath The resource path. |
||
| 88 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is not implemented. |
||
| 89 | */ |
||
| 90 | protected function __construct($method, CURLConfiguration $configuration, $resourcePath) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function addHeader($name, $value) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function addPostData($name, $value) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function addQueryData($name, $value) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function call() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function clearHeaders() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function clearPostData() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function clearQueryData() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | public function getConfiguration() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function getHeaders() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function getMethod() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function getPostData() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function getQueryData() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function getResourcePath() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Merge the headers. |
||
| 263 | * |
||
| 264 | * @return array Returns the merged headers. |
||
| 265 | */ |
||
| 266 | private function mergeHeaders() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Merge the URL. |
||
| 278 | * |
||
| 279 | * @return string Returns the merged URL. |
||
| 280 | */ |
||
| 281 | private function mergeURL() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Parse the raw header. |
||
| 296 | * |
||
| 297 | * @param string $rawHeader The raw header. |
||
| 298 | * @return array Returns the headers. |
||
| 299 | */ |
||
| 300 | private function parseHeader($rawHeader) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function removeHeader($name) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | public function removePostData($name) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | public function removeQueryData($name) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set the configuration. |
||
| 361 | * |
||
| 362 | * @param CURLConfiguration $configuration The configuration. |
||
| 363 | * @return CURLRequestInterface Returns this cURL request. |
||
| 364 | */ |
||
| 365 | protected function setConfiguration(CURLConfiguration $configuration) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set cURL POST. |
||
| 372 | * |
||
| 373 | * @param resource $stream The stream. |
||
| 374 | * @param string $postData The POST data. |
||
| 375 | * @return CURLRequestInterface Returns this cURL request. |
||
| 376 | */ |
||
| 377 | protected function setCurlPost($stream, $postData) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set cURL proxy. |
||
| 404 | * |
||
| 405 | * @param resource $stream The stream. |
||
| 406 | * @return CURLRequestInterface Returns this cURL request. |
||
| 407 | */ |
||
| 408 | protected function setCurlProxy($stream) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set cURL return transfer. |
||
| 431 | * |
||
| 432 | * @param resource $stream The stream. |
||
| 433 | * @return CURLRequestInterface Returns this cURL request. |
||
| 434 | */ |
||
| 435 | protected function setCurlReturnTransfer($stream) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Set cURL SSL. |
||
| 442 | * |
||
| 443 | * @param resource $stream The stream. |
||
| 444 | * @return CURLRequestInterface Returns this cURL request. |
||
| 445 | */ |
||
| 446 | protected function setCurlSsl($stream) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Set cURL timeout. |
||
| 458 | * |
||
| 459 | * @param resource $stream The stream. |
||
| 460 | * @return CURLRequestInterface Returns this cURL request. |
||
| 461 | */ |
||
| 462 | protected function setCurlTimeout($stream) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set cURL user agent. |
||
| 473 | * |
||
| 474 | * @param resource $stream The stream. |
||
| 475 | * @return CURLRequestInterface Returns this cURL request. |
||
| 476 | */ |
||
| 477 | protected function setCurlUserAgent($stream) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set cURL verbose. |
||
| 484 | * |
||
| 485 | * @param resource $stream The stream. |
||
| 486 | * @param string $url The URL. |
||
| 487 | * @param string $postData The POST data. |
||
| 488 | * @return CURLRequestInterface Returns this cURL request. |
||
| 489 | * @throws Exception Throws an exception if an error occurs. |
||
| 490 | */ |
||
| 491 | protected function setCurlVerbose($stream, $url, $postData) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Set the headers. |
||
| 514 | * |
||
| 515 | * @param array $headers The headers. |
||
| 516 | * @return CURLRequestInterface Returns this cURL request. |
||
| 517 | */ |
||
| 518 | protected function setHeaders(array $headers) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set the method. |
||
| 525 | * |
||
| 526 | * @param string $method The method. |
||
| 527 | * @return CURLRequestInterface Returns this cURL request. |
||
| 528 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is not implemented. |
||
| 529 | */ |
||
| 530 | protected function setMethod($method) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Set the POST data. |
||
| 549 | * |
||
| 550 | * @param array $postData The POST data. |
||
| 551 | * @return CURLRequestInterface Returns this cURL request. |
||
| 552 | */ |
||
| 553 | protected function setPostData(array $postData) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Set the query data. |
||
| 560 | * |
||
| 561 | * @param array $queryData The query data. |
||
| 562 | * @return CURLRequestInterface Returns this cURL request. |
||
| 563 | */ |
||
| 564 | protected function setQueryData(array $queryData) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * {@inheritdoc} |
||
| 571 | */ |
||
| 572 | public function setResourcePath($resourcePath) { |
||
| 576 | } |
||
| 577 |