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

ItemCollection::sort()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 13.3014

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 1
nop 0
dl 0
loc 35
ccs 19
cts 24
cp 0.7917
crap 13.3014
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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