Conditions | 58 |
Paths | > 20000 |
Total Lines | 233 |
Lines | 142 |
Ratio | 60.94 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
78 | public function getQueryBuilderBySearchDataForAdmin($searchData) |
||
79 | { |
||
80 | $qb = $this->createQueryBuilder('o') |
||
81 | ->select('o, s') |
||
82 | ->addSelect('oi', 'pref') |
||
83 | ->leftJoin('o.OrderItems', 'oi') |
||
84 | ->leftJoin('o.Pref', 'pref') |
||
85 | ->innerJoin('o.Shippings', 's'); |
||
86 | |||
87 | // order_id_start |
||
88 | View Code Duplication | if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) { |
|
|
|||
89 | $qb |
||
90 | ->andWhere('o.id = :order_id') |
||
91 | ->setParameter('order_id', $searchData['order_id']); |
||
92 | } |
||
93 | |||
94 | // order_no |
||
95 | View Code Duplication | if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) { |
|
96 | $qb |
||
97 | ->andWhere('o.order_no = :order_no') |
||
98 | ->setParameter('order_no', $searchData['order_no']); |
||
99 | } |
||
100 | |||
101 | // order_id_start |
||
102 | View Code Duplication | if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) { |
|
103 | $qb |
||
104 | ->andWhere('o.id >= :order_id_start') |
||
105 | ->setParameter('order_id_start', $searchData['order_id_start']); |
||
106 | } |
||
107 | // multi |
||
108 | View Code Duplication | if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) { |
|
109 | $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null; |
||
110 | $qb |
||
111 | ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR '. |
||
112 | 'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti OR '. |
||
113 | 'o.order_no LIKE :likemulti') |
||
114 | ->setParameter('multi', $multi) |
||
115 | ->setParameter('likemulti', '%'.$searchData['multi'].'%'); |
||
116 | } |
||
117 | |||
118 | // order_id_end |
||
119 | View Code Duplication | if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) { |
|
120 | $qb |
||
121 | ->andWhere('o.id <= :order_id_end') |
||
122 | ->setParameter('order_id_end', $searchData['order_id_end']); |
||
123 | } |
||
124 | |||
125 | // status |
||
126 | $filterStatus = false; |
||
127 | View Code Duplication | if (!empty($searchData['status']) && count($searchData['status'])) { |
|
128 | $qb |
||
129 | ->andWhere($qb->expr()->in('o.OrderStatus', ':status')) |
||
130 | ->setParameter('status', $searchData['status']); |
||
131 | $filterStatus = true; |
||
132 | } |
||
133 | |||
134 | if (!$filterStatus) { |
||
135 | // 購入処理中は検索対象から除外 |
||
136 | $OrderStatuses = $this->getEntityManager() |
||
137 | ->getRepository('Eccube\Entity\Master\OrderStatus') |
||
138 | ->findNotContainsBy(['id' => OrderStatus::PROCESSING]); |
||
139 | $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status')) |
||
140 | ->setParameter('status', $OrderStatuses); |
||
141 | } |
||
142 | |||
143 | // company_name |
||
144 | View Code Duplication | if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) { |
|
145 | $qb |
||
146 | ->andWhere('o.company_name LIKE :company_name') |
||
147 | ->setParameter('company_name', '%'.$searchData['company_name'].'%'); |
||
148 | } |
||
149 | |||
150 | // name |
||
151 | View Code Duplication | if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) { |
|
152 | $qb |
||
153 | ->andWhere('CONCAT(o.name01, o.name02) LIKE :name') |
||
154 | ->setParameter('name', '%'.$searchData['name'].'%'); |
||
155 | } |
||
156 | |||
157 | // kana |
||
158 | View Code Duplication | if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) { |
|
159 | $qb |
||
160 | ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana') |
||
161 | ->setParameter('kana', '%'.$searchData['kana'].'%'); |
||
162 | } |
||
163 | |||
164 | |||
165 | View Code Duplication | if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) { |
|
166 | $qb |
||
167 | ->andWhere('o.email like :email') |
||
168 | ->setParameter('email', '%'.$searchData['email'].'%'); |
||
169 | } |
||
170 | |||
171 | // tel |
||
172 | View Code Duplication | if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) { |
|
173 | $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']); |
||
174 | $qb |
||
175 | ->andWhere('o.phone_number LIKE :phone_number') |
||
176 | ->setParameter('phone_number', '%'.$tel.'%'); |
||
177 | } |
||
178 | |||
179 | // sex |
||
180 | if (!empty($searchData['sex']) && count($searchData['sex']) > 0) { |
||
181 | $qb |
||
182 | ->andWhere($qb->expr()->in('o.Sex', ':sex')) |
||
183 | ->setParameter('sex', $searchData['sex']->toArray()); |
||
184 | } |
||
185 | |||
186 | // payment |
||
187 | View Code Duplication | if (!empty($searchData['payment']) && count($searchData['payment'])) { |
|
188 | $payments = []; |
||
189 | foreach ($searchData['payment'] as $payment) { |
||
190 | $payments[] = $payment->getId(); |
||
191 | } |
||
192 | $qb |
||
193 | ->leftJoin('o.Payment', 'p') |
||
194 | ->andWhere($qb->expr()->in('p.id', ':payments')) |
||
195 | ->setParameter('payments', $payments); |
||
196 | } |
||
197 | |||
198 | // oreder_date |
||
199 | View Code Duplication | if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) { |
|
200 | $date = $searchData['order_date_start']; |
||
201 | $qb |
||
202 | ->andWhere('o.order_date >= :order_date_start') |
||
203 | ->setParameter('order_date_start', $date); |
||
204 | } |
||
205 | View Code Duplication | if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) { |
|
206 | $date = clone $searchData['order_date_end']; |
||
207 | $date = $date |
||
208 | ->modify('+1 days'); |
||
209 | $qb |
||
210 | ->andWhere('o.order_date < :order_date_end') |
||
211 | ->setParameter('order_date_end', $date); |
||
212 | } |
||
213 | |||
214 | // payment_date |
||
215 | View Code Duplication | if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) { |
|
216 | $date = $searchData['payment_date_start']; |
||
217 | $qb |
||
218 | ->andWhere('o.payment_date >= :payment_date_start') |
||
219 | ->setParameter('payment_date_start', $date); |
||
220 | } |
||
221 | View Code Duplication | if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) { |
|
222 | $date = clone $searchData['payment_date_end']; |
||
223 | $date = $date |
||
224 | ->modify('+1 days'); |
||
225 | $qb |
||
226 | ->andWhere('o.payment_date < :payment_date_end') |
||
227 | ->setParameter('payment_date_end', $date); |
||
228 | } |
||
229 | |||
230 | // update_date |
||
231 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
|
232 | $date = $searchData['update_date_start']; |
||
233 | $qb |
||
234 | ->andWhere('o.update_date >= :update_date_start') |
||
235 | ->setParameter('update_date_start', $date); |
||
236 | } |
||
237 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
|
238 | $date = clone $searchData['update_date_end']; |
||
239 | $date = $date |
||
240 | ->modify('+1 days'); |
||
241 | $qb |
||
242 | ->andWhere('o.update_date < :update_date_end') |
||
243 | ->setParameter('update_date_end', $date); |
||
244 | } |
||
245 | |||
246 | // payment_total |
||
247 | View Code Duplication | if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) { |
|
248 | $qb |
||
249 | ->andWhere('o.payment_total >= :payment_total_start') |
||
250 | ->setParameter('payment_total_start', $searchData['payment_total_start']); |
||
251 | } |
||
252 | View Code Duplication | if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) { |
|
253 | $qb |
||
254 | ->andWhere('o.payment_total <= :payment_total_end') |
||
255 | ->setParameter('payment_total_end', $searchData['payment_total_end']); |
||
256 | } |
||
257 | |||
258 | // buy_product_name |
||
259 | View Code Duplication | if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) { |
|
260 | $qb |
||
261 | ->andWhere('oi.product_name LIKE :buy_product_name') |
||
262 | ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%'); |
||
263 | } |
||
264 | |||
265 | // 発送メール送信/未送信. |
||
266 | if (isset($searchData['shipping_mail']) && $count = count($searchData['shipping_mail'])) { |
||
267 | // 送信済/未送信両方にチェックされている場合は検索条件に追加しない |
||
268 | if ($count < 2) { |
||
269 | $checked = current($searchData['shipping_mail']); |
||
270 | if ($checked == Shipping::SHIPPING_MAIL_UNSENT) { |
||
271 | // 未送信 |
||
272 | $qb |
||
273 | ->andWhere('s.mail_send_date IS NULL'); |
||
274 | } elseif ($checked == Shipping::SHIPPING_MAIL_SENT) { |
||
275 | // 送信 |
||
276 | $qb |
||
277 | ->andWhere('s.mail_send_date IS NOT NULL'); |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | // 送り状番号. |
||
283 | if (!empty($searchData['tracking_number'])) { |
||
284 | $qb |
||
285 | ->andWhere('s.tracking_number = :tracking_number') |
||
286 | ->setParameter('tracking_number', $searchData['tracking_number']); |
||
287 | } |
||
288 | |||
289 | // お届け予定日(Shipping.delivery_date) |
||
290 | View Code Duplication | if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) { |
|
291 | $date = $searchData['shipping_delivery_date_start']; |
||
292 | $qb |
||
293 | ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start') |
||
294 | ->setParameter('shipping_delivery_date_start', $date); |
||
295 | } |
||
296 | View Code Duplication | if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) { |
|
297 | $date = clone $searchData['shipping_delivery_date_end']; |
||
298 | $date = $date |
||
299 | ->modify('+1 days'); |
||
300 | $qb |
||
301 | ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end') |
||
302 | ->setParameter('shipping_delivery_date_end', $date); |
||
303 | } |
||
304 | |||
305 | // Order By |
||
306 | $qb->orderBy('o.update_date', 'DESC'); |
||
307 | $qb->addorderBy('o.id', 'DESC'); |
||
308 | |||
309 | return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData); |
||
310 | } |
||
311 | |||
386 |
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.