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 | 759 | /** |
|
50 | * @var ArrayCollection|ItemHolderPostValidator[] |
||
51 | 759 | */ |
|
52 | 759 | protected $itemHolderPostValidators; |
|
53 | 759 | ||
54 | 759 | /** |
|
55 | 759 | * @var ArrayCollection|DiscountProcessor[] |
|
56 | */ |
||
57 | protected $discountProcessors; |
||
58 | 728 | ||
59 | /** |
||
60 | 728 | * @var ArrayCollection|PurchaseProcessor[] |
|
61 | */ |
||
62 | protected $purchaseProcessors; |
||
63 | 753 | ||
64 | public function __construct() |
||
74 | |||
75 | public function setFlowType($flowType) |
||
79 | |||
80 | 753 | public function setPurchaseProcessors(ArrayCollection $processors) |
|
84 | |||
85 | 295 | public function setItemValidators(ArrayCollection $itemValidators) |
|
89 | 295 | ||
90 | 291 | public function setItemHolderValidators(ArrayCollection $itemHolderValidators) |
|
94 | |||
95 | 295 | public function setItemPreprocessors(ArrayCollection $itemPreprocessors) |
|
99 | |||
100 | public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors) |
||
104 | 291 | ||
105 | 81 | public function setItemHolderPostValidators(ArrayCollection $itemHolderPostValidators) |
|
109 | |||
110 | 295 | public function setDiscountProcessors(ArrayCollection $discountProcessors) |
|
114 | 266 | ||
115 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
179 | |||
180 | 3 | /** |
|
181 | * 購入フロー仮確定処理. |
||
182 | 3 | * |
|
183 | * @param ItemHolderInterface $target |
||
184 | * @param PurchaseContext $context |
||
185 | 36 | * |
|
186 | * @throws PurchaseException |
||
187 | 36 | */ |
|
188 | public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
||
196 | 291 | ||
197 | /** |
||
198 | 291 | * 購入フロー確定処理. |
|
199 | 295 | * |
|
200 | 295 | * @param ItemHolderInterface $target |
|
201 | * @param PurchaseContext $context |
||
202 | 295 | * |
|
203 | * @throws PurchaseException |
||
204 | 264 | */ |
|
205 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
213 | 234 | ||
214 | /** |
||
215 | 234 | * 購入フロー仮確定取り消し処理. |
|
216 | 295 | * |
|
217 | * @param ItemHolderInterface $target |
||
218 | 295 | * @param PurchaseContext $context |
|
219 | */ |
||
220 | 264 | public function rollback(ItemHolderInterface $target, PurchaseContext $context) |
|
228 | |||
229 | 295 | public function addPurchaseProcessor(PurchaseProcessor $processor) |
|
233 | |||
234 | 205 | public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor) |
|
238 | |||
239 | public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor) |
||
243 | |||
244 | 295 | public function addItemValidator(ItemValidator $itemValidator) |
|
248 | |||
249 | 170 | public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator) |
|
253 | |||
254 | public function addItemHolderPostValidator(ItemHolderPostValidator $itemHolderValidator) |
||
258 | 295 | ||
259 | public function addDiscountProcessor(DiscountProcessor $discountProcessor) |
||
263 | 154 | ||
264 | /** |
||
265 | 154 | * @param ItemHolderInterface $itemHolder |
|
266 | 295 | */ |
|
267 | 295 | 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 | 295 | }, 0); |
|
274 | $itemHolder->setTotal($total); |
||
275 | 295 | // TODO |
|
276 | 295 | if ($itemHolder instanceof Order) { |
|
277 | 291 | // Order には PaymentTotal もセットする |
|
278 | 264 | $itemHolder->setPaymentTotal($total); |
|
279 | } |
||
280 | 79 | } |
|
281 | |||
282 | View Code Duplication | protected function calculateSubTotal(ItemHolderInterface $itemHolder) |
|
297 | 295 | ||
298 | 295 | /** |
|
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.