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 |
||
| 19 | abstract class AbstractOffer implements OfferInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $id; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | private $available; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $url; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var float |
||
| 38 | */ |
||
| 39 | private $price; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var float |
||
| 43 | */ |
||
| 44 | private $oldPrice; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $currencyId; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | private $categoryId; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $categoriesId = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $name; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $marketCategory; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | private $adult; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $salesNotes; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $manufacturerWarranty; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $pickup; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | private $downloadable; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var bool |
||
| 98 | */ |
||
| 99 | private $delivery; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var float |
||
| 103 | */ |
||
| 104 | private $localDeliveryCost; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private $deliveryOptions = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | private $description; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | private $countryOfOrigin; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | private $weight; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var int |
||
| 128 | */ |
||
| 129 | private $cpa; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string[] |
||
| 133 | */ |
||
| 134 | private $barcodes; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var array |
||
| 138 | */ |
||
| 139 | private $pictures = []; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | private $params = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var bool |
||
| 148 | */ |
||
| 149 | private $store; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var bool |
||
| 153 | */ |
||
| 154 | private $autoDiscount; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Array of custom elements (element types are keys) of arrays of element values |
||
| 158 | * There may be multiple elements of the same type |
||
| 159 | * |
||
| 160 | * @var array[] |
||
| 161 | */ |
||
| 162 | private $customElements; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var OfferCondition |
||
| 166 | */ |
||
| 167 | private $condition; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function toArray() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getId() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $id |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setId($id) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function isAvailable() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param bool $available |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function setAvailable($available) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getUrl() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $url |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | public function setUrl($url) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return float |
||
| 239 | */ |
||
| 240 | public function getPrice() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param float $price |
||
| 247 | * |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | public function setPrice($price) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return float |
||
| 259 | */ |
||
| 260 | public function getOldPrice() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param float $oldPrice |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function setOldPrice($oldPrice) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getCurrencyId() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $currencyId |
||
| 287 | * |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function setCurrencyId($currencyId) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return int |
||
| 299 | */ |
||
| 300 | public function getCategoryId() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param int $categoryId |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function setCategoryId($categoryId) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getCategoriesId() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param array $categoriesId |
||
| 327 | * |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function setCategoriesId(array $categoriesId) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getName() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $name |
||
| 347 | * |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function setName($name) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getMarketCategory() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $marketCategory |
||
| 367 | * |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | public function setMarketCategory($marketCategory) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function isAdult() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param bool $adult |
||
| 387 | * |
||
| 388 | * @return $this |
||
| 389 | */ |
||
| 390 | public function setAdult($adult) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getSalesNotes() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $salesNotes |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | public function setSalesNotes($salesNotes) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | public function isManufacturerWarranty() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param bool $manufacturerWarranty |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function setManufacturerWarranty($manufacturerWarranty) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | public function isPickup() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param bool $pickup |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function setPickup($pickup) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function isDownloadable() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param bool $downloadable |
||
| 467 | * |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | public function setDownloadable($downloadable) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | public function isDelivery() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param bool $delivery |
||
| 487 | * |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | public function setDelivery($delivery) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | public function getDeliveryOptions() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param Delivery $option |
||
| 507 | * |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | public function addDeliveryOption(Delivery $option) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param bool $store |
||
| 519 | * |
||
| 520 | * @return $this |
||
| 521 | */ |
||
| 522 | public function setStore($store) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return bool |
||
| 531 | */ |
||
| 532 | public function isStore() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @return float |
||
| 539 | */ |
||
| 540 | public function getLocalDeliveryCost() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param float $localDeliveryCost |
||
| 547 | * |
||
| 548 | * @return $this |
||
| 549 | */ |
||
| 550 | public function setLocalDeliveryCost($localDeliveryCost) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | public function getDescription() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $description |
||
| 567 | * |
||
| 568 | * @return $this |
||
| 569 | */ |
||
| 570 | public function setDescription($description) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | public function getCountryOfOrigin() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param string $countryOfOrigin |
||
| 587 | * |
||
| 588 | * @return $this |
||
| 589 | */ |
||
| 590 | public function setCountryOfOrigin($countryOfOrigin) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | public function getWeight() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param string $weight |
||
| 607 | * |
||
| 608 | * @return $this |
||
| 609 | */ |
||
| 610 | public function setWeight($weight) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @return int |
||
| 619 | */ |
||
| 620 | public function getCpa() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param int $cpa |
||
| 627 | * |
||
| 628 | * @return $this |
||
| 629 | */ |
||
| 630 | public function setCpa($cpa) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return array |
||
| 639 | */ |
||
| 640 | public function getParams() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param OfferParam $param |
||
| 647 | * |
||
| 648 | * @return $this |
||
| 649 | */ |
||
| 650 | public function addParam(OfferParam $param) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Add picture |
||
| 659 | * |
||
| 660 | * @param string $url |
||
| 661 | * |
||
| 662 | * @return $this |
||
| 663 | */ |
||
| 664 | public function addPicture($url) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Set pictures |
||
| 675 | * |
||
| 676 | * @param array $pictures |
||
| 677 | * |
||
| 678 | * @return $this |
||
| 679 | */ |
||
| 680 | public function setPictures(array $pictures) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Get picture list |
||
| 689 | * |
||
| 690 | * @return array |
||
| 691 | */ |
||
| 692 | public function getPictures() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Get list of barcodes of the offer |
||
| 699 | * |
||
| 700 | * @return string[] |
||
| 701 | */ |
||
| 702 | public function getBarcodes() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Set list of barcodes for that offer |
||
| 709 | * |
||
| 710 | * @param string[] $barcodes |
||
| 711 | * |
||
| 712 | * @return $this |
||
| 713 | */ |
||
| 714 | public function setBarcodes(array $barcodes = []) |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Add one barcode to the collection of barcodes of this offer |
||
| 723 | * |
||
| 724 | * @param string $barcode |
||
| 725 | * |
||
| 726 | * @return $this |
||
| 727 | */ |
||
| 728 | public function addBarcode($barcode) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Sets list of custom elements |
||
| 737 | * |
||
| 738 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
| 739 | * |
||
| 740 | * @return $this |
||
| 741 | */ |
||
| 742 | public function setCustomElements(array $customElements = []) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Add a custom element with given type and value |
||
| 751 | * Multiple elements of the same type are supported |
||
| 752 | * |
||
| 753 | * @param string $elementType |
||
| 754 | * @param mixed $value |
||
| 755 | * |
||
| 756 | * @return $this |
||
| 757 | */ |
||
| 758 | public function addCustomElement($elementType, $value) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Returns a list of custom elements |
||
| 770 | * Always returns an array even if no custom elements were added |
||
| 771 | * |
||
| 772 | * @return array |
||
| 773 | */ |
||
| 774 | public function getCustomElements() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Returns a list of values for the specified custom element type |
||
| 781 | * Always returns an array |
||
| 782 | * |
||
| 783 | * @param string $elementType |
||
| 784 | * |
||
| 785 | * @return array |
||
| 786 | */ |
||
| 787 | public function getCustomElementByType($elementType) |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @return OfferCondition |
||
| 799 | */ |
||
| 800 | public function getCondition() |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @param OfferCondition $condition |
||
| 807 | * |
||
| 808 | * @return $this |
||
| 809 | */ |
||
| 810 | public function addCondition(OfferCondition $condition) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | public function getAutoDiscount() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @param bool $autoDiscount |
||
| 827 | * |
||
| 828 | * @return $this |
||
| 829 | */ |
||
| 830 | public function setAutoDiscount($autoDiscount) |
||
| 836 | |||
| 837 | /** |
||
| 838 | * @return array |
||
| 839 | */ |
||
| 840 | abstract protected function getOptions(); |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @return array |
||
| 844 | */ |
||
| 845 | private function getHeaderOptions() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * @return array |
||
| 870 | */ |
||
| 871 | private function getFooterOptions() |
||
| 884 | } |
||
| 885 |