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 |
||
37 | class MypageController extends AbstractController |
||
|
|||
38 | { |
||
39 | /** |
||
40 | * ログイン画面. |
||
41 | * |
||
42 | * @param Application $app |
||
43 | * @param Request $request |
||
44 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
45 | */ |
||
46 | 4 | public function login(Application $app, Request $request) |
|
47 | { |
||
48 | 4 | if ($app->isGranted('IS_AUTHENTICATED_FULLY')) { |
|
49 | 2 | log_info('認証済のためログイン処理をスキップ'); |
|
50 | |||
51 | 2 | return $app->redirect($app->url('mypage')); |
|
52 | } |
||
53 | |||
54 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
55 | 2 | $builder = $app['form.factory'] |
|
56 | 2 | ->createNamedBuilder('', CustomerLoginType::class); |
|
57 | |||
58 | 2 | View Code Duplication | if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
59 | $Customer = $app->user(); |
||
60 | if ($Customer) { |
||
61 | $builder->get('login_email')->setData($Customer->getEmail()); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | 2 | $event = new EventArgs( |
|
66 | array( |
||
67 | 2 | 'builder' => $builder, |
|
68 | ), |
||
69 | $request |
||
70 | ); |
||
71 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE, $event); |
|
72 | |||
73 | 2 | $form = $builder->getForm(); |
|
74 | |||
75 | 2 | return $app->render('Mypage/login.twig', array( |
|
76 | 2 | 'error' => $app['security.last_error']($request), |
|
77 | 2 | 'form' => $form->createView(), |
|
78 | )); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * マイページ |
||
83 | * |
||
84 | * @param Application $app |
||
85 | * @param Request $request |
||
86 | * @return \Symfony\Component\HttpFoundation\Response |
||
87 | */ |
||
88 | 2 | public function index(Application $app, Request $request) |
|
89 | { |
||
90 | 2 | $Customer = $app['user']; |
|
91 | |||
92 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
93 | 2 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
|
94 | 2 | $softDeleteFilter->setExcludes(array( |
|
95 | 2 | 'Eccube\Entity\ProductClass', |
|
96 | )); |
||
97 | |||
98 | // 購入処理中/決済処理中ステータスの受注を非表示にする. |
||
99 | 2 | $app['orm.em'] |
|
100 | 2 | ->getFilters() |
|
101 | 2 | ->enable('incomplete_order_status_hidden'); |
|
102 | |||
103 | // paginator |
||
104 | 2 | $qb = $app['eccube.repository.order']->getQueryBuilderByCustomer($Customer); |
|
105 | |||
106 | 2 | $event = new EventArgs( |
|
107 | array( |
||
108 | 2 | 'qb' => $qb, |
|
109 | 2 | 'Customer' => $Customer, |
|
110 | ), |
||
111 | $request |
||
112 | ); |
||
113 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH, $event); |
|
114 | |||
115 | 2 | $pagination = $app['paginator']()->paginate( |
|
116 | $qb, |
||
117 | 2 | $request->get('pageno', 1), |
|
118 | 2 | $app['config']['search_pmax'] |
|
119 | ); |
||
120 | |||
121 | 2 | return $app->render('Mypage/index.twig', array( |
|
122 | 2 | 'pagination' => $pagination, |
|
123 | )); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * 購入履歴詳細を表示する. |
||
128 | * |
||
129 | * @param Application $app |
||
130 | * @param Request $request |
||
131 | * @param $id |
||
132 | * @return \Symfony\Component\HttpFoundation\Response |
||
133 | */ |
||
134 | 4 | public function history(Application $app, Request $request, $id) |
|
135 | { |
||
136 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
137 | 4 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
|
138 | 4 | $softDeleteFilter->setExcludes(array( |
|
139 | 4 | 'Eccube\Entity\ProductClass', |
|
140 | )); |
||
141 | |||
142 | 4 | $app['orm.em']->getFilters()->enable('incomplete_order_status_hidden'); |
|
143 | 4 | $Order = $app['eccube.repository.order']->findOneBy(array( |
|
144 | 4 | 'id' => $id, |
|
145 | 4 | 'Customer' => $app->user(), |
|
146 | )); |
||
147 | |||
148 | 4 | $event = new EventArgs( |
|
149 | array( |
||
150 | 4 | 'Order' => $Order, |
|
151 | ), |
||
152 | $request |
||
153 | ); |
||
154 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event); |
|
155 | |||
156 | 4 | $Order = $event->getArgument('Order'); |
|
157 | |||
158 | 4 | if (!$Order) { |
|
159 | 2 | throw new NotFoundHttpException(); |
|
160 | } |
||
161 | |||
162 | 2 | return $app->render('Mypage/history.twig', array( |
|
163 | 2 | 'Order' => $Order, |
|
164 | )); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * 再購入を行う. |
||
169 | * |
||
170 | * @param Application $app |
||
171 | * @param Request $request |
||
172 | * @param $id |
||
173 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
174 | */ |
||
175 | 2 | public function order(Application $app, Request $request, $id) |
|
235 | |||
236 | /** |
||
237 | * お気に入り商品を表示する. |
||
238 | * |
||
239 | * @param Application $app |
||
240 | * @param Request $request |
||
241 | * @return \Symfony\Component\HttpFoundation\Response |
||
242 | */ |
||
243 | 3 | public function favorite(Application $app, Request $request) |
|
276 | |||
277 | /** |
||
278 | * お気に入り商品を削除する. |
||
279 | * |
||
280 | * @param Application $app |
||
281 | * @param Request $request |
||
282 | * @param $id |
||
283 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
284 | */ |
||
285 | 2 | public function delete(Application $app, Request $request, $id) |
|
319 | } |
||
320 |