Completed
Push — support-coverage ( 9a8e61...2263e6 )
by Kentaro
34:05
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 80.95%

Importance

Changes 0
Metric Value
dl 19
loc 126
rs 10
c 0
b 0
f 0
ccs 51
cts 63
cp 0.8095
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 197
    public function __construct($Items, $type = null)
16
    {
17 197
        $this->type = is_null($type) ? Order::class : $type;
18
19 197
        if ($Items instanceof Collection) {
20 197
            $Items = $Items->toArray();
21
        }
22 197
        parent::__construct($Items);
23
    }
24
25 191
    public function reduce(\Closure $func, $initial = null)
26
    {
27 191
        return array_reduce($this->toArray(), $func, $initial);
28
    }
29
30
    // 明細種別ごとに返すメソッド作る
31 188
    public function getProductClasses()
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
32
    {
33 188
        return $this->filter(
34 188
            function (ItemInterface $OrderItem) {
35 184
                return $OrderItem->isProduct();
36 188
            });
37
    }
38
39 188
    public function getDeliveryFees()
40
    {
41 188
        return $this->filter(
42 188
            function (ItemInterface $OrderItem) {
43 184
                return $OrderItem->isDeliveryFee();
44 188
            });
45
    }
46
47 188
    public function getCharges()
48
    {
49 188
        return $this->filter(
50 188
            function (ItemInterface $OrderItem) {
51 184
                return $OrderItem->isCharge();
52 188
            });
53
    }
54
55 188
    public function getDiscounts()
56
    {
57 188
        return $this->filter(
58 188
            function (ItemInterface $OrderItem) {
59 184
                return $OrderItem->isDiscount();
60 188
            });
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
        $OrderItems = $this->filter(
71
            function (ItemInterface $OrderItem) use ($productName) {
72
                /* @var OrderItem $OrderItem */
73
                return $OrderItem->getProductName() == $productName;
74
            });
75
76
        return !$OrderItems->isEmpty();
77
    }
78
79
    /**
80
     * 指定した受注明細区分の明細が存在するかどうか.
81
     *
82
     * @param OrderItemType $OrderItemType 受注区分
83
     *
84
     * @return bool
85
     */
86 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 1
        $filteredItems = $this->filter(function (ItemInterface $OrderItem) use ($OrderItemType) {
89
            /* @var OrderItem $OrderItem */
90 1
            return $OrderItem->getOrderItemType() && $OrderItem->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 197
    public function sort()
102
    {
103 197
        $Items = $this->toArray();
104 197
        usort($Items, function (ItemInterface $a, ItemInterface $b) {
105 161
            if ($a->getOrderItemType() === $b->getOrderItemType()) {
106 12
                return ($a->getId() < $b->getId()) ? -1 : 1;
107 160
            } elseif ($a->isProduct()) {
108 160
                return -1;
109 130
            } elseif ($a->isDeliveryFee()) {
110 130
                if ($b->isProduct()) {
111 1
                    return 1;
112
                }
113
114 130
                return -1;
115 130
            } elseif ($a->isCharge()) {
116 130
                if ($b->isDeliveryFee() || $b->isProduct()) {
117 3
                    return 1;
118
                }
119
120 130
                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 197
        });
133
134 197
        return new self($Items);
135
    }
136
}
137