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 |
||
| 27 | class AddPointProcessor extends ItemHolderPostValidator |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var BaseInfo |
||
| 31 | */ |
||
| 32 | protected $BaseInfo; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * AddPointProcessor constructor. |
||
| 36 | * |
||
| 37 | * @param BaseInfoRepository $baseInfoRepository |
||
| 38 | */ |
||
| 39 | public function __construct(BaseInfoRepository $baseInfoRepository) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param ItemHolderInterface $itemHolder |
||
| 46 | * @param PurchaseContext $context |
||
| 47 | */ |
||
| 48 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * 付与ポイントを計算. |
||
| 61 | * |
||
| 62 | * @param ItemHolderInterface $itemHolder |
||
| 63 | * |
||
| 64 | * @return int |
||
| 65 | */ |
||
| 66 | private function calculateAddPoint(ItemHolderInterface $itemHolder) |
||
| 67 | { |
||
| 68 | $basicPointRate = $this->BaseInfo->getBasicPointRate(); |
||
| 69 | |||
| 70 | // 明細ごとのポイントを集計 |
||
| 71 | $totalPoint = array_reduce($itemHolder->getItems()->toArray(), |
||
|
|
|||
| 72 | function ($carry, ItemInterface $item) use ($basicPointRate) { |
||
| 73 | $pointRate = $item->isProduct() ? $item->getProductClass()->getPointRate() : null; |
||
| 74 | if ($pointRate === null) { |
||
| 75 | $pointRate = $basicPointRate; |
||
| 76 | } |
||
| 77 | |||
| 78 | // TODO: ポイントは税抜き分しか割引されない、ポイント明細は税抜きのままでいいのか? |
||
| 79 | $point = 0; |
||
| 80 | if ($item->isPoint()) { |
||
| 81 | $point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity(); |
||
| 82 | // Only calc point on product |
||
| 83 | } elseif ($item->isProduct()) { |
||
| 84 | // ポイント = 単価 * ポイント付与率 * 数量 |
||
| 85 | $point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity(); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $carry + $point; |
||
| 89 | }, 0); |
||
| 90 | |||
| 91 | return $totalPoint < 0 ? 0 : $totalPoint; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Processorが実行出来るかどうかを返す. |
||
| 96 | * |
||
| 97 | * 以下を満たす場合に実行できる. |
||
| 98 | * |
||
| 99 | * - ポイント設定が有効であること. |
||
| 100 | * - $itemHolderがOrderエンティティであること. |
||
| 101 | * - 会員のOrderであること. |
||
| 102 | * |
||
| 103 | * @param ItemHolderInterface $itemHolder |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | View Code Duplication | private function supports(ItemHolderInterface $itemHolder) |
|
| 123 | } |
||
| 124 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.