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 |
||
| 36 | 2 | class MypageController extends AbstractController |
|
|
|
|||
| 37 | { |
||
| 38 | /** |
||
| 39 | * ログイン画面. |
||
| 40 | * |
||
| 41 | * @param Application $app |
||
| 42 | * @param Request $request |
||
| 43 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 44 | */ |
||
| 45 | public function login(Application $app, Request $request) |
||
| 77 | |||
| 78 | /** |
||
| 79 | 1 | * マイページ |
|
| 80 | 1 | * |
|
| 81 | * @param Application $app |
||
| 82 | * @param Request $request |
||
| 83 | 1 | * @return \Symfony\Component\HttpFoundation\Response |
|
| 84 | */ |
||
| 85 | public function index(Application $app, Request $request) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * 購入履歴詳細を表示する. |
||
| 119 | * |
||
| 120 | * @param Application $app |
||
| 121 | * @param Request $request |
||
| 122 | 1 | * @param $id |
|
| 123 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 124 | */ |
||
| 125 | public function history(Application $app, Request $request, $id) |
||
| 126 | { |
||
| 127 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 128 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
||
| 129 | $softDeleteFilter->setExcludes(array( |
||
| 130 | 'Eccube\Entity\ProductClass', |
||
| 131 | )); |
||
| 132 | |||
| 133 | 1 | $Order = $app['eccube.repository.order']->findOneBy(array( |
|
| 134 | 'id' => $id, |
||
| 135 | 'Customer' => $app->user(), |
||
| 136 | )); |
||
| 137 | 1 | ||
| 138 | $event = new EventArgs( |
||
| 139 | array( |
||
| 140 | 'Order' => $Order, |
||
| 141 | ), |
||
| 142 | $request |
||
| 143 | 1 | ); |
|
| 144 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event); |
||
| 145 | |||
| 146 | 1 | $Order = $event->getArgument('Order'); |
|
| 147 | |||
| 148 | if (!$Order) { |
||
| 149 | throw new NotFoundHttpException(); |
||
| 150 | 1 | } |
|
| 151 | |||
| 152 | return $app->render('Mypage/history.twig', array( |
||
| 153 | 'Order' => $Order, |
||
| 154 | )); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | 1 | * 再購入を行う. |
|
| 159 | * |
||
| 160 | * @param Application $app |
||
| 161 | * @param Request $request |
||
| 162 | * @param $id |
||
| 163 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 164 | */ |
||
| 165 | public function order(Application $app, Request $request, $id) |
||
| 166 | { |
||
| 167 | $this->isTokenValid($app); |
||
| 168 | |||
| 169 | 1 | $Customer = $app->user(); |
|
| 170 | 1 | ||
| 171 | /* @var $Order \Eccube\Entity\Order */ |
||
| 172 | $Order = $app['eccube.repository.order']->findOneBy(array( |
||
| 173 | 1 | 'id' => $id, |
|
| 174 | 'Customer' => $Customer, |
||
| 175 | )); |
||
| 176 | |||
| 177 | $event = new EventArgs( |
||
| 178 | array( |
||
| 179 | 1 | 'Order' => $Order, |
|
| 180 | 'Customer' => $Customer, |
||
| 181 | ), |
||
| 182 | $request |
||
| 183 | ); |
||
| 184 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE, $event); |
||
| 185 | |||
| 186 | if (!$Order) { |
||
| 187 | 1 | throw new NotFoundHttpException(); |
|
| 188 | } |
||
| 189 | |||
| 190 | foreach ($Order->getOrderDetails() as $OrderDetail) { |
||
| 191 | try { |
||
| 192 | if ($OrderDetail->getProduct()) { |
||
| 193 | $app['eccube.service.cart']->addProduct($OrderDetail->getProductClass()->getId(), $OrderDetail->getQuantity())->save(); |
||
| 194 | 1 | } else { |
|
| 195 | $app->addRequestError('cart.product.delete'); |
||
| 196 | } |
||
| 197 | } catch (CartException $e) { |
||
| 198 | $app->addRequestError($e->getMessage()); |
||
| 199 | 1 | } |
|
| 200 | } |
||
| 201 | |||
| 202 | $event = new EventArgs( |
||
| 203 | array( |
||
| 204 | 'Order' => $Order, |
||
| 205 | 'Customer' => $Customer, |
||
| 206 | ), |
||
| 207 | $request |
||
| 208 | ); |
||
| 209 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE, $event); |
||
| 210 | |||
| 211 | if ($event->getResponse() !== null) { |
||
| 212 | return $event->getResponse(); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $app->redirect($app->url('cart')); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * お気に入り商品を表示する. |
||
| 220 | * |
||
| 221 | * @param Application $app |
||
| 222 | * @param Request $request |
||
| 223 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 224 | */ |
||
| 225 | public function favorite(Application $app, Request $request) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * お気に入り商品を削除する. |
||
| 260 | * |
||
| 261 | * @param Application $app |
||
| 262 | * @param Request $request |
||
| 263 | * @param $id |
||
| 264 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 265 | */ |
||
| 266 | public function delete(Application $app, Request $request, $id) |
||
| 296 | } |
||
| 297 |