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 $marketCategory; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | private $adult; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $salesNotes; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $manufacturerWarranty; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $pickup; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $downloadable; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $delivery; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var float |
||
| 96 | */ |
||
| 97 | private $localDeliveryCost; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $description; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $countryOfOrigin; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private $weight; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | private $cpa; |
||
| 118 | |||
| 119 | /** @var string[] */ |
||
| 120 | private $barcodes; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | private $pictures = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | private $params = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var bool |
||
| 134 | */ |
||
| 135 | private $store; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function toArray() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getId() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $id |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function setId($id) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function isAvailable() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param bool $available |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function setAvailable($available) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getUrl() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $url |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setUrl($url) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return float |
||
| 207 | */ |
||
| 208 | public function getPrice() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param float $price |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function setPrice($price) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return float |
||
| 227 | */ |
||
| 228 | public function getOldPrice() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param float $oldPrice |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setOldPrice($oldPrice) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getCurrencyId() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $currencyId |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function setCurrencyId($currencyId) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | public function getCategoryId() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param int $categoryId |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function setCategoryId($categoryId) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getCategoriesId() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $categoriesId |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function setCategoriesId(array $categoriesId) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getMarketCategory() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $marketCategory |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setMarketCategory($marketCategory) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function isAdult() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param bool $adult |
||
| 335 | * |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function setAdult($adult) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getSalesNotes() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $salesNotes |
||
| 355 | * |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function setSalesNotes($salesNotes) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function isManufacturerWarranty() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param bool $manufacturerWarranty |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function setManufacturerWarranty($manufacturerWarranty) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function isPickup() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param bool $pickup |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function setPickup($pickup) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function isDownloadable() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param bool $downloadable |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function setDownloadable($downloadable) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function isDelivery() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param bool $delivery |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | public function setDelivery($delivery) |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * @param bool $store |
||
| 448 | * |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function setStore($store) |
||
| 452 | { |
||
| 453 | $this->store = $store; |
||
| 454 | |||
| 455 | return $this; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function isStore() |
||
| 462 | { |
||
| 463 | return $this->store; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param bool $store |
||
| 468 | * |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function setStore($store) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function isStore() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return float |
||
| 488 | */ |
||
| 489 | public function getLocalDeliveryCost() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param float $localDeliveryCost |
||
| 496 | * |
||
| 497 | * @return $this |
||
| 498 | */ |
||
| 499 | public function setLocalDeliveryCost($localDeliveryCost) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public function getDescription() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $description |
||
| 516 | * |
||
| 517 | * @return $this |
||
| 518 | */ |
||
| 519 | public function setDescription($description) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function getCountryOfOrigin() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $countryOfOrigin |
||
| 536 | * |
||
| 537 | * @return $this |
||
| 538 | */ |
||
| 539 | public function setCountryOfOrigin($countryOfOrigin) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function getWeight() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param string $weight |
||
| 556 | * |
||
| 557 | * @return $this |
||
| 558 | */ |
||
| 559 | public function setWeight($weight) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @return int |
||
| 568 | */ |
||
| 569 | public function getCpa() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param int $cpa |
||
| 576 | * |
||
| 577 | * @return $this |
||
| 578 | */ |
||
| 579 | public function setCpa($cpa) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @return array |
||
| 588 | */ |
||
| 589 | public function getParams() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @param OfferParam $param |
||
| 596 | * |
||
| 597 | * @return $this |
||
| 598 | */ |
||
| 599 | public function addParam(OfferParam $param) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Add picture |
||
| 608 | * |
||
| 609 | * @param string $url |
||
| 610 | * |
||
| 611 | * @return $this |
||
| 612 | */ |
||
| 613 | public function addPicture($url) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Set pictures |
||
| 624 | * |
||
| 625 | * @param array $pictures |
||
| 626 | * |
||
| 627 | * @return $this |
||
| 628 | */ |
||
| 629 | public function setPictures(array $pictures) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get picture list |
||
| 638 | * |
||
| 639 | * @return array |
||
| 640 | */ |
||
| 641 | public function getPictures() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get list of barcodes of the offer |
||
| 648 | * |
||
| 649 | * @return string[] |
||
| 650 | */ |
||
| 651 | public function getBarcodes() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Set list of barcodes for that offer |
||
| 658 | * |
||
| 659 | * @param string[] $barcodes |
||
| 660 | * |
||
| 661 | * @return $this |
||
| 662 | */ |
||
| 663 | public function setBarcodes(array $barcodes = []) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Add one barcode to the collection of barcodes of this offer |
||
| 672 | * |
||
| 673 | * @param string $barcode |
||
| 674 | * |
||
| 675 | * @return AbstractOffer |
||
| 676 | */ |
||
| 677 | public function addBarcode($barcode) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @return array |
||
| 686 | */ |
||
| 687 | abstract protected function getOptions(); |
||
| 688 | |||
| 689 | /** |
||
| 690 | * @return array |
||
| 691 | */ |
||
| 692 | private function getHeaderOptions() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @return array |
||
| 715 | */ |
||
| 716 | private function getFooterOptions() |
||
| 729 | } |
||
| 730 |