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 string |
||
48 | */ |
||
49 | private $currencyId; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $categoryId; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $categoriesId = []; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $name; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $marketCategory; |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | private $adult; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | private $salesNotes; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | private $manufacturerWarranty; |
||
85 | |||
86 | /** |
||
87 | * @var bool |
||
88 | */ |
||
89 | private $pickup; |
||
90 | |||
91 | /** |
||
92 | * @var bool |
||
93 | */ |
||
94 | private $downloadable; |
||
95 | |||
96 | /** |
||
97 | * @var bool |
||
98 | */ |
||
99 | private $delivery; |
||
100 | |||
101 | /** |
||
102 | * @var float |
||
103 | */ |
||
104 | private $localDeliveryCost; |
||
105 | |||
106 | /** |
||
107 | * @var array |
||
108 | */ |
||
109 | private $deliveryOptions = []; |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | */ |
||
114 | private $description; |
||
115 | |||
116 | /** |
||
117 | * @var string |
||
118 | */ |
||
119 | private $countryOfOrigin; |
||
120 | |||
121 | /** |
||
122 | * @var string |
||
123 | */ |
||
124 | private $weight; |
||
125 | |||
126 | /** |
||
127 | * @var int |
||
128 | */ |
||
129 | private $cpa; |
||
130 | |||
131 | /** |
||
132 | * @var string[] |
||
133 | */ |
||
134 | private $barcodes; |
||
135 | |||
136 | /** |
||
137 | * @var array |
||
138 | */ |
||
139 | private $pictures = []; |
||
140 | |||
141 | /** |
||
142 | * @var array |
||
143 | */ |
||
144 | private $params = []; |
||
145 | |||
146 | /** |
||
147 | * @var bool |
||
148 | */ |
||
149 | private $store; |
||
150 | |||
151 | /** |
||
152 | * Array of custom elements (element types are keys) of arrays of element values |
||
153 | * There may be multiple elements of the same type |
||
154 | * |
||
155 | * @var array[] |
||
156 | */ |
||
157 | private $customElements; |
||
158 | |||
159 | /** |
||
160 | * @var OfferCondition |
||
161 | */ |
||
162 | private $condition; |
||
163 | |||
164 | /** |
||
165 | * @return array |
||
166 | */ |
||
167 | public function toArray() |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getId() |
||
179 | |||
180 | /** |
||
181 | * @param string $id |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function setId($id) |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isAvailable() |
||
199 | |||
200 | /** |
||
201 | * @param bool $available |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function setAvailable($available) |
||
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getUrl() |
||
219 | |||
220 | /** |
||
221 | * @param string $url |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function setUrl($url) |
||
231 | |||
232 | /** |
||
233 | * @return float |
||
234 | */ |
||
235 | public function getPrice() |
||
239 | |||
240 | /** |
||
241 | * @param float $price |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setPrice($price) |
||
251 | |||
252 | /** |
||
253 | * @return float |
||
254 | */ |
||
255 | public function getOldPrice() |
||
259 | |||
260 | /** |
||
261 | * @param float $oldPrice |
||
262 | * |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function setOldPrice($oldPrice) |
||
271 | |||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getCurrencyId() |
||
279 | |||
280 | /** |
||
281 | * @param string $currencyId |
||
282 | * |
||
283 | * @return $this |
||
284 | */ |
||
285 | public function setCurrencyId($currencyId) |
||
291 | |||
292 | /** |
||
293 | * @return int |
||
294 | */ |
||
295 | public function getCategoryId() |
||
299 | |||
300 | /** |
||
301 | * @param int $categoryId |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function setCategoryId($categoryId) |
||
311 | |||
312 | /** |
||
313 | * @return array |
||
314 | */ |
||
315 | public function getCategoriesId() |
||
319 | |||
320 | /** |
||
321 | * @param array $categoriesId |
||
322 | * |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setCategoriesId(array $categoriesId) |
||
331 | |||
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getName() |
||
339 | |||
340 | /** |
||
341 | * @param string $name |
||
342 | * |
||
343 | * @return $this |
||
344 | */ |
||
345 | public function setName($name) |
||
351 | |||
352 | /** |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getMarketCategory() |
||
359 | |||
360 | /** |
||
361 | * @param string $marketCategory |
||
362 | * |
||
363 | * @return $this |
||
364 | */ |
||
365 | public function setMarketCategory($marketCategory) |
||
371 | |||
372 | /** |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function isAdult() |
||
379 | |||
380 | /** |
||
381 | * @param bool $adult |
||
382 | * |
||
383 | * @return $this |
||
384 | */ |
||
385 | public function setAdult($adult) |
||
391 | |||
392 | /** |
||
393 | * @return string |
||
394 | */ |
||
395 | public function getSalesNotes() |
||
399 | |||
400 | /** |
||
401 | * @param string $salesNotes |
||
402 | * |
||
403 | * @return $this |
||
404 | */ |
||
405 | public function setSalesNotes($salesNotes) |
||
411 | |||
412 | /** |
||
413 | * @return bool |
||
414 | */ |
||
415 | public function isManufacturerWarranty() |
||
419 | |||
420 | /** |
||
421 | * @param bool $manufacturerWarranty |
||
422 | * |
||
423 | * @return $this |
||
424 | */ |
||
425 | public function setManufacturerWarranty($manufacturerWarranty) |
||
431 | |||
432 | /** |
||
433 | * @return bool |
||
434 | */ |
||
435 | public function isPickup() |
||
439 | |||
440 | /** |
||
441 | * @param bool $pickup |
||
442 | * |
||
443 | * @return $this |
||
444 | */ |
||
445 | public function setPickup($pickup) |
||
451 | |||
452 | /** |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function isDownloadable() |
||
459 | |||
460 | /** |
||
461 | * @param bool $downloadable |
||
462 | * |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function setDownloadable($downloadable) |
||
471 | |||
472 | /** |
||
473 | * @return bool |
||
474 | */ |
||
475 | public function isDelivery() |
||
479 | |||
480 | /** |
||
481 | * @param bool $delivery |
||
482 | * |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function setDelivery($delivery) |
||
491 | |||
492 | /** |
||
493 | * @return array |
||
494 | */ |
||
495 | public function getDeliveryOptions() |
||
499 | |||
500 | /** |
||
501 | * @param Delivery $option |
||
502 | * |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function addDeliveryOption(Delivery $option) |
||
511 | |||
512 | /** |
||
513 | * @param bool $store |
||
514 | * |
||
515 | * @return $this |
||
516 | */ |
||
517 | public function setStore($store) |
||
523 | |||
524 | /** |
||
525 | * @return bool |
||
526 | */ |
||
527 | public function isStore() |
||
531 | |||
532 | /** |
||
533 | * @return float |
||
534 | */ |
||
535 | public function getLocalDeliveryCost() |
||
539 | |||
540 | /** |
||
541 | * @param float $localDeliveryCost |
||
542 | * |
||
543 | * @return $this |
||
544 | */ |
||
545 | public function setLocalDeliveryCost($localDeliveryCost) |
||
551 | |||
552 | /** |
||
553 | * @return string |
||
554 | */ |
||
555 | public function getDescription() |
||
559 | |||
560 | /** |
||
561 | * @param string $description |
||
562 | * |
||
563 | * @return $this |
||
564 | */ |
||
565 | public function setDescription($description) |
||
571 | |||
572 | /** |
||
573 | * @return string |
||
574 | */ |
||
575 | public function getCountryOfOrigin() |
||
579 | |||
580 | /** |
||
581 | * @param string $countryOfOrigin |
||
582 | * |
||
583 | * @return $this |
||
584 | */ |
||
585 | public function setCountryOfOrigin($countryOfOrigin) |
||
591 | |||
592 | /** |
||
593 | * @return string |
||
594 | */ |
||
595 | public function getWeight() |
||
599 | |||
600 | /** |
||
601 | * @param string $weight |
||
602 | * |
||
603 | * @return $this |
||
604 | */ |
||
605 | public function setWeight($weight) |
||
611 | |||
612 | /** |
||
613 | * @return int |
||
614 | */ |
||
615 | public function getCpa() |
||
619 | |||
620 | /** |
||
621 | * @param int $cpa |
||
622 | * |
||
623 | * @return $this |
||
624 | */ |
||
625 | public function setCpa($cpa) |
||
631 | |||
632 | /** |
||
633 | * @return array |
||
634 | */ |
||
635 | public function getParams() |
||
639 | |||
640 | /** |
||
641 | * @param OfferParam $param |
||
642 | * |
||
643 | * @return $this |
||
644 | */ |
||
645 | public function addParam(OfferParam $param) |
||
651 | |||
652 | /** |
||
653 | * Add picture |
||
654 | * |
||
655 | * @param string $url |
||
656 | * |
||
657 | * @return $this |
||
658 | */ |
||
659 | public function addPicture($url) |
||
667 | |||
668 | /** |
||
669 | * Set pictures |
||
670 | * |
||
671 | * @param array $pictures |
||
672 | * |
||
673 | * @return $this |
||
674 | */ |
||
675 | public function setPictures(array $pictures) |
||
681 | |||
682 | /** |
||
683 | * Get picture list |
||
684 | * |
||
685 | * @return array |
||
686 | */ |
||
687 | public function getPictures() |
||
691 | |||
692 | /** |
||
693 | * Get list of barcodes of the offer |
||
694 | * |
||
695 | * @return string[] |
||
696 | */ |
||
697 | public function getBarcodes() |
||
701 | |||
702 | /** |
||
703 | * Set list of barcodes for that offer |
||
704 | * |
||
705 | * @param string[] $barcodes |
||
706 | * |
||
707 | * @return $this |
||
708 | */ |
||
709 | public function setBarcodes(array $barcodes = []) |
||
715 | |||
716 | /** |
||
717 | * Add one barcode to the collection of barcodes of this offer |
||
718 | * |
||
719 | * @param string $barcode |
||
720 | * |
||
721 | * @return AbstractOffer |
||
722 | */ |
||
723 | public function addBarcode($barcode) |
||
729 | |||
730 | /** |
||
731 | * Sets list of custom elements |
||
732 | * |
||
733 | * @param array $customElements Array (keys are element types) of arrays (element values) |
||
734 | * |
||
735 | * @return $this |
||
736 | */ |
||
737 | public function setCustomElements(array $customElements = []) |
||
743 | |||
744 | /** |
||
745 | * Add a custom element with given type and value |
||
746 | * Multiple elements of the same type are supported |
||
747 | * |
||
748 | * @param string $elementType |
||
749 | * @param mixed $value |
||
750 | * |
||
751 | * @return $this |
||
752 | */ |
||
753 | public function addCustomElement($elementType, $value) |
||
762 | |||
763 | /** |
||
764 | * Returns a list of custom elements |
||
765 | * Always returns an array even if no custom elements were added |
||
766 | * |
||
767 | * @return array |
||
768 | */ |
||
769 | public function getCustomElements() |
||
773 | |||
774 | /** |
||
775 | * Returns a list of values for the specified custom element type |
||
776 | * Always returns an array |
||
777 | * |
||
778 | * @param string $elementType |
||
779 | * |
||
780 | * @return array |
||
781 | */ |
||
782 | public function getCustomElementByType($elementType) |
||
791 | |||
792 | /** |
||
793 | * @return OfferCondition |
||
794 | */ |
||
795 | public function getCondition() |
||
799 | |||
800 | /** |
||
801 | * @param OfferCondition $condition |
||
802 | * |
||
803 | * @return $this |
||
804 | */ |
||
805 | public function addCondition(OfferCondition $condition) |
||
811 | |||
812 | /** |
||
813 | * @return array |
||
814 | */ |
||
815 | abstract protected function getOptions(); |
||
816 | |||
817 | /** |
||
818 | * @return array |
||
819 | */ |
||
820 | private function getHeaderOptions() |
||
841 | |||
842 | /** |
||
843 | * @return array |
||
844 | */ |
||
845 | private function getFooterOptions() |
||
858 | } |
||
859 |