Complex classes like Adjustment 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 Adjustment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Adjustment implements AdjustmentInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var mixed |
||
21 | */ |
||
22 | protected $id; |
||
23 | |||
24 | /** |
||
25 | * @var OrderInterface |
||
26 | */ |
||
27 | protected $order; |
||
28 | |||
29 | /** |
||
30 | * @var OrderItemInterface |
||
31 | */ |
||
32 | protected $orderItem; |
||
33 | |||
34 | /** |
||
35 | * @var OrderItemUnitInterface |
||
36 | */ |
||
37 | protected $orderItemUnit; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $type; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $description; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $amount = 0; |
||
53 | |||
54 | /** |
||
55 | * Is adjustment neutral? |
||
56 | * Should it modify the order total? |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $neutral = false; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $locked = false; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $originId; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $originType; |
||
76 | |||
77 | /** |
||
78 | * @var \DateTime |
||
79 | */ |
||
80 | protected $createdAt; |
||
81 | |||
82 | /** |
||
83 | * @var \DateTime |
||
84 | */ |
||
85 | protected $updatedAt; |
||
86 | |||
87 | public function __construct() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function getId() |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getAdjustable() |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function setAdjustable(AdjustableInterface $adjustable = null) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function getType() |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function setType($type) |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getDescription() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function setDescription($description) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function getAmount() |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function setAmount($amount) |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function isNeutral() |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function setNeutral($neutral) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function isLocked() |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function lock() |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function unlock() |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function isCharge() |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function isCredit() |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function getOriginId() |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function setOriginId($originId) |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function getOriginType() |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function setOriginType($originType) |
||
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | public function getCreatedAt() |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function setCreatedAt(\DateTime $createdAt) |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function getUpdatedAt() |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | */ |
||
320 | public function setUpdatedAt(\DateTime $updatedAt) |
||
324 | |||
325 | private function recalculateAdjustable() |
||
332 | |||
333 | /** |
||
334 | * @param AdjustableInterface $adjustable |
||
335 | * |
||
336 | * @throws \InvalidArgumentException when adjustable class is not supported |
||
337 | */ |
||
338 | private function assignAdjustable(AdjustableInterface $adjustable) |
||
350 | |||
351 | /** |
||
352 | * @throws \LogicException when adjustment is locked |
||
353 | */ |
||
354 | private function assertNotLocked() |
||
360 | } |
||
361 |