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 | * |
||
148 | * @var array[] |
||
149 | */ |
||
150 | private $customElements; |
||
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 string $name |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function setName($name) |
||
339 | { |
||
340 | $this->name = $name; |
||
341 | |||
342 | return $this; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return string |
||
347 | */ |
||
348 | public function getMarketCategory() |
||
349 | { |
||
350 | return $this->marketCategory; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @param string $marketCategory |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function setMarketCategory($marketCategory) |
||
359 | { |
||
360 | $this->marketCategory = $marketCategory; |
||
361 | |||
362 | return $this; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function isAdult() |
||
369 | { |
||
370 | return $this->adult; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param bool $adult |
||
375 | * |
||
376 | * @return $this |
||
377 | */ |
||
378 | public function setAdult($adult) |
||
379 | { |
||
380 | $this->adult = $adult; |
||
381 | |||
382 | return $this; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return string |
||
387 | */ |
||
388 | public function getSalesNotes() |
||
389 | { |
||
390 | return $this->salesNotes; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @param string $salesNotes |
||
395 | * |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function setSalesNotes($salesNotes) |
||
399 | { |
||
400 | $this->salesNotes = $salesNotes; |
||
401 | |||
402 | return $this; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @return bool |
||
407 | */ |
||
408 | public function isManufacturerWarranty() |
||
409 | { |
||
410 | return $this->manufacturerWarranty; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * @param bool $manufacturerWarranty |
||
415 | * |
||
416 | * @return $this |
||
417 | */ |
||
418 | public function setManufacturerWarranty($manufacturerWarranty) |
||
419 | { |
||
420 | $this->manufacturerWarranty = $manufacturerWarranty; |
||
421 | |||
422 | return $this; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @return bool |
||
427 | */ |
||
428 | public function isPickup() |
||
429 | { |
||
430 | return $this->pickup; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @param bool $pickup |
||
435 | * |
||
436 | * @return $this |
||
437 | */ |
||
438 | public function setPickup($pickup) |
||
439 | { |
||
440 | $this->pickup = $pickup; |
||
441 | |||
442 | return $this; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function isDownloadable() |
||
449 | { |
||
450 | return $this->downloadable; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * @param bool $downloadable |
||
455 | * |
||
456 | * @return $this |
||
457 | */ |
||
458 | public function setDownloadable($downloadable) |
||
459 | { |
||
460 | $this->downloadable = $downloadable; |
||
461 | |||
462 | return $this; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * @return bool |
||
467 | */ |
||
468 | public function isDelivery() |
||
469 | { |
||
470 | return $this->delivery; |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * @param bool $delivery |
||
475 | * |
||
476 | * @return $this |
||
477 | */ |
||
478 | public function setDelivery($delivery) |
||
479 | { |
||
480 | $this->delivery = $delivery; |
||
481 | |||
482 | return $this; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @param bool $store |
||
487 | * |
||
488 | * @return $this |
||
489 | */ |
||
490 | public function setStore($store) |
||
491 | { |
||
492 | $this->store = $store; |
||
493 | |||
494 | return $this; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * @return bool |
||
499 | */ |
||
500 | public function isStore() |
||
501 | { |
||
502 | return $this->store; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @return float |
||
507 | */ |
||
508 | public function getLocalDeliveryCost() |
||
509 | { |
||
510 | return $this->localDeliveryCost; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @param float $localDeliveryCost |
||
515 | * |
||
516 | * @return $this |
||
517 | */ |
||
518 | public function setLocalDeliveryCost($localDeliveryCost) |
||
519 | { |
||
520 | $this->localDeliveryCost = $localDeliveryCost; |
||
521 | |||
522 | return $this; |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * @return string |
||
527 | */ |
||
528 | public function getDescription() |
||
529 | { |
||
530 | return $this->description; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @param string $description |
||
535 | * |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function setDescription($description) |
||
539 | { |
||
540 | $this->description = $description; |
||
541 | |||
542 | return $this; |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * @return string |
||
547 | */ |
||
548 | public function getCountryOfOrigin() |
||
549 | { |
||
550 | return $this->countryOfOrigin; |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * @param string $countryOfOrigin |
||
555 | * |
||
556 | * @return $this |
||
557 | */ |
||
558 | public function setCountryOfOrigin($countryOfOrigin) |
||
559 | { |
||
560 | $this->countryOfOrigin = $countryOfOrigin; |
||
561 | |||
562 | return $this; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * @return string |
||
567 | */ |
||
568 | public function getWeight() |
||
569 | { |
||
570 | return $this->weight; |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * @param string $weight |
||
575 | * |
||
576 | * @return $this |
||
577 | */ |
||
578 | public function setWeight($weight) |
||
579 | { |
||
580 | $this->weight = $weight; |
||
581 | |||
582 | return $this; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * @return int |
||
587 | */ |
||
588 | public function getCpa() |
||
589 | { |
||
590 | return $this->cpa; |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * @param int $cpa |
||
595 | * |
||
596 | * @return $this |
||
597 | */ |
||
598 | public function setCpa($cpa) |
||
599 | { |
||
600 | $this->cpa = $cpa; |
||
601 | |||
602 | return $this; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * @return array |
||
607 | */ |
||
608 | public function getParams() |
||
609 | { |
||
610 | return $this->params; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * @param OfferParam $param |
||
615 | * |
||
616 | * @return $this |
||
617 | */ |
||
618 | public function addParam(OfferParam $param) |
||
619 | { |
||
620 | $this->params[] = $param; |
||
621 | |||
622 | return $this; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Add picture |
||
627 | * |
||
628 | * @param string $url |
||
629 | * |
||
630 | * @return $this |
||
631 | */ |
||
632 | public function addPicture($url) |
||
633 | { |
||
634 | if (\count($this->pictures) < 10) { |
||
635 | $this->pictures[] = $url; |
||
636 | } |
||
637 | |||
638 | return $this; |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Set pictures |
||
643 | * |
||
644 | * @param array $pictures |
||
645 | * |
||
646 | * @return $this |
||
647 | */ |
||
648 | public function setPictures(array $pictures) |
||
649 | { |
||
650 | $this->pictures = $pictures; |
||
651 | |||
652 | return $this; |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * Get picture list |
||
657 | * |
||
658 | * @return array |
||
659 | */ |
||
660 | public function getPictures() |
||
661 | { |
||
662 | return $this->pictures; |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Get list of barcodes of the offer |
||
667 | * |
||
668 | * @return string[] |
||
669 | */ |
||
670 | public function getBarcodes() |
||
671 | { |
||
672 | return $this->barcodes; |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | * Set list of barcodes for that offer |
||
677 | * |
||
678 | * @param string[] $barcodes |
||
679 | * |
||
680 | * @return $this |
||
681 | */ |
||
682 | public function setBarcodes(array $barcodes = []) |
||
683 | { |
||
684 | $this->barcodes = $barcodes; |
||
685 | |||
686 | return $this; |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * Add one barcode to the collection of barcodes of this offer |
||
691 | * |
||
692 | * @param string $barcode |
||
693 | * |
||
694 | * @return AbstractOffer |
||
695 | */ |
||
696 | public function addBarcode($barcode) |
||
697 | { |
||
698 | $this->barcodes[] = $barcode; |
||
699 | |||
700 | return $this; |
||
701 | } |
||
702 | |||
703 | /** |
||
704 | * Sets list of custom elements |
||
705 | * |
||
706 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
707 | * |
||
708 | * @return $this |
||
709 | */ |
||
710 | public function setCustomElements(array $customElements = []) |
||
711 | { |
||
712 | $this->customElements = $customElements; |
||
713 | |||
714 | return $this; |
||
715 | } |
||
716 | |||
717 | /** |
||
718 | * Add a custom element with given type and value |
||
719 | * Multiple elements of the same type are supported |
||
720 | * |
||
721 | * @param string $elementType |
||
722 | * @param mixed $value |
||
723 | * |
||
724 | * @return $this |
||
725 | */ |
||
726 | public function addCustomElement($elementType, $value) |
||
727 | { |
||
728 | if ($value !== null) { |
||
729 | // Add value to the list of values of the given element type creating array when needed |
||
730 | $this->customElements[$elementType][] = $value; |
||
731 | } |
||
732 | |||
733 | return $this; |
||
734 | } |
||
735 | |||
736 | /** |
||
737 | * Returns a list of custom elements |
||
738 | * Always returns an array even if no custom elements were added |
||
739 | * |
||
740 | * @return array |
||
741 | */ |
||
742 | public function getCustomElements() |
||
743 | { |
||
744 | return $this->customElements ?: []; |
||
745 | } |
||
746 | |||
747 | /** |
||
748 | * Returns a list of values for the specified custom element type |
||
749 | * Always returns an array |
||
750 | * |
||
751 | * @param string $elementType |
||
752 | * |
||
753 | * @return array |
||
754 | */ |
||
755 | public function getCustomElementByType($elementType) |
||
756 | { |
||
757 | // TODO: Use ?? operator when support for PHP 5.6 is no longer needed |
||
758 | if (isset($this->customElements[$elementType])) { |
||
759 | return $this->customElements[$elementType]; |
||
760 | } |
||
761 | |||
762 | return []; |
||
763 | } |
||
764 | |||
765 | /** |
||
766 | * @return OfferCondition |
||
767 | */ |
||
768 | public function getCondition() |
||
772 | |||
773 | /** |
||
774 | * @param OfferCondition $condition |
||
775 | * |
||
776 | * @return $this |
||
777 | */ |
||
778 | public function addCondition(OfferCondition $condition) |
||
779 | { |
||
780 | $this->condition = $condition; |
||
781 | |||
782 | return $this; |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * @return array |
||
787 | */ |
||
788 | abstract protected function getOptions(); |
||
789 | |||
790 | /** |
||
791 | * @return array |
||
792 | */ |
||
793 | private function getHeaderOptions() |
||
794 | { |
||
795 | return [ |
||
796 | 'url' => $this->getUrl(), |
||
797 | 'price' => $this->getPrice(), |
||
798 | 'oldprice' => $this->getOldPrice(), |
||
799 | 'currencyId' => $this->getCurrencyId(), |
||
800 | 'categoryId' => \array_merge( |
||
801 | [$this->getCategoryId()], |
||
802 | $this->getCategoriesId() |
||
803 | ), |
||
804 | 'market_category' => $this->getMarketCategory(), |
||
805 | 'picture' => $this->getPictures(), |
||
806 | 'pickup' => $this->isPickup(), |
||
807 | 'store' => $this->isStore(), |
||
808 | 'delivery' => $this->isDelivery(), |
||
809 | 'local_delivery_cost' => $this->getLocalDeliveryCost(), |
||
810 | 'weight' => $this->getWeight(), |
||
811 | 'name' => $this->getName(), |
||
812 | ] + $this->getCustomElements(); |
||
813 | } |
||
814 | |||
815 | /** |
||
816 | * @return array |
||
817 | */ |
||
818 | private function getFooterOptions() |
||
831 | } |
||
832 |