Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PurchaseFlow 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 PurchaseFlow, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PurchaseFlow |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $flowType; |
||
28 | |||
29 | /** |
||
30 | * @var ArrayCollection|ItemPreprocessor[] |
||
31 | */ |
||
32 | protected $itemPreprocessors; |
||
33 | |||
34 | /** |
||
35 | * @var ArrayCollection|ItemHolderPreprocessor[] |
||
36 | */ |
||
37 | protected $itemHolderPreprocessors; |
||
38 | |||
39 | /** |
||
40 | * @var ArrayCollection|ItemValidator[] |
||
41 | */ |
||
42 | protected $itemValidators; |
||
43 | |||
44 | /** |
||
45 | * @var ArrayCollection|ItemHolderValidator[] |
||
46 | */ |
||
47 | protected $itemHolderValidators; |
||
48 | |||
49 | /** |
||
50 | * @var ArrayCollection|ItemHolderPostValidator[] |
||
51 | */ |
||
52 | protected $itemHolderPostValidators; |
||
53 | |||
54 | /** |
||
55 | * @var ArrayCollection|DiscountProcessor[] |
||
56 | */ |
||
57 | protected $discountProcessors; |
||
58 | |||
59 | /** |
||
60 | * @var ArrayCollection|PurchaseProcessor[] |
||
61 | */ |
||
62 | protected $purchaseProcessors; |
||
63 | |||
64 | public function __construct() |
||
74 | |||
75 | public function setFlowType($flowType) |
||
79 | |||
80 | public function setPurchaseProcessors(ArrayCollection $processors) |
||
84 | |||
85 | public function setItemValidators(ArrayCollection $itemValidators) |
||
89 | |||
90 | public function setItemHolderValidators(ArrayCollection $itemHolderValidators) |
||
94 | |||
95 | public function setItemPreprocessors(ArrayCollection $itemPreprocessors) |
||
99 | |||
100 | public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors) |
||
104 | |||
105 | public function setItemHolderPostValidators(ArrayCollection $itemHolderPostValidators) |
||
109 | |||
110 | public function setDiscountProcessors(ArrayCollection $discountProcessors) |
||
114 | |||
115 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
179 | |||
180 | /** |
||
181 | * 購入フロー仮確定処理. |
||
182 | * |
||
183 | * @param ItemHolderInterface $target |
||
184 | * @param PurchaseContext $context |
||
185 | * |
||
186 | * @throws PurchaseException |
||
187 | */ |
||
188 | public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
||
196 | |||
197 | /** |
||
198 | * 購入フロー確定処理. |
||
199 | * |
||
200 | * @param ItemHolderInterface $target |
||
201 | * @param PurchaseContext $context |
||
202 | * |
||
203 | * @throws PurchaseException |
||
204 | */ |
||
205 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
213 | |||
214 | /** |
||
215 | * 購入フロー仮確定取り消し処理. |
||
216 | * |
||
217 | * @param ItemHolderInterface $target |
||
218 | * @param PurchaseContext $context |
||
219 | */ |
||
220 | public function rollback(ItemHolderInterface $target, PurchaseContext $context) |
||
228 | |||
229 | public function addPurchaseProcessor(PurchaseProcessor $processor) |
||
233 | |||
234 | public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor) |
||
238 | |||
239 | public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor) |
||
243 | |||
244 | public function addItemValidator(ItemValidator $itemValidator) |
||
248 | |||
249 | public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator) |
||
253 | |||
254 | public function addItemHolderPostValidator(ItemHolderPostValidator $itemHolderValidator) |
||
258 | |||
259 | public function addDiscountProcessor(DiscountProcessor $discountProcessor) |
||
263 | |||
264 | /** |
||
265 | * @param ItemHolderInterface $itemHolder |
||
266 | */ |
||
267 | protected function calculateTotal(ItemHolderInterface $itemHolder) |
||
268 | { |
||
269 | $total = array_reduce($itemHolder->getItems()->toArray(), function ($sum, ItemInterface $item) { |
||
|
|||
270 | $sum += $item->getPriceIncTax() * $item->getQuantity(); |
||
271 | |||
272 | return $sum; |
||
273 | }, 0); |
||
274 | $itemHolder->setTotal($total); |
||
275 | // TODO |
||
276 | if ($itemHolder instanceof Order) { |
||
277 | // Order には PaymentTotal もセットする |
||
278 | $itemHolder->setPaymentTotal($total); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | View Code Duplication | protected function calculateSubTotal(ItemHolderInterface $itemHolder) |
|
297 | |||
298 | /** |
||
299 | * @param ItemHolderInterface $itemHolder |
||
300 | */ |
||
301 | View Code Duplication | protected function calculateDeliveryFeeTotal(ItemHolderInterface $itemHolder) |
|
312 | |||
313 | /** |
||
314 | * @param ItemHolderInterface $itemHolder |
||
315 | */ |
||
316 | View Code Duplication | protected function calculateDiscount(ItemHolderInterface $itemHolder) |
|
328 | |||
329 | /** |
||
330 | * @param ItemHolderInterface $itemHolder |
||
331 | */ |
||
332 | View Code Duplication | protected function calculateCharge(ItemHolderInterface $itemHolder) |
|
343 | |||
344 | /** |
||
345 | * @param ItemHolderInterface $itemHolder |
||
346 | */ |
||
347 | protected function calculateTax(ItemHolderInterface $itemHolder) |
||
361 | |||
362 | /** |
||
363 | * @param ItemHolderInterface $itemHolder |
||
364 | */ |
||
365 | protected function calculateAll(ItemHolderInterface $itemHolder) |
||
374 | |||
375 | /** |
||
376 | * PurchaseFlow をツリー表示します. |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | public function dump() |
||
410 | |||
411 | /** |
||
412 | * @return string |
||
413 | */ |
||
414 | public function __toString() |
||
418 | } |
||
419 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.