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 |
||
| 22 | class ItemCollection extends ArrayCollection |
||
|
|
|||
| 23 | { |
||
| 24 | protected $type; |
||
| 25 | |||
| 26 | 329 | public function __construct($Items, $type = null) |
|
| 35 | |||
| 36 | 299 | public function reduce(\Closure $func, $initial = null) |
|
| 40 | |||
| 41 | // 明細種別ごとに返すメソッド作る |
||
| 42 | 294 | public function getProductClasses() |
|
| 49 | |||
| 50 | 294 | public function getDeliveryFees() |
|
| 57 | |||
| 58 | 294 | public function getCharges() |
|
| 65 | |||
| 66 | 294 | public function getDiscounts() |
|
| 67 | { |
||
| 68 | 294 | return $this->filter( |
|
| 69 | 294 | function (ItemInterface $OrderItem) { |
|
| 70 | 290 | return $OrderItem->isDiscount() || $OrderItem->isPoint(); |
|
| 71 | 294 | }); |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * 同名の明細が存在するかどうか. |
||
| 76 | * |
||
| 77 | * TODO 暫定対応. 本来は明細種別でチェックする. |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function hasProductByName($productName) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * 指定した受注明細区分の明細が存在するかどうか. |
||
| 92 | * |
||
| 93 | * @param OrderItemType $OrderItemType 受注区分 |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function hasItemByOrderItemType($OrderItemType) |
|
| 106 | |||
| 107 | public function getType() |
||
| 111 | |||
| 112 | 329 | public function sort() |
|
| 155 | } |
||
| 156 |