| Conditions | 15 |
| Paths | 9 |
| Total Lines | 45 |
| Lines | 8 |
| Ratio | 17.78 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 52 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
| 53 | { |
||
| 54 | if (is_null($context->getOriginHolder())) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | $From = $context->getOriginHolder(); |
||
| 59 | $To = $itemHolder; |
||
| 60 | $diff = $this->getDiffOfQuantities($From, $To); |
||
| 61 | |||
| 62 | foreach ($diff as $id => $quantity) { |
||
| 63 | /** @var ProductClass $ProductClass */ |
||
| 64 | $ProductClass = $this->productClassRepository->find($id); |
||
| 65 | if ($ProductClass->isStockUnlimited()) { |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | $stock = $ProductClass->getStock(); |
||
| 70 | $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 | $this->throwInvalidItemException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()])); |
||
| 83 | } |
||
| 84 | // ステータスをキャンセルから対応中に変更した場合 |
||
| 85 | } elseif ($To->getOrderStatus() && $To->getOrderStatus()->getId() == OrderStatus::IN_PROGRESS |
||
| 86 | && $From->getOrderStatus() && $From->getOrderStatus()->getId() == OrderStatus::CANCEL) { |
||
| 87 | if ($stock - $toQuantity < 0) { |
||
| 88 | $this->throwInvalidItemException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()])); |
||
| 89 | } |
||
| 90 | View Code Duplication | } else { |
|
| 91 | if ($stock - $quantity < 0) { |
||
| 92 | $this->throwInvalidItemException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()])); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 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: