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 | 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 | 4 | public function login(Application $app, Request $request) |
|
79 | |||
80 | /** |
||
81 | * マイページ |
||
82 | * |
||
83 | * @param Application $app |
||
84 | * @param Request $request |
||
85 | * @return \Symfony\Component\HttpFoundation\Response |
||
86 | */ |
||
87 | 8 | public function index(Application $app, Request $request) |
|
124 | |||
125 | /** |
||
126 | * 購入履歴詳細を表示する. |
||
127 | * |
||
128 | * @param Application $app |
||
129 | * @param Request $request |
||
130 | * @param $id |
||
131 | * @return \Symfony\Component\HttpFoundation\Response |
||
132 | */ |
||
133 | 10 | public function history(Application $app, Request $request, $id) |
|
134 | { |
||
135 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
136 | 10 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
|
137 | 10 | $softDeleteFilter->setExcludes(array( |
|
138 | 10 | 'Eccube\Entity\ProductClass', |
|
139 | )); |
||
140 | |||
141 | 10 | $app['orm.em']->getFilters()->enable('incomplete_order_status_hidden'); |
|
142 | 10 | $Order = $app['eccube.repository.order']->findOneBy(array( |
|
143 | 10 | 'id' => $id, |
|
144 | 10 | 'Customer' => $app->user(), |
|
145 | )); |
||
146 | |||
147 | 10 | $event = new EventArgs( |
|
148 | array( |
||
149 | 10 | 'Order' => $Order, |
|
150 | ), |
||
151 | $request |
||
152 | ); |
||
153 | 10 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event); |
|
154 | |||
155 | 10 | $Order = $event->getArgument('Order'); |
|
156 | |||
157 | 10 | if (!$Order) { |
|
158 | 2 | throw new NotFoundHttpException(); |
|
159 | } |
||
160 | |||
161 | 8 | return $app->render('Mypage/history.twig', array( |
|
162 | 8 | 'Order' => $Order, |
|
163 | )); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * 再購入を行う. |
||
168 | * |
||
169 | * @param Application $app |
||
170 | * @param Request $request |
||
171 | * @param $id |
||
172 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
173 | */ |
||
174 | 8 | public function order(Application $app, Request $request, $id) |
|
175 | { |
||
176 | 8 | $this->isTokenValid($app); |
|
177 | |||
178 | 8 | log_info('再注文開始', array($id)); |
|
179 | |||
180 | 8 | $Customer = $app->user(); |
|
181 | |||
182 | /* @var $Order \Eccube\Entity\Order */ |
||
183 | 8 | $Order = $app['eccube.repository.order']->findOneBy(array( |
|
184 | 8 | 'id' => $id, |
|
185 | 8 | 'Customer' => $Customer, |
|
186 | )); |
||
187 | |||
188 | 8 | $event = new EventArgs( |
|
189 | array( |
||
190 | 8 | 'Order' => $Order, |
|
191 | 8 | 'Customer' => $Customer, |
|
192 | ), |
||
193 | $request |
||
194 | ); |
||
195 | 8 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE, $event); |
|
196 | |||
197 | 8 | if (!$Order) { |
|
198 | log_info('対象の注文が見つかりません', array($id)); |
||
199 | throw new NotFoundHttpException(); |
||
200 | } |
||
201 | |||
202 | 8 | foreach ($Order->getOrderDetails() as $OrderDetail) { |
|
203 | try { |
||
204 | 8 | if ($OrderDetail->getProduct() && |
|
205 | 8 | $OrderDetail->getProductClass()) { |
|
206 | 8 | $app['eccube.service.cart']->addProduct($OrderDetail->getProductClass()->getId(), $OrderDetail->getQuantity())->save(); |
|
207 | } else { |
||
208 | log_info($app->trans('cart.product.delete'), array($id)); |
||
209 | 7 | $app->addRequestError('cart.product.delete'); |
|
210 | } |
||
211 | 2 | } catch (CartException $e) { |
|
212 | 2 | log_info($e->getMessage(), array($id)); |
|
213 | 8 | $app->addRequestError($e->getMessage()); |
|
214 | } |
||
215 | } |
||
216 | |||
217 | 8 | $event = new EventArgs( |
|
218 | array( |
||
219 | 8 | 'Order' => $Order, |
|
220 | 8 | 'Customer' => $Customer, |
|
221 | ), |
||
222 | $request |
||
223 | ); |
||
224 | 8 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE, $event); |
|
225 | |||
226 | 8 | if ($event->getResponse() !== null) { |
|
227 | return $event->getResponse(); |
||
228 | } |
||
229 | |||
230 | 8 | log_info('再注文完了', array($id)); |
|
231 | |||
232 | 8 | return $app->redirect($app->url('cart')); |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * お気に入り商品を表示する. |
||
237 | * |
||
238 | * @param Application $app |
||
239 | * @param Request $request |
||
240 | * @return \Symfony\Component\HttpFoundation\Response |
||
241 | */ |
||
242 | 3 | public function favorite(Application $app, Request $request) |
|
275 | |||
276 | /** |
||
277 | * お気に入り商品を削除する. |
||
278 | * |
||
279 | * @param Application $app |
||
280 | * @param Request $request |
||
281 | * @param $id |
||
282 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
283 | */ |
||
284 | 2 | public function delete(Application $app, Request $request, $id) |
|
318 | } |
||
319 |