Complex classes like AbstractOffer 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 AbstractOffer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class AbstractOffer implements OfferInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $id; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | private $available; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $url; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var float |
||
| 36 | */ |
||
| 37 | private $price; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var float |
||
| 41 | */ |
||
| 42 | private $oldPrice; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $currencyId; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private $categoryId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $categoriesId = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $name; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $marketCategory; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | private $adult; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $salesNotes; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $manufacturerWarranty; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $pickup; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $downloadable; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | private $delivery; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var float |
||
| 101 | */ |
||
| 102 | private $localDeliveryCost; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $description; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private $countryOfOrigin; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private $weight; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | private $cpa; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var string[] |
||
| 126 | */ |
||
| 127 | private $barcodes; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private $pictures = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var array |
||
| 136 | */ |
||
| 137 | private $params = []; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var bool |
||
| 141 | */ |
||
| 142 | private $store; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Array of custom elements (element types are keys) of arrays of element values |
||
| 146 | * There may be multiple elements of the same type |
||
| 147 | * @var array[] |
||
| 148 | */ |
||
| 149 | private $customElements; |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @var OfferCondition |
||
| 154 | */ |
||
| 155 | private $condition; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function toArray() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getId() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $id |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function setId($id) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function isAvailable() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param bool $available |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setAvailable($available) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getUrl() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $url |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function setUrl($url) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return float |
||
| 227 | */ |
||
| 228 | public function getPrice() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param float $price |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setPrice($price) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return float |
||
| 247 | */ |
||
| 248 | public function getOldPrice() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param float $oldPrice |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function setOldPrice($oldPrice) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getCurrencyId() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $currencyId |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function setCurrencyId($currencyId) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return int |
||
| 287 | */ |
||
| 288 | public function getCategoryId() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param int $categoryId |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function setCategoryId($categoryId) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | public function getCategoriesId() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param array $categoriesId |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setCategoriesId(array $categoriesId) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public function getName() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param $name |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setName($name) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getMarketCategory() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $marketCategory |
||
| 354 | * |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setMarketCategory($marketCategory) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function isAdult() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param bool $adult |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function setAdult($adult) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function getSalesNotes() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $salesNotes |
||
| 394 | * |
||
| 395 | * @return $this |
||
| 396 | */ |
||
| 397 | public function setSalesNotes($salesNotes) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function isManufacturerWarranty() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param bool $manufacturerWarranty |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function setManufacturerWarranty($manufacturerWarranty) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | public function isPickup() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param bool $pickup |
||
| 434 | * |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | public function setPickup($pickup) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | public function isDownloadable() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param bool $downloadable |
||
| 454 | * |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function setDownloadable($downloadable) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | public function isDelivery() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param bool $delivery |
||
| 474 | * |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | public function setDelivery($delivery) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param bool $store |
||
| 486 | * |
||
| 487 | * @return $this |
||
| 488 | */ |
||
| 489 | public function setStore($store) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | public function isStore() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return float |
||
| 506 | */ |
||
| 507 | public function getLocalDeliveryCost() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param float $localDeliveryCost |
||
| 514 | * |
||
| 515 | * @return $this |
||
| 516 | */ |
||
| 517 | public function setLocalDeliveryCost($localDeliveryCost) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function getDescription() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param string $description |
||
| 534 | * |
||
| 535 | * @return $this |
||
| 536 | */ |
||
| 537 | public function setDescription($description) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | public function getCountryOfOrigin() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param string $countryOfOrigin |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function setCountryOfOrigin($countryOfOrigin) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function getWeight() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param string $weight |
||
| 574 | * |
||
| 575 | * @return $this |
||
| 576 | */ |
||
| 577 | public function setWeight($weight) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @return int |
||
| 586 | */ |
||
| 587 | public function getCpa() |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @param int $cpa |
||
| 594 | * |
||
| 595 | * @return $this |
||
| 596 | */ |
||
| 597 | public function setCpa($cpa) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @return array |
||
| 606 | */ |
||
| 607 | public function getParams() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param OfferParam $param |
||
| 614 | * |
||
| 615 | * @return $this |
||
| 616 | */ |
||
| 617 | public function addParam(OfferParam $param) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Add picture |
||
| 626 | * |
||
| 627 | * @param string $url |
||
| 628 | * |
||
| 629 | * @return $this |
||
| 630 | */ |
||
| 631 | public function addPicture($url) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Set pictures |
||
| 642 | * |
||
| 643 | * @param array $pictures |
||
| 644 | * |
||
| 645 | * @return $this |
||
| 646 | */ |
||
| 647 | public function setPictures(array $pictures) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get picture list |
||
| 656 | * |
||
| 657 | * @return array |
||
| 658 | */ |
||
| 659 | public function getPictures() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Get list of barcodes of the offer |
||
| 666 | * |
||
| 667 | * @return string[] |
||
| 668 | */ |
||
| 669 | public function getBarcodes() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Set list of barcodes for that offer |
||
| 676 | * |
||
| 677 | * @param string[] $barcodes |
||
| 678 | * |
||
| 679 | * @return $this |
||
| 680 | */ |
||
| 681 | public function setBarcodes(array $barcodes = []) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Add one barcode to the collection of barcodes of this offer |
||
| 690 | * |
||
| 691 | * @param string $barcode |
||
| 692 | * |
||
| 693 | * @return AbstractOffer |
||
| 694 | */ |
||
| 695 | public function addBarcode($barcode) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Sets list of custom elements |
||
| 704 | * |
||
| 705 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
| 706 | * |
||
| 707 | * @return $this |
||
| 708 | */ |
||
| 709 | public function setCustomElements(array $customElements = []) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Add a custom element with given type and value |
||
| 718 | * Multiple elements of the same type are supported |
||
| 719 | * |
||
| 720 | * @param string $elementType |
||
| 721 | * @param mixed $value |
||
| 722 | * |
||
| 723 | * @return $this |
||
| 724 | */ |
||
| 725 | public function addCustomElement($elementType, $value) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Returns a list of custom elements |
||
| 737 | * Always returns an array even if no custom elements were added |
||
| 738 | * |
||
| 739 | * @return array |
||
| 740 | */ |
||
| 741 | public function getCustomElements() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Returns a list of values for the specified custom element type |
||
| 748 | * Always returns an array |
||
| 749 | * |
||
| 750 | * @param string $elementType |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | public function getCustomElementByType($elementType) |
||
| 763 | |||
| 764 | |||
| 765 | /** |
||
| 766 | * @return OfferCondition |
||
| 767 | */ |
||
| 768 | public function getCondition() |
||
| 772 | |||
| 773 | |||
| 774 | /** |
||
| 775 | * @param OfferCondition $condition |
||
| 776 | * |
||
| 777 | * @return $this |
||
| 778 | */ |
||
| 779 | public function addCondition(OfferCondition $condition) |
||
| 785 | |||
| 786 | |||
| 787 | /** |
||
| 788 | * @return array |
||
| 789 | */ |
||
| 790 | abstract protected function getOptions(); |
||
| 791 | |||
| 792 | /** |
||
| 793 | * @return array |
||
| 794 | */ |
||
| 795 | private function getHeaderOptions() |
||
| 816 | |||
| 817 | /** |
||
| 818 | * @return array |
||
| 819 | */ |
||
| 820 | private function getFooterOptions() |
||
| 833 | } |
||
| 834 |