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 classes like OrderRepository 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 OrderRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class OrderRepository extends EntityRepository |
||
38 | { |
||
39 | protected $app; |
||
40 | |||
41 | 97 | public function setApplication($app) |
|
45 | |||
46 | 7 | public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status) |
|
66 | |||
67 | /** |
||
68 | * |
||
69 | * @param array $searchData |
||
70 | * @return QueryBuilder |
||
71 | */ |
||
72 | 19 | public function getQueryBuilderBySearchData($searchData) |
|
255 | |||
256 | |||
257 | /** |
||
258 | * |
||
259 | * @param array $searchData |
||
260 | * @return QueryBuilder |
||
261 | */ |
||
262 | 25 | public function getQueryBuilderBySearchDataForAdmin($searchData) |
|
263 | { |
||
264 | $qb = $this->createQueryBuilder('o'); |
||
265 | |||
266 | // order_id_start |
||
267 | 24 | View Code Duplication | if (isset($searchData['order_id_start']) && Str::isNotBlank($searchData['order_id_start'])) { |
268 | $qb |
||
269 | 1 | ->andWhere('o.id >= :order_id_start') |
|
270 | ->setParameter('order_id_start', $searchData['order_id_start']); |
||
271 | } |
||
272 | // multi |
||
273 | 22 | View Code Duplication | if (isset( $searchData['multi']) && Str::isNotBlank($searchData['multi'])) { |
274 | $multi = preg_match('/^\d+$/', $searchData['multi']) ? $searchData['multi'] : null; |
||
275 | $qb |
||
276 | ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR ' . |
||
277 | 3 | 'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti') |
|
278 | 3 | ->setParameter('multi', $multi) |
|
279 | ->setParameter('likemulti', '%' . $searchData['multi'] . '%'); |
||
280 | } |
||
281 | |||
282 | // order_id_end |
||
283 | 24 | View Code Duplication | if (isset($searchData['order_id_end']) && Str::isNotBlank($searchData['order_id_end'])) { |
284 | $qb |
||
285 | 1 | ->andWhere('o.id <= :order_id_end') |
|
286 | ->setParameter('order_id_end', $searchData['order_id_end']); |
||
287 | } |
||
288 | |||
289 | // status |
||
290 | 25 | $filterStatus = false; |
|
291 | View Code Duplication | if (!empty($searchData['status']) && $searchData['status']) { |
|
292 | 1 | $qb |
|
293 | ->andWhere('o.OrderStatus = :status') |
||
294 | ->setParameter('status', $searchData['status']); |
||
295 | $filterStatus = true; |
||
296 | 24 | } |
|
297 | if (!empty($searchData['multi_status']) && count($searchData['multi_status'])) { |
||
298 | 1 | $qb |
|
299 | ->andWhere($qb->expr()->in('o.OrderStatus', ':multi_status')) |
||
300 | ->setParameter('multi_status', $searchData['multi_status']->toArray()); |
||
301 | 24 | $filterStatus = true; |
|
302 | } |
||
303 | 1 | if (!$filterStatus) { |
|
304 | // 購入処理中は検索対象から除外 |
||
305 | $OrderStatuses = $this->getEntityManager() |
||
306 | ->getRepository('Eccube\Entity\Master\OrderStatus') |
||
307 | ->findNotContainsBy(array('id' => $this->app['config']['order_processing'])); |
||
308 | 24 | $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status')) |
|
309 | ->setParameter('status', $OrderStatuses); |
||
310 | 1 | } |
|
311 | |||
312 | // name |
||
313 | View Code Duplication | if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) { |
|
314 | $qb |
||
315 | 24 | ->andWhere('CONCAT(o.name01, o.name02) LIKE :name') |
|
316 | ->setParameter('name', '%' . $searchData['name'] . '%'); |
||
317 | 1 | } |
|
318 | |||
319 | // kana |
||
320 | View Code Duplication | if (isset($searchData['kana']) && Str::isNotBlank($searchData['kana'])) { |
|
321 | $qb |
||
322 | 24 | ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana') |
|
323 | ->setParameter('kana', '%' . $searchData['kana'] . '%'); |
||
324 | 1 | } |
|
325 | |||
326 | |||
327 | View Code Duplication | if (isset($searchData['email']) && Str::isNotBlank($searchData['email'])) { |
|
328 | $qb |
||
329 | 23 | ->andWhere('o.email like :email') |
|
330 | ->setParameter('email', '%' . $searchData['email'] . '%'); |
||
331 | } |
||
332 | |||
333 | // tel |
||
334 | View Code Duplication | if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) { |
|
335 | $qb |
||
336 | 24 | ->andWhere('CONCAT(o.tel01, o.tel02, o.tel03) LIKE :tel') |
|
337 | ->setParameter('tel', '%' . $searchData['tel'] . '%'); |
||
338 | } |
||
339 | |||
340 | // sex |
||
341 | if (!empty($searchData['sex']) && count($searchData['sex']) > 0) { |
||
342 | $qb |
||
343 | ->andWhere($qb->expr()->in('o.Sex', ':sex')) |
||
344 | ->setParameter('sex', $searchData['sex']->toArray()); |
||
345 | } |
||
346 | |||
347 | // payment |
||
348 | 25 | View Code Duplication | if (!empty($searchData['payment']) && count($searchData['payment'])) { |
349 | $payments = array(); |
||
350 | foreach ($searchData['payment'] as $payment) { |
||
351 | $payments[] = $payment->getId(); |
||
352 | 1 | } |
|
353 | $qb |
||
354 | ->leftJoin('o.Payment', 'p') |
||
355 | 25 | ->andWhere($qb->expr()->in('p.id', ':payments')) |
|
356 | 1 | ->setParameter('payments', $payments); |
|
357 | } |
||
358 | 1 | ||
359 | // oreder_date |
||
360 | View Code Duplication | if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) { |
|
361 | 1 | $date = $searchData['order_date_start'] |
|
362 | ->format('Y-m-d H:i:s'); |
||
363 | $qb |
||
364 | ->andWhere('o.order_date >= :order_date_start') |
||
365 | ->setParameter('order_date_start', $date); |
||
366 | 25 | } |
|
367 | View Code Duplication | if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) { |
|
368 | $date = clone $searchData['order_date_end']; |
||
369 | $date = $date |
||
370 | 1 | ->modify('+1 days') |
|
371 | ->format('Y-m-d H:i:s'); |
||
372 | $qb |
||
373 | 25 | ->andWhere('o.order_date < :order_date_end') |
|
374 | 1 | ->setParameter('order_date_end', $date); |
|
375 | } |
||
376 | 1 | ||
377 | // payment_date |
||
378 | View Code Duplication | if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) { |
|
379 | 1 | $date = $searchData['payment_date_start'] |
|
380 | ->format('Y-m-d H:i:s'); |
||
381 | $qb |
||
382 | ->andWhere('o.payment_date >= :payment_date_start') |
||
383 | ->setParameter('payment_date_start', $date); |
||
384 | 25 | } |
|
385 | View Code Duplication | if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) { |
|
386 | $date = clone $searchData['payment_date_end']; |
||
387 | $date = $date |
||
388 | 1 | ->modify('+1 days') |
|
389 | ->format('Y-m-d H:i:s'); |
||
390 | $qb |
||
391 | 25 | ->andWhere('o.payment_date < :payment_date_end') |
|
392 | 1 | ->setParameter('payment_date_end', $date); |
|
393 | } |
||
394 | 1 | ||
395 | // commit_date |
||
396 | View Code Duplication | if (!empty($searchData['commit_date_start']) && $searchData['commit_date_start']) { |
|
397 | 1 | $date = $searchData['commit_date_start'] |
|
398 | ->format('Y-m-d H:i:s'); |
||
399 | $qb |
||
400 | ->andWhere('o.commit_date >= :commit_date_start') |
||
401 | ->setParameter('commit_date_start', $date); |
||
402 | } |
||
403 | 25 | View Code Duplication | if (!empty($searchData['commit_date_end']) && $searchData['commit_date_end']) { |
404 | $date = clone $searchData['commit_date_end']; |
||
405 | $date = $date |
||
406 | ->modify('+1 days') |
||
407 | 1 | ->format('Y-m-d H:i:s'); |
|
408 | $qb |
||
409 | ->andWhere('o.commit_date < :commit_date_end') |
||
410 | 25 | ->setParameter('commit_date_end', $date); |
|
411 | 1 | } |
|
412 | |||
413 | 1 | ||
414 | // update_date |
||
415 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
|
416 | 1 | $date = $searchData['update_date_start'] |
|
417 | ->format('Y-m-d H:i:s'); |
||
418 | $qb |
||
419 | ->andWhere('o.update_date >= :update_date_start') |
||
420 | ->setParameter('update_date_start', $date); |
||
421 | 24 | } |
|
422 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
|
423 | 1 | $date = clone $searchData['update_date_end']; |
|
424 | $date = $date |
||
425 | ->modify('+1 days') |
||
426 | 24 | ->format('Y-m-d H:i:s'); |
|
427 | $qb |
||
428 | 1 | ->andWhere('o.update_date < :update_date_end') |
|
429 | ->setParameter('update_date_end', $date); |
||
430 | } |
||
431 | |||
432 | // payment_total |
||
433 | 24 | View Code Duplication | if (isset($searchData['payment_total_start']) && Str::isNotBlank($searchData['payment_total_start'])) { |
434 | $qb |
||
435 | 1 | ->andWhere('o.payment_total >= :payment_total_start') |
|
436 | 1 | ->setParameter('payment_total_start', $searchData['payment_total_start']); |
|
437 | } |
||
438 | View Code Duplication | if (isset($searchData['payment_total_end']) && Str::isNotBlank($searchData['payment_total_end'])) { |
|
439 | $qb |
||
440 | ->andWhere('o.payment_total <= :payment_total_end') |
||
441 | ->setParameter('payment_total_end', $searchData['payment_total_end']); |
||
442 | } |
||
443 | |||
444 | 25 | // buy_product_name |
|
445 | View Code Duplication | if (isset($searchData['buy_product_name']) && Str::isNotBlank($searchData['buy_product_name'])) { |
|
446 | $qb |
||
447 | ->leftJoin('o.OrderDetails', 'od') |
||
448 | ->andWhere('od.product_name LIKE :buy_product_name') |
||
449 | ->setParameter('buy_product_name', '%' . $searchData['buy_product_name'] . '%'); |
||
450 | } |
||
451 | |||
452 | 2 | // Order By |
|
453 | $qb->orderBy('o.update_date', 'DESC'); |
||
454 | 2 | $qb->addorderBy('o.id', 'DESC'); |
|
455 | 2 | ||
456 | return $qb; |
||
457 | } |
||
458 | |||
459 | |||
460 | /** |
||
461 | 2 | * @param \Eccube\Entity\Customer $Customer |
|
462 | 2 | * @return QueryBuilder |
|
463 | */ |
||
464 | public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer) |
||
475 | 1 | ||
476 | /** |
||
477 | * 新規受付一覧の取得 |
||
478 | * |
||
479 | 1 | * @return \Eccube\Entity\Order[] |
|
480 | */ |
||
481 | public function getNew() |
||
494 | 4 | ||
495 | 4 | /** |
|
496 | 4 | * 会員の合計購入金額を取得、回数を取得 |
|
497 | 4 | * |
|
498 | 4 | * @param \Eccube\Entity\Customer $Customer |
|
499 | 4 | * @param array $OrderStatuses |
|
500 | 4 | * @return QueryBuilder |
|
501 | */ |
||
502 | public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses) |
||
516 | } |
||
517 |