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 int |
||
133 | */ |
||
134 | private $cpa; |
||
135 | |||
136 | /** |
||
137 | * @var string[] |
||
138 | */ |
||
139 | private $barcodes; |
||
140 | |||
141 | /** |
||
142 | * @var array |
||
143 | */ |
||
144 | private $pictures = []; |
||
145 | |||
146 | /** |
||
147 | * @var array |
||
148 | */ |
||
149 | private $params = []; |
||
150 | |||
151 | /** |
||
152 | * @var bool |
||
153 | */ |
||
154 | private $store; |
||
155 | |||
156 | /** |
||
157 | * @var bool |
||
158 | */ |
||
159 | private $autoDiscount; |
||
160 | |||
161 | /** |
||
162 | * Array of custom elements (element types are keys) of arrays of element values |
||
163 | * There may be multiple elements of the same type |
||
164 | * |
||
165 | * @var array[] |
||
166 | */ |
||
167 | private $customElements; |
||
168 | |||
169 | /** |
||
170 | * @var OfferCondition |
||
171 | */ |
||
172 | private $condition; |
||
173 | |||
174 | /** |
||
175 | * @return array |
||
176 | */ |
||
177 | public function toArray() |
||
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getId() |
||
189 | |||
190 | /** |
||
191 | * @param string $id |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setId($id) |
||
201 | |||
202 | /** |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function isAvailable() |
||
209 | |||
210 | /** |
||
211 | * @param bool $available |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setAvailable($available) |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getUrl() |
||
229 | |||
230 | /** |
||
231 | * @param string $url |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setUrl($url) |
||
241 | |||
242 | /** |
||
243 | * @return float |
||
244 | */ |
||
245 | public function getPrice() |
||
249 | |||
250 | /** |
||
251 | * @param float $price |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setPrice($price) |
||
261 | |||
262 | /** |
||
263 | * @return float |
||
264 | */ |
||
265 | public function getOldPrice() |
||
269 | |||
270 | /** |
||
271 | * @param float $oldPrice |
||
272 | * |
||
273 | * @return $this |
||
274 | */ |
||
275 | public function setOldPrice($oldPrice) |
||
281 | |||
282 | /** |
||
283 | * @return float |
||
284 | */ |
||
285 | public function getPurchasePrice() |
||
289 | |||
290 | /** |
||
291 | * @param float $purchasePrice |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setPurchasePrice($purchasePrice) |
||
301 | |||
302 | /** |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getCurrencyId() |
||
309 | |||
310 | /** |
||
311 | * @param string $currencyId |
||
312 | * |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function setCurrencyId($currencyId) |
||
321 | |||
322 | /** |
||
323 | * @return int |
||
324 | */ |
||
325 | public function getCategoryId() |
||
329 | |||
330 | /** |
||
331 | * @param int $categoryId |
||
332 | * |
||
333 | * @return $this |
||
334 | */ |
||
335 | public function setCategoryId($categoryId) |
||
341 | |||
342 | /** |
||
343 | * @return array |
||
344 | */ |
||
345 | public function getCategoriesId() |
||
349 | |||
350 | /** |
||
351 | * @param array $categoriesId |
||
352 | * |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function setCategoriesId(array $categoriesId) |
||
361 | |||
362 | /** |
||
363 | * @return string |
||
364 | */ |
||
365 | public function getName() |
||
369 | |||
370 | /** |
||
371 | * @param string $name |
||
372 | * |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function setName($name) |
||
381 | |||
382 | /** |
||
383 | * @return string |
||
384 | */ |
||
385 | public function getMarketCategory() |
||
389 | |||
390 | /** |
||
391 | * @param string $marketCategory |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setMarketCategory($marketCategory) |
||
401 | |||
402 | /** |
||
403 | * @return bool |
||
404 | */ |
||
405 | public function isAdult() |
||
409 | |||
410 | /** |
||
411 | * @param bool $adult |
||
412 | * |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function setAdult($adult) |
||
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | public function getSalesNotes() |
||
429 | |||
430 | /** |
||
431 | * @param string $salesNotes |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function setSalesNotes($salesNotes) |
||
441 | |||
442 | /** |
||
443 | * @return bool |
||
444 | */ |
||
445 | public function isManufacturerWarranty() |
||
449 | |||
450 | /** |
||
451 | * @param bool $manufacturerWarranty |
||
452 | * |
||
453 | * @return $this |
||
454 | */ |
||
455 | public function setManufacturerWarranty($manufacturerWarranty) |
||
461 | |||
462 | /** |
||
463 | * @return bool |
||
464 | */ |
||
465 | public function isPickup() |
||
469 | |||
470 | /** |
||
471 | * @param bool $pickup |
||
472 | * |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function setPickup($pickup) |
||
481 | |||
482 | /** |
||
483 | * @return bool |
||
484 | */ |
||
485 | public function isDownloadable() |
||
489 | |||
490 | /** |
||
491 | * @param bool $downloadable |
||
492 | * |
||
493 | * @return $this |
||
494 | */ |
||
495 | public function setDownloadable($downloadable) |
||
501 | |||
502 | /** |
||
503 | * @return bool |
||
504 | */ |
||
505 | public function isDelivery() |
||
509 | |||
510 | /** |
||
511 | * @param bool $delivery |
||
512 | * |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function setDelivery($delivery) |
||
521 | |||
522 | /** |
||
523 | * @return array |
||
524 | */ |
||
525 | public function getDeliveryOptions() |
||
529 | |||
530 | /** |
||
531 | * @param Delivery $option |
||
532 | * |
||
533 | * @return $this |
||
534 | */ |
||
535 | public function addDeliveryOption(Delivery $option) |
||
541 | |||
542 | /** |
||
543 | * @param bool $store |
||
544 | * |
||
545 | * @return $this |
||
546 | */ |
||
547 | public function setStore($store) |
||
553 | |||
554 | /** |
||
555 | * @return bool |
||
556 | */ |
||
557 | public function isStore() |
||
561 | |||
562 | /** |
||
563 | * @return float |
||
564 | */ |
||
565 | public function getLocalDeliveryCost() |
||
569 | |||
570 | /** |
||
571 | * @param float $localDeliveryCost |
||
572 | * |
||
573 | * @return $this |
||
574 | */ |
||
575 | public function setLocalDeliveryCost($localDeliveryCost) |
||
581 | |||
582 | /** |
||
583 | * @return string |
||
584 | */ |
||
585 | public function getDescription() |
||
589 | |||
590 | /** |
||
591 | * @param string $description |
||
592 | * |
||
593 | * @return $this |
||
594 | */ |
||
595 | public function setDescription($description) |
||
601 | |||
602 | /** |
||
603 | * @return string |
||
604 | */ |
||
605 | public function getCountryOfOrigin() |
||
609 | |||
610 | /** |
||
611 | * @param string $countryOfOrigin |
||
612 | * |
||
613 | * @return $this |
||
614 | */ |
||
615 | public function setCountryOfOrigin($countryOfOrigin) |
||
621 | |||
622 | /** |
||
623 | * @return string |
||
624 | */ |
||
625 | public function getWeight() |
||
629 | |||
630 | /** |
||
631 | * @param string $weight |
||
632 | * |
||
633 | * @return $this |
||
634 | */ |
||
635 | public function setWeight($weight) |
||
641 | |||
642 | /** |
||
643 | * @return int |
||
644 | */ |
||
645 | public function getCpa() |
||
649 | |||
650 | /** |
||
651 | * @param int $cpa |
||
652 | * |
||
653 | * @return $this |
||
654 | */ |
||
655 | public function setCpa($cpa) |
||
661 | |||
662 | /** |
||
663 | * @return array |
||
664 | */ |
||
665 | public function getParams() |
||
669 | |||
670 | /** |
||
671 | * @param OfferParam $param |
||
672 | * |
||
673 | * @return $this |
||
674 | */ |
||
675 | public function addParam(OfferParam $param) |
||
681 | |||
682 | /** |
||
683 | * Add picture |
||
684 | * |
||
685 | * @param string $url |
||
686 | * |
||
687 | * @return $this |
||
688 | */ |
||
689 | public function addPicture($url) |
||
697 | |||
698 | /** |
||
699 | * Set pictures |
||
700 | * |
||
701 | * @param array $pictures |
||
702 | * |
||
703 | * @return $this |
||
704 | */ |
||
705 | public function setPictures(array $pictures) |
||
711 | |||
712 | /** |
||
713 | * Get picture list |
||
714 | * |
||
715 | * @return array |
||
716 | */ |
||
717 | public function getPictures() |
||
721 | |||
722 | /** |
||
723 | * Get list of barcodes of the offer |
||
724 | * |
||
725 | * @return string[] |
||
726 | */ |
||
727 | public function getBarcodes() |
||
731 | |||
732 | /** |
||
733 | * Set list of barcodes for that offer |
||
734 | * |
||
735 | * @param string[] $barcodes |
||
736 | * |
||
737 | * @return $this |
||
738 | */ |
||
739 | public function setBarcodes(array $barcodes = []) |
||
745 | |||
746 | /** |
||
747 | * Add one barcode to the collection of barcodes of this offer |
||
748 | * |
||
749 | * @param string $barcode |
||
750 | * |
||
751 | * @return $this |
||
752 | */ |
||
753 | public function addBarcode($barcode) |
||
759 | |||
760 | /** |
||
761 | * Sets list of custom elements |
||
762 | * |
||
763 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
764 | * |
||
765 | * @return $this |
||
766 | */ |
||
767 | public function setCustomElements(array $customElements = []) |
||
773 | |||
774 | /** |
||
775 | * Add a custom element with given type and value |
||
776 | * Multiple elements of the same type are supported |
||
777 | * |
||
778 | * @param string $elementType |
||
779 | * @param mixed $value |
||
780 | * |
||
781 | * @return $this |
||
782 | */ |
||
783 | public function addCustomElement($elementType, $value) |
||
792 | |||
793 | /** |
||
794 | * Returns a list of custom elements |
||
795 | * Always returns an array even if no custom elements were added |
||
796 | * |
||
797 | * @return array |
||
798 | */ |
||
799 | public function getCustomElements() |
||
803 | |||
804 | /** |
||
805 | * Returns a list of values for the specified custom element type |
||
806 | * Always returns an array |
||
807 | * |
||
808 | * @param string $elementType |
||
809 | * |
||
810 | * @return array |
||
811 | */ |
||
812 | public function getCustomElementByType($elementType) |
||
821 | |||
822 | /** |
||
823 | * @return OfferCondition |
||
824 | */ |
||
825 | public function getCondition() |
||
829 | |||
830 | /** |
||
831 | * @param OfferCondition $condition |
||
832 | * |
||
833 | * @return $this |
||
834 | */ |
||
835 | public function addCondition(OfferCondition $condition) |
||
841 | |||
842 | /** |
||
843 | * @return bool |
||
844 | */ |
||
845 | public function getAutoDiscount() |
||
849 | |||
850 | /** |
||
851 | * @param bool $autoDiscount |
||
852 | * |
||
853 | * @return $this |
||
854 | */ |
||
855 | public function setAutoDiscount($autoDiscount) |
||
861 | |||
862 | /** |
||
863 | * @return array |
||
864 | */ |
||
865 | abstract protected function getOptions(); |
||
866 | |||
867 | /** |
||
868 | * @return array |
||
869 | */ |
||
870 | private function getHeaderOptions() |
||
893 | |||
894 | /** |
||
895 | * @return array |
||
896 | */ |
||
897 | private function getFooterOptions() |
||
910 | } |
||
911 |