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 string |
||
| 56 | */ |
||
| 57 | private $marketCategory; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | private $adult; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $salesNotes; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | private $manufacturerWarranty; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $pickup; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $downloadable; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $delivery; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var float |
||
| 91 | */ |
||
| 92 | private $localDeliveryCost; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $description; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $countryOfOrigin; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $weight; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | private $cpa; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string[] |
||
| 116 | */ |
||
| 117 | private $barcodes; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $pictures = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | private $params = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | private $store; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Array of custom elements (element types are keys) of arrays of element values |
||
| 136 | * There may be multiple elements of the same type |
||
| 137 | * @var array[] |
||
| 138 | */ |
||
| 139 | private $customElements; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function toArray() |
||
| 145 | { |
||
| 146 | return \array_merge($this->getHeaderOptions(), $this->getOptions(), $this->getFooterOptions()); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function getId() |
||
| 153 | { |
||
| 154 | return $this->id; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $id |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function setId($id) |
||
| 163 | { |
||
| 164 | $this->id = $id; |
||
| 165 | |||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function isAvailable() |
||
| 173 | { |
||
| 174 | return $this->available; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param bool $available |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setAvailable($available) |
||
| 183 | { |
||
| 184 | $this->available = $available; |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getUrl() |
||
| 193 | { |
||
| 194 | return $this->url; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $url |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function setUrl($url) |
||
| 203 | { |
||
| 204 | $this->url = $url; |
||
| 205 | |||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return float |
||
| 211 | */ |
||
| 212 | public function getPrice() |
||
| 213 | { |
||
| 214 | return $this->price; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param float $price |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function setPrice($price) |
||
| 223 | { |
||
| 224 | $this->price = $price; |
||
| 225 | |||
| 226 | return $this; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return float |
||
| 231 | */ |
||
| 232 | public function getOldPrice() |
||
| 233 | { |
||
| 234 | return $this->oldPrice; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param float $oldPrice |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function setOldPrice($oldPrice) |
||
| 243 | { |
||
| 244 | $this->oldPrice = $oldPrice; |
||
| 245 | |||
| 246 | return $this; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getCurrencyId() |
||
| 253 | { |
||
| 254 | return $this->currencyId; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $currencyId |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function setCurrencyId($currencyId) |
||
| 263 | { |
||
| 264 | $this->currencyId = $currencyId; |
||
| 265 | |||
| 266 | return $this; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function getCategoryId() |
||
| 273 | { |
||
| 274 | return $this->categoryId; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param int $categoryId |
||
| 279 | * |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function setCategoryId($categoryId) |
||
| 283 | { |
||
| 284 | $this->categoryId = $categoryId; |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getMarketCategory() |
||
| 293 | { |
||
| 294 | return $this->marketCategory; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $marketCategory |
||
| 299 | * |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function setMarketCategory($marketCategory) |
||
| 303 | { |
||
| 304 | $this->marketCategory = $marketCategory; |
||
| 305 | |||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function isAdult() |
||
| 313 | { |
||
| 314 | return $this->adult; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param bool $adult |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function setAdult($adult) |
||
| 323 | { |
||
| 324 | $this->adult = $adult; |
||
| 325 | |||
| 326 | return $this; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function getSalesNotes() |
||
| 333 | { |
||
| 334 | return $this->salesNotes; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $salesNotes |
||
| 339 | * |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function setSalesNotes($salesNotes) |
||
| 343 | { |
||
| 344 | $this->salesNotes = $salesNotes; |
||
| 345 | |||
| 346 | return $this; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function isManufacturerWarranty() |
||
| 353 | { |
||
| 354 | return $this->manufacturerWarranty; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param bool $manufacturerWarranty |
||
| 359 | * |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | public function setManufacturerWarranty($manufacturerWarranty) |
||
| 363 | { |
||
| 364 | $this->manufacturerWarranty = $manufacturerWarranty; |
||
| 365 | |||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function isPickup() |
||
| 373 | { |
||
| 374 | return $this->pickup; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param bool $pickup |
||
| 379 | * |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function setPickup($pickup) |
||
| 383 | { |
||
| 384 | $this->pickup = $pickup; |
||
| 385 | |||
| 386 | return $this; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | public function isDownloadable() |
||
| 393 | { |
||
| 394 | return $this->downloadable; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param bool $downloadable |
||
| 399 | * |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | public function setDownloadable($downloadable) |
||
| 403 | { |
||
| 404 | $this->downloadable = $downloadable; |
||
| 405 | |||
| 406 | return $this; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function isDelivery() |
||
| 413 | { |
||
| 414 | return $this->delivery; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param bool $delivery |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | public function setDelivery($delivery) |
||
| 423 | { |
||
| 424 | $this->delivery = $delivery; |
||
| 425 | |||
| 426 | return $this; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param bool $store |
||
| 431 | * |
||
| 432 | * @return $this |
||
| 433 | */ |
||
| 434 | public function setStore($store) |
||
| 435 | { |
||
| 436 | $this->store = $store; |
||
| 437 | |||
| 438 | return $this; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function isStore() |
||
| 445 | { |
||
| 446 | return $this->store; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return float |
||
| 451 | */ |
||
| 452 | public function getLocalDeliveryCost() |
||
| 453 | { |
||
| 454 | return $this->localDeliveryCost; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param float $localDeliveryCost |
||
| 459 | * |
||
| 460 | * @return $this |
||
| 461 | */ |
||
| 462 | public function setLocalDeliveryCost($localDeliveryCost) |
||
| 463 | { |
||
| 464 | $this->localDeliveryCost = $localDeliveryCost; |
||
| 465 | |||
| 466 | return $this; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getDescription() |
||
| 473 | { |
||
| 474 | return $this->description; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $description |
||
| 479 | * |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function setDescription($description) |
||
| 483 | { |
||
| 484 | $this->description = $description; |
||
| 485 | |||
| 486 | return $this; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public function getCountryOfOrigin() |
||
| 493 | { |
||
| 494 | return $this->countryOfOrigin; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param string $countryOfOrigin |
||
| 499 | * |
||
| 500 | * @return $this |
||
| 501 | */ |
||
| 502 | public function setCountryOfOrigin($countryOfOrigin) |
||
| 503 | { |
||
| 504 | $this->countryOfOrigin = $countryOfOrigin; |
||
| 505 | |||
| 506 | return $this; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getWeight() |
||
| 513 | { |
||
| 514 | return $this->weight; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param string $weight |
||
| 519 | * |
||
| 520 | * @return $this |
||
| 521 | */ |
||
| 522 | public function setWeight($weight) |
||
| 523 | { |
||
| 524 | $this->weight = $weight; |
||
| 525 | |||
| 526 | return $this; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return int |
||
| 531 | */ |
||
| 532 | public function getCpa() |
||
| 533 | { |
||
| 534 | return $this->cpa; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param int $cpa |
||
| 539 | * |
||
| 540 | * @return $this |
||
| 541 | */ |
||
| 542 | public function setCpa($cpa) |
||
| 543 | { |
||
| 544 | $this->cpa = $cpa; |
||
| 545 | |||
| 546 | return $this; |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function getParams() |
||
| 553 | { |
||
| 554 | return $this->params; |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param OfferParam $param |
||
| 559 | * |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | public function addParam(OfferParam $param) |
||
| 563 | { |
||
| 564 | $this->params[] = $param; |
||
| 565 | |||
| 566 | return $this; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Add picture |
||
| 571 | * |
||
| 572 | * @param string $url |
||
| 573 | * |
||
| 574 | * @return $this |
||
| 575 | */ |
||
| 576 | public function addPicture($url) |
||
| 577 | { |
||
| 578 | if (\count($this->pictures) < 10) { |
||
| 579 | $this->pictures[] = $url; |
||
| 580 | } |
||
| 581 | |||
| 582 | return $this; |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Set pictures |
||
| 587 | * |
||
| 588 | * @param array $pictures |
||
| 589 | * |
||
| 590 | * @return $this |
||
| 591 | */ |
||
| 592 | public function setPictures(array $pictures) |
||
| 593 | { |
||
| 594 | $this->pictures = $pictures; |
||
| 595 | |||
| 596 | return $this; |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Get picture list |
||
| 601 | * |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | public function getPictures() |
||
| 605 | { |
||
| 606 | return $this->pictures; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Get list of barcodes of the offer |
||
| 611 | * |
||
| 612 | * @return string[] |
||
| 613 | */ |
||
| 614 | public function getBarcodes() |
||
| 615 | { |
||
| 616 | return $this->barcodes; |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Set list of barcodes for that offer |
||
| 621 | * |
||
| 622 | * @param string[] $barcodes |
||
| 623 | * |
||
| 624 | * @return $this |
||
| 625 | */ |
||
| 626 | public function setBarcodes(array $barcodes = []) |
||
| 627 | { |
||
| 628 | $this->barcodes = $barcodes; |
||
| 629 | |||
| 630 | return $this; |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Add one barcode to the collection of barcodes of this offer |
||
| 635 | * |
||
| 636 | * @param string $barcode |
||
| 637 | * |
||
| 638 | * @return AbstractOffer |
||
| 639 | */ |
||
| 640 | public function addBarcode($barcode) |
||
| 641 | { |
||
| 642 | $this->barcodes[] = $barcode; |
||
| 643 | |||
| 644 | return $this; |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Sets list of custom elements |
||
| 649 | * |
||
| 650 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
| 651 | * |
||
| 652 | * @return $this |
||
| 653 | */ |
||
| 654 | public function setCustomElements(array $customElements = []) |
||
| 655 | { |
||
| 656 | $this->customElements = $customElements; |
||
| 657 | |||
| 658 | return $this; |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Add a custom element with given type and value |
||
| 663 | * Multiple elements of the same type are supported |
||
| 664 | * |
||
| 665 | * @param string $elementType |
||
| 666 | * @param mixed $value |
||
| 667 | * |
||
| 668 | * @return $this |
||
| 669 | */ |
||
| 670 | public function addCustomElement($elementType, $value) |
||
| 671 | { |
||
| 672 | // Add value to the list of values of the given element type creating array when needed |
||
| 673 | $this->customElements[$elementType][] = $value; |
||
| 674 | |||
| 675 | return $this; |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Returns a list of custom elements |
||
| 680 | * Always returns an array even if no custom elements were added |
||
| 681 | * |
||
| 682 | * @return array |
||
| 683 | */ |
||
| 684 | public function getCustomElements() |
||
| 685 | { |
||
| 686 | return $this->customElements ?: []; |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Returns a list of values for the specified custom element type |
||
| 691 | * Always returns an array |
||
| 692 | * |
||
| 693 | * @param string $elementType |
||
| 694 | * |
||
| 695 | * @return array |
||
| 696 | */ |
||
| 697 | public function getCustomElementByType($elementType) |
||
| 698 | { |
||
| 699 | // TODO: Use ?? operator when support for PHP 5.6 is no longer needed |
||
| 700 | if (isset($this->customElements[$elementType])) { |
||
| 701 | return $this->customElements[$elementType]; |
||
| 702 | } |
||
| 703 | |||
| 704 | return []; |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | abstract protected function getOptions(); |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @return array |
||
| 714 | */ |
||
| 715 | private function getHeaderOptions() |
||
| 716 | { |
||
| 717 | return [ |
||
| 718 | 'url' => $this->getUrl(), |
||
| 719 | 'price' => $this->getPrice(), |
||
| 720 | 'oldprice' => $this->getOldPrice(), |
||
| 721 | 'currencyId' => $this->getCurrencyId(), |
||
| 722 | 'categoryId' => $this->getCategoryId(), |
||
| 723 | 'market_category' => $this->getMarketCategory(), |
||
| 724 | 'picture' => $this->getPictures(), |
||
| 725 | 'pickup' => $this->isPickup(), |
||
| 726 | 'store' => $this->isStore(), |
||
| 727 | 'delivery' => $this->isDelivery(), |
||
| 728 | 'weight' => $this->getWeight(), |
||
| 729 | 'local_delivery_cost' => $this->getLocalDeliveryCost(), |
||
| 730 | ] + $this->getCustomElements(); |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @return array |
||
| 735 | */ |
||
| 736 | private function getFooterOptions() |
||
| 737 | { |
||
| 738 | return [ |
||
| 739 | 'description' => $this->getDescription(), |
||
| 740 | 'sales_notes' => $this->getSalesNotes(), |
||
| 741 | 'manufacturer_warranty' => $this->isManufacturerWarranty(), |
||
| 742 | 'country_of_origin' => $this->getCountryOfOrigin(), |
||
| 743 | 'downloadable' => $this->isDownloadable(), |
||
| 744 | 'adult' => $this->isAdult(), |
||
| 745 | 'cpa' => $this->getCpa(), |
||
| 746 | 'barcode' => $this->getBarcodes(), |
||
| 747 | ]; |
||
| 748 | } |
||
| 749 | } |
||
| 750 |