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 float |
||
| 48 | */ |
||
| 49 | private $purchasePrice; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $currencyId; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | private $categoryId; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $categoriesId = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $name; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $marketCategory; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $adult; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $salesNotes; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $manufacturerWarranty; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | private $pickup; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var bool |
||
| 98 | */ |
||
| 99 | private $downloadable; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private $delivery; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var float |
||
| 108 | */ |
||
| 109 | private $localDeliveryCost; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private $deliveryOptions = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | private $description; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | private $countryOfOrigin; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | private $weight; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | private $dimensions; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var int |
||
| 138 | */ |
||
| 139 | private $cpa; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string[] |
||
| 143 | */ |
||
| 144 | private $barcodes; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | private $pictures = []; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $params = []; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var bool |
||
| 158 | */ |
||
| 159 | private $store; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var bool |
||
| 163 | */ |
||
| 164 | private $autoDiscount; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Array of custom elements (element types are keys) of arrays of element values |
||
| 168 | * There may be multiple elements of the same type |
||
| 169 | * |
||
| 170 | * @var array[] |
||
| 171 | */ |
||
| 172 | private $customElements; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var OfferCondition |
||
| 176 | */ |
||
| 177 | private $condition; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function toArray() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getId() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $id |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | public function setId($id) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function isAvailable() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param bool $available |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function setAvailable($available) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getUrl() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $url |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function setUrl($url) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return float |
||
| 249 | */ |
||
| 250 | public function getPrice() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param float $price |
||
| 257 | * |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | public function setPrice($price) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return float |
||
| 269 | */ |
||
| 270 | public function getOldPrice() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param float $oldPrice |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function setOldPrice($oldPrice) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return float |
||
| 289 | */ |
||
| 290 | public function getPurchasePrice() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param float $purchasePrice |
||
| 297 | * |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function setPurchasePrice($purchasePrice) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getCurrencyId() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $currencyId |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function setCurrencyId($currencyId) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return int |
||
| 329 | */ |
||
| 330 | public function getCategoryId() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param int $categoryId |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function setCategoryId($categoryId) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getCategoriesId() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $categoriesId |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setCategoriesId(array $categoriesId) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function getName() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $name |
||
| 377 | * |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | public function setName($name) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function getMarketCategory() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $marketCategory |
||
| 397 | * |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | public function setMarketCategory($marketCategory) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | public function isAdult() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param bool $adult |
||
| 417 | * |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function setAdult($adult) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getSalesNotes() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param string $salesNotes |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setSalesNotes($salesNotes) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | public function isManufacturerWarranty() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param bool $manufacturerWarranty |
||
| 457 | * |
||
| 458 | * @return $this |
||
| 459 | */ |
||
| 460 | public function setManufacturerWarranty($manufacturerWarranty) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @return bool |
||
| 469 | */ |
||
| 470 | public function isPickup() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param bool $pickup |
||
| 477 | * |
||
| 478 | * @return $this |
||
| 479 | */ |
||
| 480 | public function setPickup($pickup) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | public function isDownloadable() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param bool $downloadable |
||
| 497 | * |
||
| 498 | * @return $this |
||
| 499 | */ |
||
| 500 | public function setDownloadable($downloadable) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | public function isDelivery() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param bool $delivery |
||
| 517 | * |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | public function setDelivery($delivery) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | public function getDeliveryOptions() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @param Delivery $option |
||
| 537 | * |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function addDeliveryOption(Delivery $option) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param bool $store |
||
| 549 | * |
||
| 550 | * @return $this |
||
| 551 | */ |
||
| 552 | public function setStore($store) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | public function isStore() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return float |
||
| 569 | */ |
||
| 570 | public function getLocalDeliveryCost() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param float $localDeliveryCost |
||
| 577 | * |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | public function setLocalDeliveryCost($localDeliveryCost) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function getDescription() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param string $description |
||
| 597 | * |
||
| 598 | * @return $this |
||
| 599 | */ |
||
| 600 | public function setDescription($description) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | public function getCountryOfOrigin() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param string $countryOfOrigin |
||
| 617 | * |
||
| 618 | * @return $this |
||
| 619 | */ |
||
| 620 | public function setCountryOfOrigin($countryOfOrigin) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function getWeight() |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param string $weight |
||
| 637 | * |
||
| 638 | * @return $this |
||
| 639 | */ |
||
| 640 | public function setWeight($weight) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | public function getDimensions() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param float $length |
||
| 657 | * @param float $width |
||
| 658 | * @param float $height |
||
| 659 | * |
||
| 660 | * @return $this |
||
| 661 | */ |
||
| 662 | public function setDimensions($length, $width, $height) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @return int |
||
| 677 | */ |
||
| 678 | public function getCpa() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param int $cpa |
||
| 685 | * |
||
| 686 | * @return $this |
||
| 687 | */ |
||
| 688 | public function setCpa($cpa) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @return array |
||
| 697 | */ |
||
| 698 | public function getParams() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @param OfferParam $param |
||
| 705 | * |
||
| 706 | * @return $this |
||
| 707 | */ |
||
| 708 | public function addParam(OfferParam $param) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Add picture |
||
| 717 | * |
||
| 718 | * @param string $url |
||
| 719 | * |
||
| 720 | * @return $this |
||
| 721 | */ |
||
| 722 | public function addPicture($url) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Set pictures |
||
| 733 | * |
||
| 734 | * @param array $pictures |
||
| 735 | * |
||
| 736 | * @return $this |
||
| 737 | */ |
||
| 738 | public function setPictures(array $pictures) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Get picture list |
||
| 747 | * |
||
| 748 | * @return array |
||
| 749 | */ |
||
| 750 | public function getPictures() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Get list of barcodes of the offer |
||
| 757 | * |
||
| 758 | * @return string[] |
||
| 759 | */ |
||
| 760 | public function getBarcodes() |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Set list of barcodes for that offer |
||
| 767 | * |
||
| 768 | * @param string[] $barcodes |
||
| 769 | * |
||
| 770 | * @return $this |
||
| 771 | */ |
||
| 772 | public function setBarcodes(array $barcodes = []) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Add one barcode to the collection of barcodes of this offer |
||
| 781 | * |
||
| 782 | * @param string $barcode |
||
| 783 | * |
||
| 784 | * @return $this |
||
| 785 | */ |
||
| 786 | public function addBarcode($barcode) |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Sets list of custom elements |
||
| 795 | * |
||
| 796 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
| 797 | * |
||
| 798 | * @return $this |
||
| 799 | */ |
||
| 800 | public function setCustomElements(array $customElements = []) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Add a custom element with given type and value |
||
| 809 | * Multiple elements of the same type are supported |
||
| 810 | * |
||
| 811 | * @param string $elementType |
||
| 812 | * @param mixed $value |
||
| 813 | * |
||
| 814 | * @return $this |
||
| 815 | */ |
||
| 816 | public function addCustomElement($elementType, $value) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Returns a list of custom elements |
||
| 828 | * Always returns an array even if no custom elements were added |
||
| 829 | * |
||
| 830 | * @return array |
||
| 831 | */ |
||
| 832 | public function getCustomElements() |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Returns a list of values for the specified custom element type |
||
| 839 | * Always returns an array |
||
| 840 | * |
||
| 841 | * @param string $elementType |
||
| 842 | * |
||
| 843 | * @return array |
||
| 844 | */ |
||
| 845 | public function getCustomElementByType($elementType) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @return OfferCondition |
||
| 857 | */ |
||
| 858 | public function getCondition() |
||
| 862 | |||
| 863 | /** |
||
| 864 | * @param OfferCondition $condition |
||
| 865 | * |
||
| 866 | * @return $this |
||
| 867 | */ |
||
| 868 | public function addCondition(OfferCondition $condition) |
||
| 874 | |||
| 875 | /** |
||
| 876 | * @return bool |
||
| 877 | */ |
||
| 878 | public function getAutoDiscount() |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @param bool $autoDiscount |
||
| 885 | * |
||
| 886 | * @return $this |
||
| 887 | */ |
||
| 888 | public function setAutoDiscount($autoDiscount) |
||
| 894 | |||
| 895 | /** |
||
| 896 | * @return array |
||
| 897 | */ |
||
| 898 | abstract protected function getOptions(); |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @return array |
||
| 902 | */ |
||
| 903 | private function getHeaderOptions() |
||
| 927 | |||
| 928 | /** |
||
| 929 | * @return array |
||
| 930 | */ |
||
| 931 | private function getFooterOptions() |
||
| 944 | } |
||
| 945 |