Completed
Push — experimental/3.1 ( 682baa...396eec )
by Kiyotaka
112:16 queued 111:58
created

ItemCollection   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 126
Duplicated Lines 15.08 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
dl 19
loc 126
ccs 46
cts 58
cp 0.7931
rs 10
c 0
b 0
f 0
wmc 24
lcom 2
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A reduce() 0 4 1
A getProductClasses() 0 7 1
A getDeliveryFees() 0 7 1
A getCharges() 0 7 1
A getDiscounts() 0 7 1
A hasProductByName() 10 10 1
A hasItemByOrderItemType() 9 9 2
A getType() 0 4 1
C sort() 0 35 12

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Eccube\Service\PurchaseFlow;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Eccube\Entity\ItemInterface;
8
use Eccube\Entity\Master\OrderItemType;
9
use Eccube\Entity\Order;
10
11
class ItemCollection extends ArrayCollection
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
12
{
13
    protected $type;
14
15 115
    public function __construct($Items, $type = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
16
    {
17 115
        $this->type = is_null($type) ? Order::class : $type;
18
19 115
        if ($Items instanceof Collection) {
20 115
            $Items = $Items->toArray();
21
        }
22 115
        parent::__construct($Items);
23
    }
24
25 27
    public function reduce(\Closure $func, $initial = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
26
    {
27 27
        return array_reduce($this->toArray(), $func, $initial);
28
    }
29
30
    // 明細種別ごとに返すメソッド作る
31 25
    public function getProductClasses()
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
32
    {
33 25
        return $this->filter(
34
            function (ItemInterface $ShipmentItem) {
35 19
                return $ShipmentItem->isProduct();
36 25
            });
37
    }
38
39 24
    public function getDeliveryFees()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41 24
        return $this->filter(
42
            function (ItemInterface $ShipmentItem) {
43 18
                return $ShipmentItem->isDeliveryFee();
44 24
            });
45
    }
46
47 25
    public function getCharges()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
48
    {
49 25
        return $this->filter(
50
            function (ItemInterface $ShipmentItem) {
51 19
                return $ShipmentItem->isCharge();
52 25
            });
53
    }
54
55 23
    public function getDiscounts()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
56
    {
57 23
        return $this->filter(
58
            function (ItemInterface $ShipmentItem) {
59 17
                return $ShipmentItem->isDiscount();
60 23
            });
61
    }
62
63
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productName" missing
Loading history...
64
     * 同名の明細が存在するかどうか.
65
     *
66
     * TODO 暫定対応. 本来は明細種別でチェックする.
67
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
68 View Code Duplication
    public function hasProductByName($productName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
97
    {
98
        return $this->type;
99
    }
100
101 115
    public function sort()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
102
    {
103 115
        $Items = $this->toArray();
104 115
        usort($Items, function (ItemInterface $a, ItemInterface $b) {
105 28
            if ($a->getOrderItemType() === $b->getOrderItemType()) {
106 11
                return ($a->getId() < $b->getId()) ? -1 : 1;
107 27
            } elseif ($a->isProduct()) {
108 27
                return -1;
109 14
            } elseif ($a->isDeliveryFee()) {
110 12
                if ($b->isProduct()) {
111
                    return 1;
112
                }
113
114 12
                return -1;
115 14
            } elseif ($a->isCharge()) {
116 14
                if ($b->isDeliveryFee() || $b->isProduct()) {
117 2
                    return 1;
118
                }
119
120 14
                return -1;
121 3
            } elseif ($a->isDiscount()) {
122 3
                if (!$b->isTax()) {
123 3
                    return 1;
124
                }
125
126
                return -1;
127
            } elseif ($a->isTax()) {
128
                return 1;
129
            }
130
131
            return 0;
132 115
        });
133
134 115
        return new self($Items);
135
    }
136
}
137