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 |
||
20 | class Order implements OrderInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var mixed |
||
24 | */ |
||
25 | protected $id; |
||
26 | |||
27 | /** |
||
28 | * @var \DateTime |
||
29 | */ |
||
30 | protected $completedAt; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $number; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $additionalInformation; |
||
41 | |||
42 | /** |
||
43 | * @var Collection|OrderItemInterface[] |
||
44 | */ |
||
45 | protected $items; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $itemsTotal = 0; |
||
51 | |||
52 | /** |
||
53 | * @var Collection|AdjustmentInterface[] |
||
54 | */ |
||
55 | protected $adjustments; |
||
56 | |||
57 | /** |
||
58 | * @var Collection|CommentInterface[] |
||
59 | */ |
||
60 | protected $comments; |
||
61 | |||
62 | /** |
||
63 | * @var Collection|IdentityInterface[] |
||
64 | */ |
||
65 | protected $identities; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $adjustmentsTotal = 0; |
||
71 | |||
72 | /** |
||
73 | * Calculated total. |
||
74 | * Items total + adjustments total. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $total = 0; |
||
79 | |||
80 | /** |
||
81 | * @var \DateTime |
||
82 | */ |
||
83 | protected $createdAt; |
||
84 | |||
85 | /** |
||
86 | * @var \DateTime |
||
87 | */ |
||
88 | protected $updatedAt; |
||
89 | |||
90 | /** |
||
91 | * @var \DateTime |
||
92 | */ |
||
93 | protected $deletedAt; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $state = OrderInterface::STATE_CART; |
||
99 | |||
100 | public function __construct() |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function getId() |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function isCompleted() |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function complete() |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getCompletedAt() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function setCompletedAt(\DateTime $completedAt = null) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function getNumber() |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function setNumber($number) |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function getSequenceType() |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function getItems() |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function clearItems() |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function countItems() |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function addItem(OrderItemInterface $item) |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function removeItem(OrderItemInterface $item) |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function hasItem(OrderItemInterface $item) |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function getItemsTotal() |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function recalculateItemsTotal() |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | public function getComments() |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function addComment(CommentInterface $comment) |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function removeComment(CommentInterface $comment) |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function getTotal() |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function getCreatedAt() |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function setCreatedAt(\DateTime $createdAt) |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function getUpdatedAt() |
||
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | public function setUpdatedAt(\DateTime $updatedAt) |
||
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | public function isDeleted() |
||
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | public function getDeletedAt() |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | public function setDeletedAt(\DateTime $deletedAt = null) |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function setState($state) |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function getState() |
||
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | public function getTotalItems() |
||
375 | |||
376 | /** |
||
377 | * {@inheritdoc} |
||
378 | */ |
||
379 | public function getTotalQuantity() |
||
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | public function isEmpty() |
||
397 | |||
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | public function addIdentity(IdentityInterface $identity) |
||
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | */ |
||
414 | public function hasIdentity(IdentityInterface $identity) |
||
418 | |||
419 | /** |
||
420 | * {@inheritdoc} |
||
421 | */ |
||
422 | public function getIdentities() |
||
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | */ |
||
430 | public function removeIdentity(IdentityInterface $identity) |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | */ |
||
441 | public function getAdjustments($type = null) |
||
451 | |||
452 | /** |
||
453 | * {@inheritdoc} |
||
454 | */ |
||
455 | public function addAdjustment(AdjustmentInterface $adjustment) |
||
463 | |||
464 | /** |
||
465 | * {@inheritdoc} |
||
466 | */ |
||
467 | public function removeAdjustment(AdjustmentInterface $adjustment) |
||
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | public function hasAdjustment(AdjustmentInterface $adjustment) |
||
483 | |||
484 | /** |
||
485 | * {@inheritdoc} |
||
486 | */ |
||
487 | public function getAdjustmentsTotal($type = null) |
||
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | public function removeAdjustments($type) |
||
514 | |||
515 | /** |
||
516 | * {@inheritdoc} |
||
517 | */ |
||
518 | public function clearAdjustments() |
||
523 | |||
524 | /** |
||
525 | * {@inheritdoc} |
||
526 | */ |
||
527 | public function recalculateAdjustmentsTotal() |
||
539 | |||
540 | /** |
||
541 | * Calculate total. |
||
542 | * Items total + Adjustments total. |
||
543 | */ |
||
544 | protected function recalculateTotal() |
||
552 | |||
553 | /** |
||
554 | * @param AdjustmentInterface $adjustment |
||
555 | */ |
||
556 | protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment) |
||
563 | |||
564 | /** |
||
565 | * @param AdjustmentInterface $adjustment |
||
566 | */ |
||
567 | protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment) |
||
574 | |||
575 | /** |
||
576 | * {@inheritdoc} |
||
577 | */ |
||
578 | public function getAdditionalInformation() |
||
582 | |||
583 | /** |
||
584 | * {@inheritdoc} |
||
585 | */ |
||
586 | public function setAdditionalInformation($information) |
||
590 | } |
||
591 |