Conditions | 13 |
Paths | 84 |
Total Lines | 44 |
Lines | 0 |
Ratio | 0 % |
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 |
||
46 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
47 | { |
||
48 | foreach ($itemHolder->getItems() as $item) { |
||
49 | if ($item->isProduct() && $item->getQuantity() == 0) { |
||
50 | if ($itemHolder instanceof Order) { |
||
51 | foreach ($itemHolder->getShippings() as $Shipping) { |
||
52 | $Shipping->removeOrderItem($item); |
||
53 | } |
||
54 | $itemHolder->removeOrderItem($item); |
||
|
|||
55 | } else { |
||
56 | $itemHolder->removeItem($item); |
||
57 | } |
||
58 | $this->entityManager->remove($item); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | if (!$itemHolder instanceof Order) { |
||
63 | return; |
||
64 | } |
||
65 | |||
66 | // 受注の場合は, Shippingに紐づく商品明細がない場合はShippingも削除する. |
||
67 | foreach ($itemHolder->getShippings() as $Shipping) { |
||
68 | $hasProductItem = false; |
||
69 | foreach ($Shipping->getOrderItems() as $item) { |
||
70 | if ($item->isProduct()) { |
||
71 | $hasProductItem = true; |
||
72 | break; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | if (!$hasProductItem) { |
||
77 | foreach ($Shipping->getOrderItems() as $item) { |
||
78 | $this->entityManager->remove($item); |
||
79 | } |
||
80 | $itemHolder->removeShipping($Shipping); |
||
81 | $this->entityManager->remove($Shipping); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | // Shippingがなくなれば購入エラー. |
||
86 | if (count($itemHolder->getShippings()) < 1) { |
||
87 | $this->throwInvalidItemException('front.shopping.empty_items_error'); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.