Complex classes like OrderItem 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 OrderItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class OrderItem implements OrderItemInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var mixed |
||
25 | */ |
||
26 | protected $id; |
||
27 | |||
28 | /** |
||
29 | * @var OrderInterface |
||
30 | */ |
||
31 | protected $order; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $quantity = 0; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $unitPrice = 0; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $total = 0; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $immutable = false; |
||
52 | |||
53 | /** |
||
54 | * @var Collection|OrderItemUnitInterface[] |
||
55 | */ |
||
56 | protected $units; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $unitsTotal = 0; |
||
62 | |||
63 | /** |
||
64 | * @var Collection|AdjustmentInterface[] |
||
65 | */ |
||
66 | protected $adjustments; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $adjustmentsTotal = 0; |
||
72 | |||
73 | public function __construct() |
||
74 | { |
||
75 | $this->adjustments = new ArrayCollection(); |
||
76 | $this->units = new ArrayCollection(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function getId() |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function getQuantity() |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function getOrder() |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function setOrder(OrderInterface $order = null) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function getUnitPrice() |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function setUnitPrice($unitPrice) |
||
142 | { |
||
143 | if (!is_int($unitPrice)) { |
||
144 | throw new \InvalidArgumentException('Unit price must be an integer.'); |
||
145 | } |
||
146 | |||
147 | $this->unitPrice = $unitPrice; |
||
148 | $this->recalculateUnitsTotal(); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getTotal() |
||
155 | { |
||
156 | return $this->total; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function recalculateAdjustmentsTotal() |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function recalculateUnitsTotal() |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function equals(OrderItemInterface $orderItem) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function isImmutable() |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function setImmutable($immutable) |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function getUnits() |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function addUnit(OrderItemUnitInterface $unit) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function removeUnit(OrderItemUnitInterface $unit) |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function hasUnit(OrderItemUnitInterface $unit) |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function getAdjustments($type = null) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function getAdjustmentsRecursively($type = null) |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function addAdjustment(AdjustmentInterface $adjustment) |
||
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | public function removeAdjustment(AdjustmentInterface $adjustment) |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function hasAdjustment(AdjustmentInterface $adjustment) |
||
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | public function getAdjustmentsTotal($type = null) |
||
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | public function getAdjustmentsTotalRecursively($type = null) |
||
349 | |||
350 | /** |
||
351 | * {@inheritdoc} |
||
352 | */ |
||
353 | public function removeAdjustments($type) |
||
363 | |||
364 | /** |
||
365 | * {@inheritdoc} |
||
366 | */ |
||
367 | public function removeAdjustmentsRecursively($type = null) |
||
374 | |||
375 | /** |
||
376 | * {@inheritdoc} |
||
377 | */ |
||
378 | public function clearAdjustments() |
||
383 | |||
384 | /** |
||
385 | * Recalculates total after units total or adjustments total change. |
||
386 | */ |
||
387 | protected function recalculateTotal() |
||
399 | |||
400 | /** |
||
401 | * @param AdjustmentInterface $adjustment |
||
402 | */ |
||
403 | protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment) |
||
410 | |||
411 | /** |
||
412 | * @param AdjustmentInterface $adjustment |
||
413 | */ |
||
414 | protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment) |
||
421 | } |
||
422 |