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 |
||
| 29 | abstract class AbstractCURLRequest implements CURLRequestInterface, HTTPInterface { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Content-type application/x-www-form-urlencoded |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | const CONTENT_TYPE_X_WWW_FORM_URLENCODED = "Content-Type: application/x-www-form-urlencoded"; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Configuration. |
||
| 40 | * |
||
| 41 | * @var CURLConfiguration |
||
| 42 | */ |
||
| 43 | private $configuration; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Headers. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $headers = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Method. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $method = self::HTTP_METHOD_GET; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * POST data. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $postData = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Query data. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $queryData = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Resource path. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private $resourcePath; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructor. |
||
| 82 | * |
||
| 83 | * @param string $method The Method. |
||
| 84 | * @param CURLConfiguration $configuration The configuration. |
||
| 85 | * @param string $resourcePath The resource path. |
||
| 86 | * @throws InvalidHTTPMethodException Throws an invalid HTTP method exception if the method is not implemented. |
||
| 87 | */ |
||
| 88 | protected function __construct($method, CURLConfiguration $configuration, $resourcePath) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function addHeader($name, $value) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function addPostData($name, $value) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | public function addQueryData($name, $value) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function call() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function clearHeaders() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function clearPostData() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | public function clearQueryData() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function getConfiguration() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | public function getHeaders() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function getMethod() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | public function getPostData() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | */ |
||
| 325 | public function getQueryData() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function getResourcePath() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Merge the headers. |
||
| 338 | * |
||
| 339 | * @return array Returns the meged headers. |
||
| 340 | */ |
||
| 341 | private function mergeHeaders() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Merge the URL. |
||
| 357 | * |
||
| 358 | * @return string Returns the merged URL. |
||
| 359 | */ |
||
| 360 | private function mergeURL() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Parse the raw header. |
||
| 375 | * |
||
| 376 | * @param string $rawHeader The raw header. |
||
| 377 | * @return array Returns the headers. |
||
| 378 | */ |
||
| 379 | private function parseHeader($rawHeader) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | public function removeHeader($name) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | public function removePostData($name) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | public function removeQueryData($name) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Set the configuration. |
||
| 443 | * |
||
| 444 | * @param CURLConfiguration $configuration The configuration. |
||
| 445 | * @return AbstractCURLRequest Returns this request. |
||
| 446 | */ |
||
| 447 | protected function setConfiguration(CURLConfiguration $configuration) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set the headers. |
||
| 454 | * |
||
| 455 | * @param array $headers The headers. |
||
| 456 | * @return AbstractCURLRequest Returns this request. |
||
| 457 | */ |
||
| 458 | protected function setHeaders(array $headers = []) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Set the method. |
||
| 465 | * |
||
| 466 | * @param string $method The method. |
||
| 467 | * @return AbstractCURLRequest Returns this request. |
||
| 468 | * @throws InvalidHTTPMethodException Throws an invalid HTTP method exception if the method is not implemented. |
||
| 469 | */ |
||
| 470 | protected function setMethod($method) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set the POST data. |
||
| 489 | * |
||
| 490 | * @param array $postData The POST data. |
||
| 491 | * @return AbstractCURLRequest Returns this request. |
||
| 492 | */ |
||
| 493 | protected function setPostData(array $postData = []) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Set the query data. |
||
| 500 | * |
||
| 501 | * @param array $queryData The query data. |
||
| 502 | * @return AbstractCURLRequest Returns this request. |
||
| 503 | */ |
||
| 504 | protected function setQueryData(array $queryData = []) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritdoc} |
||
| 511 | */ |
||
| 512 | public function setResourcePath($resourcePath) { |
||
| 516 | |||
| 517 | } |
||
| 518 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.