Failed Conditions
Pull Request — experimental/sf (#3416)
by
unknown
329:25 queued 320:39
created

ItemCollection::sort()   C

Complexity

Conditions 15
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 15.6565

Importance

Changes 0
Metric Value
cc 15
nc 1
nop 0
dl 0
loc 43
ccs 24
cts 28
cp 0.8571
crap 15.6565
rs 5.9166
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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\PurchaseFlow;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Eccube\Entity\ItemInterface;
19
use Eccube\Entity\Master\OrderItemType;
20
use Eccube\Entity\Order;
21
22
class ItemCollection extends ArrayCollection
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
23
{
24
    protected $type;
25
26 329
    public function __construct($Items, $type = null)
27
    {
28 329
        $this->type = is_null($type) ? Order::class : $type;
29
30 329
        if ($Items instanceof Collection) {
31 329
            $Items = $Items->toArray();
32
        }
33 329
        parent::__construct($Items);
34
    }
35
36 299
    public function reduce(\Closure $func, $initial = null)
37
    {
38 299
        return array_reduce($this->toArray(), $func, $initial);
39
    }
40
41
    // 明細種別ごとに返すメソッド作る
42 294
    public function getProductClasses()
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
43
    {
44 294
        return $this->filter(
45 294
            function (ItemInterface $OrderItem) {
46 290
                return $OrderItem->isProduct();
47 294
            });
48
    }
49
50 294
    public function getDeliveryFees()
51
    {
52 294
        return $this->filter(
53 294
            function (ItemInterface $OrderItem) {
54 290
                return $OrderItem->isDeliveryFee();
55 294
            });
56
    }
57
58 294
    public function getCharges()
59
    {
60 294
        return $this->filter(
61 294
            function (ItemInterface $OrderItem) {
62 290
                return $OrderItem->isCharge();
63 294
            });
64
    }
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productName" missing
Loading history...
75
     * 同名の明細が存在するかどうか.
76
     *
77
     * TODO 暫定対応. 本来は明細種別でチェックする.
78
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
79 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...
80
    {
81
        $OrderItems = $this->filter(
82
            function (ItemInterface $OrderItem) use ($productName) {
83
                /* @var OrderItem $OrderItem */
84
                return $OrderItem->getProductName() == $productName;
85
            });
86
87
        return !$OrderItems->isEmpty();
88
    }
89
90
    /**
91
     * 指定した受注明細区分の明細が存在するかどうか.
92
     *
93
     * @param OrderItemType $OrderItemType 受注区分
94
     *
95
     * @return bool
96
     */
97 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...
98
    {
99 1
        $filteredItems = $this->filter(function (ItemInterface $OrderItem) use ($OrderItemType) {
100
            /* @var OrderItem $OrderItem */
101 1
            return $OrderItem->getOrderItemType() && $OrderItem->getOrderItemType()->getId() == $OrderItemType->getId();
102 1
        });
103
104 1
        return !$filteredItems->isEmpty();
105
    }
106
107
    public function getType()
108
    {
109
        return $this->type;
110
    }
111
112 329
    public function sort()
113
    {
114 329
        $Items = $this->toArray();
115 329
        usort($Items, function (ItemInterface $a, ItemInterface $b) {
116 237
            if ($a->getOrderItemType() === $b->getOrderItemType()) {
117 39
                return ($a->getId() < $b->getId()) ? -1 : 1;
118 229
            } elseif ($a->isProduct()) {
119 209
                return -1;
120 179
            } elseif ($a->isDeliveryFee()) {
121 163
                if ($b->isProduct()) {
122 11
                    return 1;
123
                }
124
125 153
                return -1;
126 169
            } elseif ($a->isCharge()) {
127 153
                if ($b->isDeliveryFee() || $b->isProduct()) {
128 2
                    return 1;
129
                }
130
131 153
                return -1;
132 19
            } elseif ($a->isDiscount() || $a->isPoint()) {
133 3
                if ($b->isDiscount()) {
134
                    return -1;
135
                }
136
137 3
                if ($b->isPoint()) {
138
                    return 1;
139
                }
140
141 3
                if (!$b->isTax()) {
142 3
                    return 1;
143
                }
144
145
                return -1;
146 16
            } elseif ($a->isTax()) {
147
                return 1;
148
            }
149
150 16
            return 0;
151 329
        });
152
153 329
        return new self($Items);
154
    }
155
}
156