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:
| 1 | <?php |
||
| 34 | class OrderController extends AbstractController |
||
|
|
|||
| 35 | { |
||
| 36 | |||
| 37 | 4 | public function index(Application $app, Request $request, $page_no = null) |
|
| 38 | { |
||
| 39 | |||
| 40 | $session = $request->getSession(); |
||
| 41 | |||
| 42 | $searchForm = $app['form.factory'] |
||
| 43 | ->createBuilder('admin_search_order') |
||
| 44 | ->getForm(); |
||
| 45 | |||
| 46 | 4 | $pagination = array(); |
|
| 47 | |||
| 48 | $disps = $app['eccube.repository.master.disp']->findAll(); |
||
| 49 | $pageMaxis = $app['eccube.repository.master.page_max']->findAll(); |
||
| 50 | $page_count = $app['config']['default_page_count']; |
||
| 51 | 4 | $page_status = null; |
|
| 52 | 4 | $active = false; |
|
| 53 | |||
| 54 | if ('POST' === $request->getMethod()) { |
||
| 55 | |||
| 56 | $searchForm->handleRequest($request); |
||
| 57 | |||
| 58 | View Code Duplication | if ($searchForm->isValid()) { |
|
| 59 | $searchData = $searchForm->getData(); |
||
| 60 | |||
| 61 | // paginator |
||
| 62 | $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData); |
||
| 63 | 4 | $page_no = 1; |
|
| 64 | $pagination = $app['paginator']()->paginate( |
||
| 65 | $qb, |
||
| 66 | $page_no, |
||
| 67 | $page_count |
||
| 68 | ); |
||
| 69 | |||
| 70 | // sessionのデータ保持 |
||
| 71 | $session->set('eccube.admin.order.search', $searchData); |
||
| 72 | } |
||
| 73 | } else { |
||
| 74 | if (is_null($page_no)) { |
||
| 75 | // sessionを削除 |
||
| 76 | $session->remove('eccube.admin.order.search'); |
||
| 77 | } else { |
||
| 78 | // pagingなどの処理 |
||
| 79 | $searchData = $session->get('eccube.admin.order.search'); |
||
| 80 | if (!is_null($searchData)) { |
||
| 81 | |||
| 82 | // 公開ステータス |
||
| 83 | $status = $request->get('status'); |
||
| 84 | 1 | if (!empty($status)) { |
|
| 85 | if ($status != $app['config']['admin_product_stock_status']) { |
||
| 86 | $searchData['status']->clear(); |
||
| 87 | $searchData['status']->add($status); |
||
| 88 | } else { |
||
| 89 | $searchData['stock_status'] = $app['config']['disabled']; |
||
| 90 | } |
||
| 91 | $page_status = $status; |
||
| 92 | } |
||
| 93 | // 表示件数 |
||
| 94 | $pcount = $request->get('page_count'); |
||
| 95 | |||
| 96 | 1 | $page_count = empty($pcount) ? $page_count : $pcount; |
|
| 97 | |||
| 98 | $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData); |
||
| 99 | $pagination = $app['paginator']()->paginate( |
||
| 100 | $qb, |
||
| 101 | $page_no, |
||
| 102 | $page_count |
||
| 103 | ); |
||
| 104 | |||
| 105 | // セッションから検索条件を復元 |
||
| 106 | 1 | if (!empty($searchData['status'])) { |
|
| 107 | $searchData['status'] = $app['eccube.repository.master.order_status']->find($searchData['status']); |
||
| 108 | } |
||
| 109 | View Code Duplication | if (count($searchData['sex']) > 0) { |
|
| 110 | 1 | $sex_ids = array(); |
|
| 111 | 1 | foreach ($searchData['sex'] as $Sex) { |
|
| 112 | $sex_ids[] = $Sex->getId(); |
||
| 113 | } |
||
| 114 | $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids)); |
||
| 115 | } |
||
| 116 | View Code Duplication | if (count($searchData['payment']) > 0) { |
|
| 117 | 1 | $payment_ids = array(); |
|
| 118 | 1 | foreach ($searchData['payment'] as $Payment) { |
|
| 119 | $payment_ids[] = $Payment->getId(); |
||
| 120 | } |
||
| 121 | $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids)); |
||
| 122 | } |
||
| 123 | $searchForm->setData($searchData); |
||
| 124 | } |
||
| 125 | 1 | } |
|
| 126 | 4 | } |
|
| 127 | |||
| 128 | 4 | return $app->render('Order/index.twig', array( |
|
| 129 | 4 | 'searchForm' => $searchForm->createView(), |
|
| 130 | 'pagination' => $pagination, |
||
| 131 | 'disps' => $disps, |
||
| 132 | 'pageMaxis' => $pageMaxis, |
||
| 133 | 'page_no' => $page_no, |
||
| 134 | 'page_status' => $page_status, |
||
| 135 | 'page_count' => $page_count, |
||
| 136 | 'active' => $active, |
||
| 137 | )); |
||
| 138 | |||
| 139 | 4 | } |
|
| 140 | |||
| 141 | 1 | public function delete(Application $app, $id) |
|
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * 受注CSVの出力. |
||
| 171 | * |
||
| 172 | * @param Application $app |
||
| 173 | * @param Request $request |
||
| 174 | * @return StreamedResponse |
||
| 175 | */ |
||
| 176 | 1 | public function exportOrder(Application $app, Request $request) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * 配送CSVの出力. |
||
| 241 | * |
||
| 242 | * @param Application $app |
||
| 243 | * @param Request $request |
||
| 244 | * @return StreamedResponse |
||
| 245 | */ |
||
| 246 | public function exportShipping(Application $app, Request $request) |
||
| 315 | } |
||
| 316 |