Complex classes like Order 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 Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Order implements OrderInterface |
||
22 | { |
||
23 | use TimestampableTrait; |
||
24 | |||
25 | /** |
||
26 | * @var mixed |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * @var \DateTime |
||
32 | */ |
||
33 | protected $completedAt; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $number; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $additionalInformation; |
||
44 | |||
45 | /** |
||
46 | * @var Collection|OrderItemInterface[] |
||
47 | */ |
||
48 | protected $items; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $itemsTotal = 0; |
||
54 | |||
55 | /** |
||
56 | * @var Collection|AdjustmentInterface[] |
||
57 | */ |
||
58 | protected $adjustments; |
||
59 | |||
60 | /** |
||
61 | * @var Collection|CommentInterface[] |
||
62 | */ |
||
63 | protected $comments; |
||
64 | |||
65 | /** |
||
66 | * @var Collection|IdentityInterface[] |
||
67 | */ |
||
68 | protected $identities; |
||
69 | |||
70 | /** |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $adjustmentsTotal = 0; |
||
74 | |||
75 | /** |
||
76 | * Calculated total. |
||
77 | * Items total + adjustments total. |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | protected $total = 0; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $state = OrderInterface::STATE_CART; |
||
87 | |||
88 | public function __construct() |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function getId() |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function isCompleted() |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function complete() |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function getCompletedAt() |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function setCompletedAt(\DateTime $completedAt = null) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function getNumber() |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function setNumber($number) |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function getSequenceType() |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getItems() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function clearItems() |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function countItems() |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function addItem(OrderItemInterface $item) |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function removeItem(OrderItemInterface $item) |
||
207 | { |
||
208 | if ($this->hasItem($item)) { |
||
209 | $this->items->removeElement($item); |
||
210 | $this->itemsTotal -= $item->getTotal(); |
||
211 | $this->recalculateTotal(); |
||
212 | $item->setOrder(null); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function hasItem(OrderItemInterface $item) |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function getItemsTotal() |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | public function recalculateItemsTotal() |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function getComments() |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function addComment(CommentInterface $comment) |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function removeComment(CommentInterface $comment) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function getTotal() |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function setState($state) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function getState() |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function getTotalQuantity() |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function isEmpty() |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function addIdentity(IdentityInterface $identity) |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | */ |
||
336 | public function hasIdentity(IdentityInterface $identity) |
||
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | public function getIdentities() |
||
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | public function removeIdentity(IdentityInterface $identity) |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function getAdjustments($type = null) |
||
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | public function getAdjustmentsRecursively($type = null) |
||
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | public function addAdjustment(AdjustmentInterface $adjustment) |
||
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | public function removeAdjustment(AdjustmentInterface $adjustment) |
||
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | */ |
||
414 | public function hasAdjustment(AdjustmentInterface $adjustment) |
||
418 | |||
419 | /** |
||
420 | * {@inheritdoc} |
||
421 | */ |
||
422 | public function getAdjustmentsTotal($type = null) |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | public function getAdjustmentsTotalRecursively($type = null) |
||
448 | |||
449 | /** |
||
450 | * {@inheritdoc} |
||
451 | */ |
||
452 | public function removeAdjustments($type) |
||
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | public function removeAdjustmentsRecursively($type = null) |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | public function recalculateAdjustmentsTotal() |
||
489 | |||
490 | /** |
||
491 | * Calculate total. |
||
492 | * Items total + Adjustments total. |
||
493 | */ |
||
494 | protected function recalculateTotal() |
||
502 | |||
503 | /** |
||
504 | * @param AdjustmentInterface $adjustment |
||
505 | */ |
||
506 | protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment) |
||
513 | |||
514 | /** |
||
515 | * @param AdjustmentInterface $adjustment |
||
516 | */ |
||
517 | protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment) |
||
524 | |||
525 | /** |
||
526 | * {@inheritdoc} |
||
527 | */ |
||
528 | public function getAdditionalInformation() |
||
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | public function setAdditionalInformation($information) |
||
540 | } |
||
541 |