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 |
||
| 31 | abstract class AbstractCurlRequest implements CurlRequestInterface, HttpInterface { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Content-Type "application/x-www-form-urlencoded". |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | const CONTENT_TYPE_X_WWW_FORM_URLENCODED = "Content-Type: application/x-www-form-urlencoded"; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Configuration. |
||
| 41 | * |
||
| 42 | * @var CurlConfiguration |
||
| 43 | */ |
||
| 44 | private $configuration; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Headers. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $headers = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Method. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $method = self::HTTP_METHOD_GET; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * POST data. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $postData = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Query data. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $queryData = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Resource path. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $resourcePath; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Constructor. |
||
| 83 | * |
||
| 84 | * @param string $method The Method. |
||
| 85 | * @param CurlConfiguration $configuration The configuration. |
||
| 86 | * @param string $resourcePath The resource path. |
||
| 87 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
| 88 | */ |
||
| 89 | protected function __construct($method, CurlConfiguration $configuration, $resourcePath) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function addHeader($name, $value) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | public function addPostData($name, $value) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | public function addQueryData($name, $value) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function call() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public function clearHeaders() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function clearPostData() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | public function clearQueryData() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | public function getConfiguration() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function getHeaders() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function getMethod() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function getPostData() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function getQueryData() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function getResourcePath() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Merge the headers. |
||
| 262 | * |
||
| 263 | * @return array Returns the merged headers. |
||
| 264 | */ |
||
| 265 | private function mergeHeaders() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Merge the URL. |
||
| 277 | * |
||
| 278 | * @return string Returns the merged URL. |
||
| 279 | */ |
||
| 280 | private function mergeURL() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Parse the raw header. |
||
| 295 | * |
||
| 296 | * @param string $rawHeader The raw header. |
||
| 297 | * @return array Returns the headers. |
||
| 298 | */ |
||
| 299 | private function parseHeader($rawHeader) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function removeHeader($name) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function removePostData($name) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function removeQueryData($name) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set the configuration. |
||
| 360 | * |
||
| 361 | * @param CurlConfiguration $configuration The configuration. |
||
| 362 | * @return CurlRequestInterface Returns this cURL request. |
||
| 363 | */ |
||
| 364 | protected function setConfiguration(CurlConfiguration $configuration) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set cURL POST. |
||
| 371 | * |
||
| 372 | * @param resource $stream The stream. |
||
| 373 | * @param string $postData The POST data. |
||
| 374 | * @return CurlRequestInterface Returns this cURL request. |
||
| 375 | */ |
||
| 376 | protected function setCurlPost($stream, $postData) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Set cURL proxy. |
||
| 403 | * |
||
| 404 | * @param resource $stream The stream. |
||
| 405 | * @return CurlRequestInterface Returns this cURL request. |
||
| 406 | */ |
||
| 407 | protected function setCurlProxy($stream) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Set cURL return transfer. |
||
| 430 | * |
||
| 431 | * @param resource $stream The stream. |
||
| 432 | * @return CurlRequestInterface Returns this cURL request. |
||
| 433 | */ |
||
| 434 | protected function setCurlReturnTransfer($stream) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set cURL SSL. |
||
| 441 | * |
||
| 442 | * @param resource $stream The stream. |
||
| 443 | * @return CurlRequestInterface Returns this cURL request. |
||
| 444 | */ |
||
| 445 | protected function setCurlSsl($stream) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Set cURL timeout. |
||
| 457 | * |
||
| 458 | * @param resource $stream The stream. |
||
| 459 | * @return CurlRequestInterface Returns this cURL request. |
||
| 460 | */ |
||
| 461 | protected function setCurlTimeout($stream) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set cURL user agent. |
||
| 472 | * |
||
| 473 | * @param resource $stream The stream. |
||
| 474 | * @return CurlRequestInterface Returns this cURL request. |
||
| 475 | */ |
||
| 476 | protected function setCurlUserAgent($stream) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Set cURL verbose. |
||
| 483 | * |
||
| 484 | * @param resource $stream The stream. |
||
| 485 | * @param string $url The URL. |
||
| 486 | * @param string $postData The POST data. |
||
| 487 | * @return CurlRequestInterface Returns this cURL request. |
||
| 488 | * @throws Exception Throws an exception if an error occurs. |
||
| 489 | */ |
||
| 490 | protected function setCurlVerbose($stream, $url, $postData) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Set the headers. |
||
| 513 | * |
||
| 514 | * @param array $headers The headers. |
||
| 515 | * @return CurlRequestInterface Returns this cURL request. |
||
| 516 | */ |
||
| 517 | protected function setHeaders(array $headers) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Set the method. |
||
| 524 | * |
||
| 525 | * @param string $method The method. |
||
| 526 | * @return CurlRequestInterface Returns this cURL request. |
||
| 527 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
| 528 | */ |
||
| 529 | protected function setMethod($method) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Set the POST data. |
||
| 548 | * |
||
| 549 | * @param array $postData The POST data. |
||
| 550 | * @return CurlRequestInterface Returns this cURL request. |
||
| 551 | */ |
||
| 552 | protected function setPostData(array $postData) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Set the query data. |
||
| 559 | * |
||
| 560 | * @param array $queryData The query data. |
||
| 561 | * @return CurlRequestInterface Returns this cURL request. |
||
| 562 | */ |
||
| 563 | protected function setQueryData(array $queryData) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * {@inheritdoc} |
||
| 570 | */ |
||
| 571 | public function setResourcePath($resourcePath) { |
||
| 575 | } |
||
| 576 |