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:
1 | <?php |
||
29 | class StockDiffProcessor extends ItemHolderValidator implements PurchaseProcessor |
||
30 | { |
||
31 | /** |
||
32 | * @var ProductClassRepository |
||
33 | */ |
||
34 | protected $productClassRepository; |
||
35 | |||
36 | /** |
||
37 | * StockProcessor constructor. |
||
38 | * |
||
39 | * @param ProductClassRepository $productClassRepository |
||
40 | */ |
||
41 | 713 | public function __construct(ProductClassRepository $productClassRepository) |
|
45 | |||
46 | /** |
||
47 | * @param ItemHolderInterface $itemHolder |
||
48 | * @param PurchaseContext $context |
||
49 | * |
||
50 | * @throws \Eccube\Service\PurchaseFlow\InvalidItemException |
||
51 | */ |
||
52 | 156 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
53 | { |
||
54 | 156 | if (is_null($context->getOriginHolder())) { |
|
55 | return; |
||
56 | } |
||
57 | |||
58 | 156 | $From = $context->getOriginHolder(); |
|
59 | 156 | $To = $itemHolder; |
|
60 | 156 | $diff = $this->getDiffOfQuantities($From, $To); |
|
61 | |||
62 | 156 | foreach ($diff as $id => $quantity) { |
|
63 | /** @var ProductClass $ProductClass */ |
||
64 | 156 | $ProductClass = $this->productClassRepository->find($id); |
|
65 | 156 | if ($ProductClass->isStockUnlimited()) { |
|
66 | continue; |
||
67 | } |
||
68 | 156 | ||
69 | $stock = $ProductClass->getStock(); |
||
70 | 156 | $Items = $To->getProductOrderItems(); |
|
|
|||
71 | $Items = array_filter($Items, function ($Item) use ($id) { |
||
72 | return $Item->getProductClass()->getId() == $id; |
||
73 | }); |
||
74 | $toQuantity = array_reduce($Items, function ($quantity, $Item) { |
||
75 | return $quantity += $Item->getQuantity(); |
||
76 | }, 0); |
||
77 | |||
78 | // ステータスをキャンセルに変更した場合 |
||
79 | if ($To->getOrderStatus() && $To->getOrderStatus()->getId() == OrderStatus::CANCEL |
||
80 | && $From->getOrderStatus() && $From->getOrderStatus()->getId() != OrderStatus::CANCEL) { |
||
81 | View Code Duplication | if ($stock + $toQuantity < 0) { |
|
82 | 156 | $this->throwInvalidItemException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()])); |
|
83 | 156 | } |
|
84 | // ステータスをキャンセルから対応中に変更した場合 |
||
85 | } elseif ($To->getOrderStatus() && $To->getOrderStatus()->getId() == OrderStatus::IN_PROGRESS |
||
86 | && $From->getOrderStatus() && $From->getOrderStatus()->getId() == OrderStatus::CANCEL) { |
||
87 | View Code Duplication | if ($stock - $toQuantity < 0) { |
|
88 | $this->throwInvalidItemException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()])); |
||
89 | 156 | } |
|
90 | View Code Duplication | } else { |
|
91 | 156 | if ($stock - $quantity < 0) { |
|
92 | 156 | $this->throwInvalidItemException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()])); |
|
93 | 156 | } |
|
94 | } |
||
95 | 156 | } |
|
96 | 156 | } |
|
97 | |||
98 | 156 | protected function getDiffOfQuantities(ItemHolderInterface $From, ItemHolderInterface $To) |
|
124 | |||
125 | 156 | protected function getQuantityByProductClass(ItemHolderInterface $ItemHolder) |
|
141 | 5 | ||
142 | /** |
||
143 | 5 | * 受注の仮確定処理を行います。 |
|
144 | * |
||
145 | * @param ItemHolderInterface $target |
||
146 | * @param PurchaseContext $context |
||
147 | 5 | * |
|
148 | * @throws PurchaseException |
||
149 | 5 | */ |
|
150 | public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
||
176 | 5 | ||
177 | /** |
||
178 | * 受注の確定処理を行います。 |
||
179 | 5 | * |
|
180 | * @param ItemHolderInterface $target |
||
181 | * @param PurchaseContext $context |
||
182 | * |
||
183 | * @throws PurchaseException |
||
184 | */ |
||
185 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
190 | |||
191 | /** |
||
192 | * 仮確定した受注データの取り消し処理を行います。 |
||
193 | * |
||
194 | * @param ItemHolderInterface $itemHolder |
||
195 | * @param PurchaseContext $context |
||
196 | */ |
||
197 | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
202 | } |
||
203 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: