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 array |
||
106 | */ |
||
107 | private $pictures = []; |
||
108 | |||
109 | /** |
||
110 | * @var array |
||
111 | */ |
||
112 | private $params = []; |
||
113 | |||
114 | /** |
||
115 | * @return array |
||
116 | */ |
||
117 | public function toArray() |
||
118 | { |
||
119 | return \array_merge($this->getHeaderOptions(), $this->getOptions(), $this->getFooterOptions()); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getId() |
||
126 | { |
||
127 | return $this->id; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $id |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setId($id) |
||
136 | { |
||
137 | $this->id = $id; |
||
138 | |||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function isAvailable() |
||
146 | { |
||
147 | return $this->available; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param bool $available |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setAvailable($available) |
||
156 | { |
||
157 | $this->available = $available; |
||
158 | |||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getUrl() |
||
166 | { |
||
167 | return $this->url; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $url |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setUrl($url) |
||
176 | { |
||
177 | $this->url = $url; |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @return float |
||
184 | */ |
||
185 | public function getPrice() |
||
186 | { |
||
187 | return $this->price; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param float $price |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setPrice($price) |
||
201 | |||
202 | /** |
||
203 | * @return float |
||
204 | */ |
||
205 | public function getOldPrice() |
||
206 | { |
||
207 | return $this->oldPrice; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param float $oldPrice |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setOldPrice($oldPrice) |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getCurrencyId() |
||
229 | |||
230 | /** |
||
231 | * @param string $currencyId |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setCurrencyId($currencyId) |
||
241 | |||
242 | /** |
||
243 | * @return int |
||
244 | */ |
||
245 | public function getCategoryId() |
||
249 | |||
250 | /** |
||
251 | * @param int $categoryId |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setCategoryId($categoryId) |
||
261 | |||
262 | /** |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getMarketCategory() |
||
269 | |||
270 | /** |
||
271 | * @param string $marketCategory |
||
272 | * |
||
273 | * @return $this |
||
274 | */ |
||
275 | public function setMarketCategory($marketCategory) |
||
281 | |||
282 | /** |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function isAdult() |
||
289 | |||
290 | /** |
||
291 | * @param bool $adult |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setAdult($adult) |
||
301 | |||
302 | /** |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getSalesNotes() |
||
309 | |||
310 | /** |
||
311 | * @param string $salesNotes |
||
312 | * |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function setSalesNotes($salesNotes) |
||
321 | |||
322 | /** |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function isManufacturerWarranty() |
||
329 | |||
330 | /** |
||
331 | * @param bool $manufacturerWarranty |
||
332 | * |
||
333 | * @return $this |
||
334 | */ |
||
335 | public function setManufacturerWarranty($manufacturerWarranty) |
||
341 | |||
342 | /** |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function isPickup() |
||
349 | |||
350 | /** |
||
351 | * @param bool $pickup |
||
352 | * |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function setPickup($pickup) |
||
361 | |||
362 | /** |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function isDownloadable() |
||
369 | |||
370 | /** |
||
371 | * @param bool $downloadable |
||
372 | * |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function setDownloadable($downloadable) |
||
381 | |||
382 | /** |
||
383 | * @return bool |
||
384 | */ |
||
385 | public function isDelivery() |
||
389 | |||
390 | /** |
||
391 | * @param bool $delivery |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setDelivery($delivery) |
||
401 | |||
402 | /** |
||
403 | * @return float |
||
404 | */ |
||
405 | public function getLocalDeliveryCost() |
||
409 | |||
410 | /** |
||
411 | * @param float $localDeliveryCost |
||
412 | * |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function setLocalDeliveryCost($localDeliveryCost) |
||
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | public function getDescription() |
||
429 | |||
430 | /** |
||
431 | * @param string $description |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function setDescription($description) |
||
441 | |||
442 | /** |
||
443 | * @return string |
||
444 | */ |
||
445 | public function getCountryOfOrigin() |
||
449 | |||
450 | /** |
||
451 | * @param string $countryOfOrigin |
||
452 | * |
||
453 | * @return $this |
||
454 | */ |
||
455 | public function setCountryOfOrigin($countryOfOrigin) |
||
461 | |||
462 | /** |
||
463 | * @return array |
||
464 | */ |
||
465 | public function getParams() |
||
469 | |||
470 | /** |
||
471 | * @param OfferParam $param |
||
472 | * |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function addParam(OfferParam $param) |
||
481 | |||
482 | /** |
||
483 | * Add picture |
||
484 | * |
||
485 | * @param string $url |
||
486 | * |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function addPicture($url) |
||
497 | |||
498 | /** |
||
499 | * Set pictures |
||
500 | * |
||
501 | * @param array $pictures |
||
502 | * |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setPictures(array $pictures) |
||
511 | |||
512 | /** |
||
513 | * Get picture list |
||
514 | * |
||
515 | * @return array |
||
516 | */ |
||
517 | public function getPictures() |
||
521 | |||
522 | /** |
||
523 | * @return array |
||
524 | */ |
||
525 | abstract protected function getOptions(); |
||
526 | |||
527 | /** |
||
528 | * @return array |
||
529 | */ |
||
530 | private function getHeaderOptions() |
||
545 | |||
546 | /** |
||
547 | * @return array |
||
548 | */ |
||
549 | private function getFooterOptions() |
||
560 | } |
||
561 |