Failed Conditions
Pull Request — experimental/3.1 (#2449)
by Kiyotaka
52:40
created

ShippingRepository   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 222
Duplicated Lines 65.32 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 9.17%

Importance

Changes 0
Metric Value
dl 145
loc 222
ccs 10
cts 109
cp 0.0917
rs 7.9487
c 0
b 0
f 0
wmc 52
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
F getQueryBuilderBySearchDataForAdmin() 145 192 51
A findShippingsProduct() 0 14 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ShippingRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ShippingRepository, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Repository;
26
27
use Eccube\Util\Str;
28
29
/**
30
 * ShippingRepository
31
 *
32
 * This class was generated by the Doctrine ORM. Add your own custom
33
 * repository methods below.
34
 */
35
class ShippingRepository extends AbstractRepository
36
{
37
    /**
38
     *
39
     * @param  array        $searchData
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
40
     * @return QueryBuilder
41
     */
42
    public function getQueryBuilderBySearchDataForAdmin($searchData)
43
    {
44
        $qb = $this->createQueryBuilder('s');
45
46
        $qb->leftJoin('s.ShipmentItems', 'si')
47
            ->leftJoin('si.Order', 'o');
48
        // order_id_start
49 View Code Duplication
        if (isset($searchData['shipping_id_start']) && Str::isNotBlank($searchData['shipping_id_start'])) {
50
            $qb
51
                ->andWhere('s.id >= :shipping_id_start')
52
                ->setParameter('shipping_id_start', $searchData['shipping_id_start']);
53
        }
54
        // multi
55 View Code Duplication
        if (isset( $searchData['multi']) && Str::isNotBlank($searchData['multi'])) {
56
            $multi = preg_match('/^\d+$/', $searchData['multi']) ? $searchData['multi'] : null;
57
            $qb
58
                ->andWhere('s.id = :multi OR s.name01 LIKE :likemulti OR s.name02 LIKE :likemulti OR ' .
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
59
                           's.kana01 LIKE :likemulti OR s.kana02 LIKE :likemulti OR s.company_name LIKE :likemulti')
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 27.
Loading history...
60
                ->setParameter('multi', $multi)
61
                ->setParameter('likemulti', '%' . $searchData['multi'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
62
        }
63
64
        // shipping_id_end
65 View Code Duplication
        if (isset($searchData['shipping_id_end']) && Str::isNotBlank($searchData['shipping_id_end'])) {
66
            $qb
67
                ->andWhere('s.id <= :shipping_id_end')
68
                ->setParameter('shipping_id_end', $searchData['shipping_id_end']);
69
        }
70
71
        // order_id
72 View Code Duplication
        if (isset($searchData['order_id']) && Str::isNotBlank($searchData['order_id'])) {
73
            $qb
74
                ->andWhere('o.id = :order_id')
75
                ->setParameter('order_id', $searchData['order_id']);
76
        }
77
78
        // status
79 View Code Duplication
        if (!empty($searchData['status']) && $searchData['status']) {
80
            $qb
81
                ->andWhere('o.OrderStatus = :status')
82
                ->setParameter('status', $searchData['status']);
83
        }
84 View Code Duplication
        if (!empty($searchData['multi_status']) && count($searchData['multi_status'])) {
85
            $qb
86
                ->andWhere($qb->expr()->in('o.OrderStatus', ':multi_status'))
87
                ->setParameter('multi_status', $searchData['multi_status']->toArray());
88
            $filterStatus = true;
0 ignored issues
show
Unused Code introduced by
$filterStatus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
        }
90
        // name
91 View Code Duplication
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
92
            $qb
93
                ->andWhere('CONCAT(s.name01, s.name02) LIKE :name')
94
                ->setParameter('name', '%' . $searchData['name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
95
        }
96
97
        // kana
98 View Code Duplication
        if (isset($searchData['kana']) && Str::isNotBlank($searchData['kana'])) {
99
            $qb
100
                ->andWhere('CONCAT(s.kana01, s.kana02) LIKE :kana')
101
                ->setParameter('kana', '%' . $searchData['kana'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
102
        }
103
104
        // order_name
105 View Code Duplication
        if (isset($searchData['order_name']) && Str::isNotBlank($searchData['order_name'])) {
106
            $qb
107
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :order_name')
108
                ->setParameter('order_name', '%' . $searchData['order_name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
109
        }
110
111
        // order_kana
112 View Code Duplication
        if (isset($searchData['order_kana']) && Str::isNotBlank($searchData['order_kana'])) {
113
            $qb
114
                ->andWhere('CONCAT(o.kana01, s.kana02) LIKE :order_kana')
115
                ->setParameter('kana', '%' . $searchData['order_kana'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
116
        }
117
118
        // order_email
119 View Code Duplication
        if (isset($searchData['email']) && Str::isNotBlank($searchData['email'])) {
120
            $qb
121
                ->andWhere('o.email like :email')
122
                ->setParameter('email', '%' . $searchData['email'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
123
        }
124
125
        // tel
126 View Code Duplication
        if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) {
127
            $qb
128
                ->andWhere('CONCAT(s.tel01, s.tel02, s.tel03) LIKE :tel')
129
                ->setParameter('tel', '%' . $searchData['tel'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
130
        }
131
132
        // payment
133 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
134
            $payments = array();
135
            foreach ($searchData['payment'] as $payment) {
136
                $payments[] = $payment->getId();
137
            }
138
            $qb
139
                ->leftJoin('o.Payment', 'p')
140
                ->andWhere($qb->expr()->in('p.id', ':payments'))
141
                ->setParameter('payments', $payments);
142
        }
143
144
        // oreder_date
145 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
146
            $date = $searchData['order_date_start'];
147
            $qb
148
                ->andWhere('o.order_date >= :order_date_start')
149
                ->setParameter('order_date_start', $date);
150
        }
151 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
152
            $date = clone $searchData['order_date_end'];
153
            $date = $date
154
                ->modify('+1 days');
155
            $qb
156
                ->andWhere('o.order_date < :order_date_end')
157
                ->setParameter('order_date_end', $date);
158
        }
159
160
        // shipping_delivery_date
161 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
162
            $date = $searchData['shipping_delivery_date_start'];
163
            $qb
164
                ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
165
                ->setParameter('shipping_delivery_date_start', $date);
166
        }
167 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
168
            $date = clone $searchData['shipping_delivery_date_end'];
169
            $date = $date
170
                ->modify('+1 days');
171
            $qb
172
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
173
                ->setParameter('shipping_delivery_date_end', $date);
174
        }
175
176
        // commit_date
177 View Code Duplication
        if (!empty($searchData['commit_date_start']) && $searchData['commit_date_start']) {
178
            $date = $searchData['commit_date_start'];
179
            $qb
180
                ->andWhere('s.commit_date >= :commit_date_start')
181
                ->setParameter('commit_date_start', $date);
182
        }
183 View Code Duplication
        if (!empty($searchData['commit_date_end']) && $searchData['commit_date_end']) {
184
            $date = clone $searchData['commit_date_end'];
185
            $date = $date
186
                ->modify('+1 days');
187
            $qb
188
                ->andWhere('s.commit_date < :commit_date_end')
189
                ->setParameter('commit_date_end', $date);
190
        }
191
192
193
        // update_date
194 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
195
            $date = $searchData['update_date_start'];
196
            $qb
197
                ->andWhere('s.update_date >= :update_date_start')
198
                ->setParameter('update_date_start', $date);
199
        }
200 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
201
            $date = clone $searchData['update_date_end'];
202
            $date = $date
203
                ->modify('+1 days');
204
            $qb
205
                ->andWhere('s.update_date < :update_date_end')
206
                ->setParameter('update_date_end', $date);
207
        }
208
209
        // payment_total
210 View Code Duplication
        if (isset($searchData['payment_total_start']) && Str::isNotBlank($searchData['payment_total_start'])) {
211
            $qb
212
                ->andWhere('o.payment_total >= :payment_total_start')
213
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
214
        }
215 View Code Duplication
        if (isset($searchData['payment_total_end']) && Str::isNotBlank($searchData['payment_total_end'])) {
216
            $qb
217
                ->andWhere('o.payment_total <= :payment_total_end')
218
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
219
        }
220
221
        // buy_product_name
222 View Code Duplication
        if (isset($searchData['buy_product_name']) && Str::isNotBlank($searchData['buy_product_name'])) {
223
            $qb
224
                ->andWhere('si.product_name LIKE :buy_product_name')
225
                ->setParameter('buy_product_name', '%' . $searchData['buy_product_name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
226
        }
227
228
        // Order By
229
        $qb->orderBy('s.update_date', 'DESC');
230
        $qb->addorderBy('s.id', 'DESC');
231
232
        return $qb;
233
    }
234
235
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Order" missing
Loading history...
introduced by
Doc comment for parameter "$productClass" missing
Loading history...
236
     * 同一商品のお届け先情報を取得
237
     *
238
     * @param $Order
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
239
     * @return array
240
     */
241 1
    public function findShippingsProduct($Order, $productClass)
242
    {
243 1
        $shippings = $this->createQueryBuilder('s')
244 1
            ->innerJoin('Eccube\Entity\ShipmentItem', 'si', 'WITH', 'si.Shipping = s.id')
245 1
            ->where('si.Order = (:order)')
246 1
            ->andWhere('si.ProductClass = (:productClass)')
247 1
            ->setParameter('order', $Order)
248 1
            ->setParameter('productClass', $productClass)
249 1
            ->getQuery()
250 1
            ->getResult();
251
252 1
        return $shippings;
253
254
    }
255
256
}
257