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 |
||
11 | class ItemCollection extends ArrayCollection |
||
|
|||
12 | { |
||
13 | protected $type; |
||
14 | |||
15 | 116 | public function __construct($Items, $type = null) |
|
16 | { |
||
17 | 116 | $this->type = is_null($type) ? Order::class : $type; |
|
18 | |||
19 | 116 | if ($Items instanceof Collection) { |
|
20 | 116 | $Items = $Items->toArray(); |
|
21 | } |
||
22 | 116 | parent::__construct($Items); |
|
23 | } |
||
24 | |||
25 | 27 | public function reduce(\Closure $func, $initial = null) |
|
26 | { |
||
27 | 27 | return array_reduce($this->toArray(), $func, $initial); |
|
28 | } |
||
29 | |||
30 | // 明細種別ごとに返すメソッド作る |
||
31 | 25 | public function getProductClasses() |
|
32 | { |
||
33 | 25 | return $this->filter( |
|
34 | function (ItemInterface $ShipmentItem) { |
||
35 | 19 | return $ShipmentItem->isProduct(); |
|
36 | 25 | }); |
|
37 | } |
||
38 | |||
39 | 24 | public function getDeliveryFees() |
|
40 | { |
||
41 | 24 | return $this->filter( |
|
42 | function (ItemInterface $ShipmentItem) { |
||
43 | 18 | return $ShipmentItem->isDeliveryFee(); |
|
44 | 24 | }); |
|
45 | } |
||
46 | |||
47 | 25 | public function getCharges() |
|
48 | { |
||
49 | 25 | return $this->filter( |
|
50 | function (ItemInterface $ShipmentItem) { |
||
51 | 19 | return $ShipmentItem->isCharge(); |
|
52 | 25 | }); |
|
53 | } |
||
54 | |||
55 | 23 | public function getDiscounts() |
|
56 | { |
||
57 | 23 | return $this->filter( |
|
58 | function (ItemInterface $ShipmentItem) { |
||
59 | 17 | return $ShipmentItem->isDiscount(); |
|
60 | 23 | }); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * 同名の明細が存在するかどうか. |
||
65 | * |
||
66 | * TODO 暫定対応. 本来は明細種別でチェックする. |
||
67 | */ |
||
68 | View Code Duplication | public function hasProductByName($productName) |
|
69 | { |
||
70 | $ShipmentItems = $this->filter( |
||
71 | function (ItemInterface $ShipmentItem) use ($productName) { |
||
72 | /* @var ShipmentItem $ShipmentItem */ |
||
73 | return $ShipmentItem->getProductName() == $productName; |
||
74 | }); |
||
75 | |||
76 | return !$ShipmentItems->isEmpty(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * 指定した受注明細区分の明細が存在するかどうか. |
||
81 | * |
||
82 | * @param OrderItemType $OrderItemType 受注区分 |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | 1 | View Code Duplication | public function hasItemByOrderItemType($OrderItemType) |
87 | { |
||
88 | $filteredItems = $this->filter(function (ItemInterface $ShipmentItem) use ($OrderItemType) { |
||
89 | /* @var ShipmentItem $ShipmentItem */ |
||
90 | 1 | return $ShipmentItem->getOrderItemType() && $ShipmentItem->getOrderItemType()->getId() == $OrderItemType->getId(); |
|
91 | 1 | }); |
|
92 | |||
93 | 1 | return !$filteredItems->isEmpty(); |
|
94 | } |
||
95 | |||
96 | public function getType() |
||
97 | { |
||
98 | return $this->type; |
||
99 | } |
||
100 | |||
101 | 116 | public function sort() |
|
136 | } |
||
137 |