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() |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getId() |
||
156 | |||
157 | /** |
||
158 | * @param string $id |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function setId($id) |
||
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isAvailable() |
||
176 | |||
177 | /** |
||
178 | * @param bool $available |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setAvailable($available) |
||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getUrl() |
||
196 | |||
197 | /** |
||
198 | * @param string $url |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setUrl($url) |
||
208 | |||
209 | /** |
||
210 | * @return float |
||
211 | */ |
||
212 | public function getPrice() |
||
216 | |||
217 | /** |
||
218 | * @param float $price |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setPrice($price) |
||
228 | |||
229 | /** |
||
230 | * @return float |
||
231 | */ |
||
232 | public function getOldPrice() |
||
236 | |||
237 | /** |
||
238 | * @param float $oldPrice |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function setOldPrice($oldPrice) |
||
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getCurrencyId() |
||
256 | |||
257 | /** |
||
258 | * @param string $currencyId |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function setCurrencyId($currencyId) |
||
268 | |||
269 | /** |
||
270 | * @return int |
||
271 | */ |
||
272 | public function getCategoryId() |
||
276 | |||
277 | /** |
||
278 | * @param int $categoryId |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function setCategoryId($categoryId) |
||
288 | |||
289 | /** |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getMarketCategory() |
||
296 | |||
297 | /** |
||
298 | * @param string $marketCategory |
||
299 | * |
||
300 | * @return $this |
||
301 | */ |
||
302 | public function setMarketCategory($marketCategory) |
||
308 | |||
309 | /** |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function isAdult() |
||
316 | |||
317 | /** |
||
318 | * @param bool $adult |
||
319 | * |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function setAdult($adult) |
||
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getSalesNotes() |
||
336 | |||
337 | /** |
||
338 | * @param string $salesNotes |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function setSalesNotes($salesNotes) |
||
348 | |||
349 | /** |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function isManufacturerWarranty() |
||
356 | |||
357 | /** |
||
358 | * @param bool $manufacturerWarranty |
||
359 | * |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function setManufacturerWarranty($manufacturerWarranty) |
||
368 | |||
369 | /** |
||
370 | * @return bool |
||
371 | */ |
||
372 | public function isPickup() |
||
376 | |||
377 | /** |
||
378 | * @param bool $pickup |
||
379 | * |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function setPickup($pickup) |
||
388 | |||
389 | /** |
||
390 | * @return bool |
||
391 | */ |
||
392 | public function isDownloadable() |
||
396 | |||
397 | /** |
||
398 | * @param bool $downloadable |
||
399 | * |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function setDownloadable($downloadable) |
||
408 | |||
409 | /** |
||
410 | * @return bool |
||
411 | */ |
||
412 | public function isDelivery() |
||
416 | |||
417 | /** |
||
418 | * @param bool $delivery |
||
419 | * |
||
420 | * @return $this |
||
421 | */ |
||
422 | public function setDelivery($delivery) |
||
428 | |||
429 | /** |
||
430 | * @param bool $store |
||
431 | * |
||
432 | * @return $this |
||
433 | */ |
||
434 | public function setStore($store) |
||
440 | |||
441 | /** |
||
442 | * @return bool |
||
443 | */ |
||
444 | public function isStore() |
||
448 | |||
449 | /** |
||
450 | * @return float |
||
451 | */ |
||
452 | public function getLocalDeliveryCost() |
||
456 | |||
457 | /** |
||
458 | * @param float $localDeliveryCost |
||
459 | * |
||
460 | * @return $this |
||
461 | */ |
||
462 | public function setLocalDeliveryCost($localDeliveryCost) |
||
468 | |||
469 | /** |
||
470 | * @return string |
||
471 | */ |
||
472 | public function getDescription() |
||
476 | |||
477 | /** |
||
478 | * @param string $description |
||
479 | * |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function setDescription($description) |
||
488 | |||
489 | /** |
||
490 | * @return string |
||
491 | */ |
||
492 | public function getCountryOfOrigin() |
||
496 | |||
497 | /** |
||
498 | * @param string $countryOfOrigin |
||
499 | * |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function setCountryOfOrigin($countryOfOrigin) |
||
508 | |||
509 | /** |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getWeight() |
||
516 | |||
517 | /** |
||
518 | * @param string $weight |
||
519 | * |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function setWeight($weight) |
||
528 | |||
529 | /** |
||
530 | * @return int |
||
531 | */ |
||
532 | public function getCpa() |
||
536 | |||
537 | /** |
||
538 | * @param int $cpa |
||
539 | * |
||
540 | * @return $this |
||
541 | */ |
||
542 | public function setCpa($cpa) |
||
548 | |||
549 | /** |
||
550 | * @return array |
||
551 | */ |
||
552 | public function getParams() |
||
556 | |||
557 | /** |
||
558 | * @param OfferParam $param |
||
559 | * |
||
560 | * @return $this |
||
561 | */ |
||
562 | public function addParam(OfferParam $param) |
||
568 | |||
569 | /** |
||
570 | * Add picture |
||
571 | * |
||
572 | * @param string $url |
||
573 | * |
||
574 | * @return $this |
||
575 | */ |
||
576 | public function addPicture($url) |
||
584 | |||
585 | /** |
||
586 | * Set pictures |
||
587 | * |
||
588 | * @param array $pictures |
||
589 | * |
||
590 | * @return $this |
||
591 | */ |
||
592 | public function setPictures(array $pictures) |
||
598 | |||
599 | /** |
||
600 | * Get picture list |
||
601 | * |
||
602 | * @return array |
||
603 | */ |
||
604 | public function getPictures() |
||
608 | |||
609 | /** |
||
610 | * Get list of barcodes of the offer |
||
611 | * |
||
612 | * @return string[] |
||
613 | */ |
||
614 | public function getBarcodes() |
||
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 = []) |
||
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) |
||
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 = []) |
||
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) |
||
679 | |||
680 | /** |
||
681 | * Returns a list of custom elements |
||
682 | * Always returns an array even if no custom elements were added |
||
683 | * |
||
684 | * @return array |
||
685 | */ |
||
686 | public function getCustomElements() |
||
690 | |||
691 | /** |
||
692 | * Returns a list of values for the specified custom element type |
||
693 | * Always returns an array |
||
694 | * |
||
695 | * @param string $elementType |
||
696 | * |
||
697 | * @return array |
||
698 | */ |
||
699 | public function getCustomElementByType($elementType) |
||
708 | |||
709 | /** |
||
710 | * @return array |
||
711 | */ |
||
712 | abstract protected function getOptions(); |
||
713 | |||
714 | /** |
||
715 | * @return array |
||
716 | */ |
||
717 | private function getHeaderOptions() |
||
734 | |||
735 | /** |
||
736 | * @return array |
||
737 | */ |
||
738 | private function getFooterOptions() |
||
751 | } |
||
752 |